-
-
Notifications
You must be signed in to change notification settings - Fork 161
fix(child-process): resume spawn waiters before lifecycle events #9647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Issue #9535: each child-process event is its own event-loop callback. | ||
| // Promise continuations released by `spawn` must therefore run before a | ||
| // short-lived child's already-queued data/exit/close events are delivered. | ||
| import { spawn } from "node:child_process"; | ||
|
|
||
| const order: string[] = []; | ||
| const child = spawn("/bin/echo", ["hello"]); | ||
|
|
||
| child.on("spawn", () => order.push("spawn")); | ||
| child.stdout!.on("data", () => order.push("data")); | ||
| child.on("exit", () => order.push("exit")); | ||
| child.on("close", () => order.push("close")); | ||
|
|
||
| // Let the tiny child finish before the first event-loop pump. This removes a | ||
| // scheduler race and exercises the bug's defining case: spawn and the full | ||
| // lifecycle are already queued together. | ||
| const spinUntil = Date.now() + 100; | ||
| while (Date.now() < spinUntil) {} | ||
|
|
||
| await new Promise<void>((resolve) => child.on("spawn", resolve)); | ||
| order.push("resumed-after-spawn"); | ||
| await Promise.resolve(); | ||
| order.push("microtask"); | ||
| await new Promise<void>((resolve) => setImmediate(resolve)); | ||
| order.push("immediate"); | ||
|
|
||
| setTimeout(() => console.log(order.join(" ")), 300); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Wait for The fixed timeout does not prove that the child completed. Under load, this can print a partial sequence and miss the terminal-event regression. Create a Proposed fix child.on("exit", () => order.push("exit"));
child.on("close", () => order.push("close"));
+const closed = new Promise<void>((resolve) => child.once("close", resolve));
...
-await new Promise<void>((resolve) => setImmediate(resolve));
+await new Promise<void>((resolve) => setImmediate(resolve));
order.push("immediate");
-
-setTimeout(() => console.log(order.join(" ")), 300);
+await closed;
+console.log(order.join(" "));🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Prevent lifecycle-event starvation.
A
spawnlistener can create another child. The next pump then has a non-emptyto_spawnlist and returns again. This repeats indefinitely while Phase A and Phase B never process earlier children.Queued
data,exit, andcloseevents can remain pending. The registry entries and live-handle counts then cannot be released. Defer lifecycle delivery per newly spawned child instead of bypassing all pending lifecycle work whenever any child needsspawn.🤖 Prompt for AI Agents