diff --git a/packages/code-runtime/code-runtime-python/src/index.ts b/packages/code-runtime/code-runtime-python/src/index.ts index 575a9076e6..f2ea5ec531 100644 --- a/packages/code-runtime/code-runtime-python/src/index.ts +++ b/packages/code-runtime/code-runtime-python/src/index.ts @@ -1082,8 +1082,15 @@ export class PythonCodeRuntime extends CodeRuntime { } const deadline = Date.now() + this.config.graceMs + CLOSE_REAP_MARGIN_MS const pollGroup = (): void => { + // The deadline is a backstop: the group is reachable by `kill(-pid)` + // and SIGKILL is uncatchable, so it always empties within graceMs — the + // `Date.now() >= deadline` arm exists only so a probe that never sees + // ESRCH (a kernel quirk) cannot hang disposal forever. + /* v8 ignore next -- SIGKILL always empties the reachable group before the deadline. */ if (groupEmpty() || Date.now() >= deadline) { - if (graceTimer !== undefined) clearTimeout(graceTimer) + // graceTimer is always defined here: pollGroup runs only when + // `killing` is set, and kill() arms graceTimer before any settle. + clearTimeout(graceTimer) finalize() return }