fix(subprocess): treat zombie-only groups as quiescent

This commit is contained in:
Tianyi Cui
2026-07-29 06:04:36 +08:00
parent ea75333c92
commit d29c73ddd6
4 changed files with 76 additions and 1 deletions
@@ -87,6 +87,35 @@ function readLinuxStat(internals: ProcessInspectorInternals, pid: number): ProcS
}
}
/**
* Report whether a Linux process group has an executing member. `false`
* means the group contains only zombie/dead entries; `undefined` means the
* process table could not prove either outcome.
* @param processGroupId - POSIX process-group id to inspect.
* @param internals - injectable process-table operations.
* @returns Live-member presence, or `undefined` when unavailable/absent.
*/
export function linuxProcessGroupHasLiveMembers(
processGroupId: number,
internals: ProcessInspectorInternals = DEFAULT_INTERNALS,
): boolean | undefined {
let entries: string[]
try {
entries = internals.readDir('/proc')
} catch (_unreadableProcDirectory) {
return undefined
}
let matched = false
for (const entry of entries) {
if (!/^\d+$/.test(entry)) continue
const stat = readLinuxStat(internals, Number(entry))
if (stat?.pgrp !== processGroupId) continue
matched = true
if (!/^[ZXx]$/.test(stat.state)) return true
}
return matched ? false : undefined
}
function numericEntries(internals: ProcessInspectorInternals, path: string): number[] {
try {
return internals.readDir(path).filter(entry => /^\d+$/.test(entry)).map(Number)
@@ -23,6 +23,7 @@ import type {
SubprocessOutputMode,
SubprocessSpawnSpec,
} from '@deepseek-ai/dsh-subprocess'
import { linuxProcessGroupHasLiveMembers } from './process-inspector.ts'
/**
* Build a child environment: explicit caller entries override the scrubbed
@@ -52,6 +53,8 @@ export interface SpawnInternals {
taskkill?: (pid: number) => void
/** Host platform override for signalling decisions. */
platform?: NodeJS.Platform
/** Linux process-group member probe (defaults to `/proc` inspection). */
linuxProcessGroupHasLiveMembers?: (processGroupId: number) => boolean | undefined
}
/**
@@ -312,6 +315,7 @@ export function spawnSubprocess(spec: SubprocessSpawnSpec, internals: SpawnInter
const spillDir = internals.spillDir ?? privateSpillDir()
const platform = internals.platform ?? process.platform
const taskkill = internals.taskkill ?? taskkillProcessTree
const linuxGroupHasLiveMembers = internals.linuxProcessGroupHasLiveMembers ?? linuxProcessGroupHasLiveMembers
if (spec.signal?.aborted) {
throw new Error(`aborted before spawn: ${String(spec.signal.reason ?? 'aborted')}`)
@@ -366,6 +370,11 @@ export function spawnSubprocess(spec: SubprocessSpawnSpec, internals: SpawnInter
}
try {
process.kill(-pid, 0)
// A group containing only unreaped zombies still answers kill(0), but
// it can execute no work and cannot be signalled into quiescence. Only
// inspect after direct-child settlement so live-process polls remain a
// syscall rather than repeated process-table scans.
if (settled && platform === 'linux' && linuxGroupHasLiveMembers(pid) === false) return false
return true
} catch (error) {
const code = (error as NodeJS.ErrnoException).code