fix(installer): bound outputPromise wait after kill to prevent indirect hang

After SIGTERM/SIGKILL escalation, the stdout stream may not close immediately on all platforms. The unconditional await on outputPromise could then hang indefinitely, defeating the bounded process lifetime guarantee. Race outputPromise against a short follow-up timeout to ensure getOpenCodeVersion always returns within a bounded time.

Refs #3766
This commit is contained in:
YeonGyu-Kim
2026-05-11 09:24:42 +09:00
parent 84073897c6
commit 5d1c8718d7
2 changed files with 42 additions and 4 deletions
+39 -2
View File
@@ -10,7 +10,11 @@ type OpenCodeBinaryModule = typeof import("./opencode-binary")
type CreateProcOptions = {
exitCode?: number | null
exited?: Promise<number>
output?: { stdout?: string; stderr?: string }
output?: {
stdout?: string
stdoutStream?: ReadableStream<Uint8Array>
stderr?: string
}
kill?: (signal?: NodeJS.Signals) => void
}
@@ -19,7 +23,9 @@ function createProc(options: CreateProcOptions = {}): ReturnType<typeof spawnHel
return {
exited: options.exited ?? Promise.resolve(exitCode),
exitCode,
stdout: options.output?.stdout !== undefined ? new Blob([options.output.stdout]).stream() : undefined,
stdout:
options.output?.stdoutStream ??
(options.output?.stdout !== undefined ? new Blob([options.output.stdout]).stream() : undefined),
stderr: options.output?.stderr !== undefined ? new Blob([options.output.stderr]).stream() : undefined,
kill: options.kill ?? (() => {}),
} satisfies ReturnType<typeof spawnHelpers.spawnWithWindowsHide>
@@ -103,6 +109,37 @@ describe("getOpenCodeVersion (installer)", () => {
})
})
describe("#given never-closing stdout after kill #when getOpenCodeVersion #then returns within bounded time", () => {
it("bounds outputPromise wait and returns null", async () => {
const neverClosingStdout = new ReadableStream<Uint8Array>({
start() {
// Intentionally never closing to simulate a hung stdout stream.
},
})
spawnSpy.mockReturnValue(
createProc({
exited: new Promise<number>(() => {}),
output: { stdoutStream: neverClosingStdout },
kill: () => {},
}),
)
const immediateSetTimeout = ((handler: TimerHandler) => {
if (typeof handler === "function") {
handler()
}
return 1 as unknown as ReturnType<typeof setTimeout>
}) as unknown as typeof globalThis.setTimeout
const setTimeoutSpy = spyOn(globalThis, "setTimeout").mockImplementation(immediateSetTimeout)
const result = await getOpenCodeVersion()
expect(result).toBe(null)
setTimeoutSpy.mockRestore()
})
})
describe("#given quick successful exit #when getOpenCodeVersion #then clears the watchdog timer", () => {
it("avoids timer leak after success", async () => {
spawnSpy.mockReturnValue(createProc({ output: { stdout: "1.14.33\n" } }))
+3 -2
View File
@@ -6,6 +6,7 @@ import { initConfigContext } from "./config-context"
const OPENCODE_BINARIES = ["opencode", "opencode-desktop"] as const
const OPENCODE_VERSION_CHECK_TIMEOUT_MS = 1500
const OPENCODE_VERSION_KILL_GRACE_MS = 200
const OPENCODE_OUTPUT_WAIT_TIMEOUT_MS = 200
interface OpenCodeBinaryResult {
binary: OpenCodeBinaryType
@@ -44,9 +45,9 @@ async function findOpenCodeBinaryWithVersion(): Promise<OpenCodeBinaryResult | n
new Promise<string>((resolve) => {
setTimeout(() => {
resolve("")
}, OPENCODE_VERSION_KILL_GRACE_MS)
}, OPENCODE_OUTPUT_WAIT_TIMEOUT_MS)
}),
])
]).catch(() => "")
if (timedExitCode === 0 && proc.exitCode === 0) {
const version = extractSemverFromOutput(output) ?? output.trim()