test(subprocess): accept zombie quiescence

This commit is contained in:
Tianyi Cui
2026-08-08 21:27:57 +08:00
parent 06a9224904
commit ea047cc634
@@ -60,7 +60,7 @@ function spec(command: string, overrides: SpecOverrides = {}) {
}
}
/** Poll until a pid no longer exists (kill(pid, 0) throws ESRCH). */
/** Poll until a pid no longer exists, or is only a zombie on Linux. */
async function waitGone(pid: number, timeoutMs = 5_000): Promise<void> {
const deadline = Date.now() + timeoutMs
while (Date.now() < deadline) {
@@ -69,6 +69,16 @@ async function waitGone(pid: number, timeoutMs = 5_000): Promise<void> {
} catch {
return
}
if (process.platform === 'linux') {
try {
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8')
const state = stat.slice(stat.lastIndexOf(')') + 2, stat.lastIndexOf(')') + 3)
if (state === 'Z' || state === 'X') return
} catch (error: unknown) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return
throw error
}
}
await new Promise(resolve => setTimeout(resolve, 20))
}
throw new Error(`pid ${pid} still alive after ${timeoutMs}ms`)