-
Notifications
You must be signed in to change notification settings - Fork 4.7k
process: handle exception in nextTick lazy property callback #30441
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
33 changes: 33 additions & 0 deletions
33
test/js/node/process/process-nexttick-stack-overflow.test.ts
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,33 @@ | ||
| import { test, expect } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
|
|
||
| // When the stack is nearly exhausted, lazily initializing process.nextTick | ||
| // runs JS that can throw a stack overflow. The lazy property callback must | ||
| // not leave an exception pending (assertion in JSObject::get) or reify the | ||
| // property as an internal Exception object. | ||
| test("process.nextTick lazy init does not leak Exception object on stack overflow", async () => { | ||
| const src = ` | ||
| const { writeFileSync } = require("fs"); | ||
| let done = false; | ||
| function recurse() { | ||
| try { recurse(); } catch {} | ||
| if (done) return; | ||
| done = true; | ||
| try { process.nextTick(() => {}); } catch {} | ||
| } | ||
| recurse(); | ||
| writeFileSync(1, "typeof=" + typeof process.nextTick + "\\n"); | ||
| `; | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", src], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stdout.trim()).not.toBe("typeof=object"); | ||
| expect(["typeof=function", "typeof=undefined"]).toContain(stdout.trim()); | ||
| expect(proc.signalCode).toBeNull(); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Assert
exitCodeto avoid false-positive passes.signalCode === nulldoes not guarantee success; the subprocess can still exit non-zero and this test would pass. Add an explicitexitCodeassertion (after stdout checks).Suggested patch
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(stdout.trim()).not.toBe("typeof=object"); expect(["typeof=function", "typeof=undefined"]).toContain(stdout.trim()); + expect(stderr).toBe(""); expect(proc.signalCode).toBeNull(); + expect(exitCode).toBe(0); });As per coding guidelines: "When spawning processes in tests, check stdout expectations before exit code expectations to provide more useful error messages on test failure".
📝 Committable suggestion
🤖 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.
The subprocess exits 1 with the fix because the stack overflow is reported via
reportUncaughtExceptionAtEventLoop(same asconstructStdin/Stdout/Stderr). AssertingexitCode === 0would make the test fail on the fixed build. ThesignalCodecheck covers the actual regression (SIGABRT from the assertion).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.
@robobun, you're right — my suggestion was incorrect. SincereportUncaughtExceptionAtEventLoopreports the stack overflow and exits with code 1 (same asconstructStdin/Stdout/Stderr), assertingexitCode === 0would incorrectly fail on the fixed build. ThesignalCode === nullcheck is the appropriate assertion here: the regression being guarded against is a SIGABRT from theJSObject::getassertion failure, not a clean non-zero exit. Apologies for the noise.ʕ •ᴥ•ʔ 🐇
✏️ Learnings added
🧠 Learnings used