-
-
Notifications
You must be signed in to change notification settings - Fork 164
fix(child_process): throw an unhandled spawn/fork error event; close out #11490 #11350 #10730 #11359
#11623
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
Merged
Merged
fix(child_process): throw an unhandled spawn/fork error event; close out #11490 #11350 #10730 #11359
#11623
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| **child_process: an `error` event with no listener now throws, as in Node (#10730).** When `spawn()` or `fork()` fails (for example `ENOENT` for a missing binary), Perry emits a deferred `error` event on the ChildProcess. If no `error` listener was registered, Perry used to drop the error, fire `close`, and let the program continue. Node's EventEmitter throws an unhandled `error` event instead: the process dies with `Unhandled 'error' event` before `close` fires. | ||
|
|
||
| That gap is why `test_gap_9592_child_timeout_threads` passed for Perry on macOS while Node crashed. The fixture spawned `/bin/true`, which macOS does not have, and never listened for `error`. #10730 guessed Perry was resolving the missing absolute path through `PATH`. It was not: Perry reported `ENOENT` correctly, and nothing was listening for it. | ||
|
|
||
| The emit now reports whether any listener took the error (`failed_spawn::emit_spawn_error`). If none did, the error object is thrown as-is from the `setImmediate` callback that delivers it, after that frame's handle scopes have dropped, so `uncaughtException` and the uncaught-exit path see the same object Node rethrows. `close` is not scheduled in that case. A handled error behaves as before. No JS-callable native signature changed. | ||
|
|
||
| Regression coverage: | ||
| - `test-files/test_gap_10730_child_unhandled_error.ts` checks three cases: a handled error, an unhandled one, and a listener that was removed. It is byte-identical to Node with the fix. Without the fix Perry prints `close without listener` and no `uncaught` lines. | ||
| - `child_process::failed_spawn::tests::spawn_error_{without,with}_listener_is_{unhandled,handled}` are the unit-level half. | ||
|
|
||
| `perry-runtime --lib` single-threaded: main 4704 passed, 0 failed. This change: 4706 passed, 0 failed (the two new tests). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // #10730 — a ChildProcess `error` event with no listener must throw, as every | ||
| // Node EventEmitter does. Perry dropped it: a failed spawn of a missing binary | ||
| // looked like a child that ran, `close` fired, and the program carried on. That | ||
| // is how test_gap_9592's missing `/bin/true` on macOS read as a Perry pass | ||
| // while Node died with `Unhandled 'error' event`. | ||
| // | ||
| // The throw is observed through `uncaughtException` so the result is printed | ||
| // rather than inferred from an exit status. | ||
| import { spawn } from "node:child_process"; | ||
|
|
||
| // Absolute, and absent on every platform the suite runs on. | ||
| const MISSING = "/nonexistent-perry-10730/true"; | ||
|
|
||
| const events: string[] = []; | ||
| process.on("uncaughtException", (err: any) => { | ||
| events.push(`uncaught ${err.code} ${err.syscall} ${err instanceof Error}`); | ||
| }); | ||
|
|
||
| // 1. With a listener: the error is delivered, nothing is thrown, `close` fires. | ||
| await new Promise<void>((resolve) => { | ||
| const child = spawn(MISSING, [], { stdio: "ignore" }); | ||
| child.on("error", (err: any) => events.push(`handled ${err.code} ${err.path}`)); | ||
| child.on("close", (code: any) => { | ||
| events.push(`close ${code}`); | ||
| resolve(); | ||
| }); | ||
| }); | ||
|
|
||
| // 2. Without one: the error is thrown, and `close` never fires. | ||
| { | ||
| const child = spawn(MISSING, ["a"], { stdio: "ignore" }); | ||
| child.on("close", () => events.push("close without listener")); | ||
| } | ||
|
|
||
| // 3. A listener that was removed again is no listener. | ||
| { | ||
| const child = spawn(MISSING, ["b"], { stdio: "ignore" }); | ||
| const onError = () => events.push("removed listener ran"); | ||
| child.on("error", onError); | ||
| child.removeListener("error", onError); | ||
| } | ||
|
|
||
| setTimeout(() => { | ||
| for (const line of events) console.log(line); | ||
| }, 200); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Root the test error before storing it.
If GC runs while
cp_set_fieldcreates the__cpErrorfield-name string, it can move the error held only in the rawerrlocal. The helper can then store a stale value and make either new test fail for the wrong reason. Rooterrinscopeand reload it for the store. Inspawn_error_without_listener_is_unhandled, also root the value read on Line 135 before comparing it afteremit_spawn_error, which can allocate. (raw.githubusercontent.com)Based on learnings, Perry’s production GC does not conservatively scan Rust locals, so NaN-boxed values must be rooted and reloaded across allocating operations.
🤖 Prompt for AI Agents
Source: Learnings