Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Report a version check its timeout cut off as a timeout

Klaus Singer1daf823e9095
diff --git a/server/procs.test.ts b/server/procs.test.ts
index 7eb6014487..ced9c02b0b 100644
--- a/server/procs.test.ts
+++ b/server/procs.test.ts
@@ -121,4 +121,15 @@ describe("CLI version probes", () => {
expect(read(trapped, "")).toEqual({ version: "2.1.302" });
expect(read(Object.assign(new Error("spawn claude ENOENT"), { code: "ENOENT" }), "")).toEqual({ reason: "`claude` CLI not found" });
}, hostTimeout(20_000));
+
+ it("names a timeout, not a missing CLI, when a busy server reads the answer only after the timeout", async () => {
+ const late = await new Promise<{ err: Error | null; stdout: string }>((resolve) => {
+ execCli(process.execPath, ["-e", "process.stdout.write('2.1.302')"], { timeout: 100 }, (err, stdout) => resolve({ err, stdout }));
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1_500);
+ });
+ expect(cliVersionReader("claude")(late.err, late.stdout)).toEqual({ reason: expect.stringContaining("took longer than 8 seconds") });
+ const read = cliVersionReader("claude");
+ expect(read(null, "2.1.302")).toEqual({ version: "2.1.302" });
+ expect(read(late.err, late.stdout)).toEqual({ version: "2.1.302" });
+ }, hostTimeout(20_000));
});
diff --git a/server/procs.ts b/server/procs.ts
index 56df248712..5ab7da4dd0 100644
--- a/server/procs.ts
+++ b/server/procs.ts
@@ -240,18 +240,31 @@ export function execCli(
return;
}
let escalate: NodeJS.Timeout | undefined;
+ let deadline: NodeJS.Timeout | undefined;
+ let timedOut = false;
const child = execFile(resolved.command, resolved.args, { ...opts, windowsHide: true, encoding: "utf8" }, (err, stdout, stderr) => {
clearTimeout(escalate);
- cb(err, stdout, stderr);
+ clearTimeout(deadline);
+ cb(err ?? (timedOut ? cutOffError(cli, opts.timeout) : null), stdout, stderr);
});
// Node (24) calls back only once the child exits, and a CLI wrapper that traps SIGTERM outlives the timeout's signal.
// Only the child is stopped: execFile starts no process group, so a grandchild that ignores SIGTERM can outlive it.
+ // When its timeout fires after the child exited but before Node saw the exit, Node (24) drops the output and still
+ // reports success; this timer is queued just after Node's, so it marks that run as timed out.
if (opts.timeout) {
escalate = setTimeout(() => void killCliTree(child, 0), opts.timeout + EXEC_KILL_GRACE_MS);
escalate.unref?.();
+ deadline = setTimeout(() => {
+ timedOut = true;
+ }, opts.timeout);
+ deadline.unref?.();
}
}
+function cutOffError(cli: string, timeout: number | undefined): Error {
+ return Object.assign(new Error(`${cli} timed out after ${timeout} ms`), { killed: true, code: null, signal: null });
+}
+
/** Concurrent probes on a busy machine can outlast the timeout, so a CLI that answered before keeps its last version. */
export function cliVersionReader(cli: string): (err: Error | null, stdout: string) => { version: string } | { reason: string } {
let last: string | null = null;