fix(doctor): add timeouts to subprocess spawns to prevent exit code 137
Doctor checks spawned subprocesses (gh, opencode, sg) without timeouts, causing the process to hang indefinitely if any binary was stuck. The OS would then SIGKILL the process (exit code 137). - Add spawnWithTimeout utility with 10s per-spawn timeout - Add 30s overall doctor command timeout with graceful error message - Update all doctor check subprocess calls to use timeouts
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { describe, it, expect } from "bun:test"
|
||||
import { spawnWithTimeout } from "./spawn-with-timeout"
|
||||
|
||||
describe("spawnWithTimeout", () => {
|
||||
describe("#given a command that completes quickly", () => {
|
||||
it("returns stdout and exit code", async () => {
|
||||
// when
|
||||
const result = await spawnWithTimeout(["echo", "hello"], { stdout: "pipe", stderr: "pipe" })
|
||||
|
||||
// then
|
||||
expect(result.timedOut).toBe(false)
|
||||
expect(result.exitCode).toBe(0)
|
||||
expect(result.stdout.trim()).toBe("hello")
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given a command that fails", () => {
|
||||
it("returns non-zero exit code without timing out", async () => {
|
||||
// when
|
||||
const result = await spawnWithTimeout(["false"], { stdout: "pipe", stderr: "pipe" })
|
||||
|
||||
// then
|
||||
expect(result.timedOut).toBe(false)
|
||||
expect(result.exitCode).not.toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given a command that exceeds timeout", () => {
|
||||
it("returns timedOut true and kills the process", async () => {
|
||||
// when
|
||||
const result = await spawnWithTimeout(
|
||||
["bash", "-c", "while true; do :; done"],
|
||||
{ stdout: "pipe", stderr: "pipe" },
|
||||
200
|
||||
)
|
||||
|
||||
// then
|
||||
expect(result.timedOut).toBe(true)
|
||||
expect(result.stdout).toBe("")
|
||||
})
|
||||
})
|
||||
|
||||
describe("#given a nonexistent command", () => {
|
||||
it("handles gracefully without hanging", async () => {
|
||||
// when
|
||||
const result = await spawnWithTimeout(
|
||||
["nonexistent-binary-that-does-not-exist-12345"],
|
||||
{ stdout: "pipe", stderr: "pipe" },
|
||||
2000
|
||||
)
|
||||
|
||||
// then
|
||||
expect(result.timedOut).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user