-
Notifications
You must be signed in to change notification settings - Fork 5k
Exit 13 on unsettled top-level await in the main entry instead of spinning #36051
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 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
263fac4
Exit 13 on unsettled top-level await in the main entry instead of spi…
robobun 568a1c7
[autofix.ci] apply automated fixes
autofix-ci[bot] 838edac
Trim inline commentary to satisfy comment-cop
robobun 06a1efa
test: assert stderr before exit status; assert entry path in dependen…
robobun 08a94d9
Report a late-rejected entry promise; update spawn unref TLA tests to…
robobun 771bc2c
ci: retrigger
robobun 9bff8dc
Protect the entry promise across GC; gate the TLA check on unhandled_…
robobun 4a34c72
Trim comment to one line (comment-cop)
robobun 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
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,115 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // When the entry module's top-level await never settles and nothing else refs | ||
| // the event loop, Bun used to spin `wait_for_promise` forever (100% CPU, no | ||
| // epoll park). Node prints a warning and exits 13. Issue #33283. | ||
|
|
||
| // Watchdog below the per-test timeout so a regression surfaces as a clean | ||
| // SIGTERM assertion instead of a suite-level timeout. The fixed path exits in | ||
| // well under a second. | ||
| const watchdog = 3_000; | ||
|
|
||
| async function run(cmd: string[], cwd?: string) { | ||
| await using proc = Bun.spawn({ | ||
| cmd, | ||
| env: bunEnv, | ||
| cwd, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| timeout: watchdog, | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout, stderr, exitCode, signalCode: proc.signalCode }; | ||
| } | ||
|
|
||
| test.concurrent("never-settling top-level await exits 13 with a warning", async () => { | ||
| using dir = tempDir("tla-unsettled", { | ||
| "a.mjs": `await new Promise(() => {});\n`, | ||
| }); | ||
| const { stderr, exitCode, signalCode } = await run([bunExe(), "a.mjs"], String(dir)); | ||
| expect(stderr).toContain("Detected unsettled top-level await at"); | ||
| expect(stderr).toContain("a.mjs"); | ||
| expect({ signalCode, exitCode }).toEqual({ signalCode: null, exitCode: 13 }); | ||
| }); | ||
|
|
||
| test.concurrent("never-settling top-level await in a dependency exits 13", async () => { | ||
| using dir = tempDir("tla-unsettled-dep", { | ||
| "entry.mjs": `import { ready } from "./dep.mjs";\nawait ready;\n`, | ||
| "dep.mjs": `export const ready = new Promise(() => {});\n`, | ||
| }); | ||
| const { stderr, exitCode, signalCode } = await run([bunExe(), "entry.mjs"], String(dir)); | ||
| expect(stderr).toContain("Detected unsettled top-level await"); | ||
| expect(stderr).toContain("entry.mjs"); | ||
| expect({ signalCode, exitCode }).toEqual({ signalCode: null, exitCode: 13 }); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test.concurrent("top-level await resolved by a ref'd timer exits 0", async () => { | ||
| using dir = tempDir("tla-timer", { | ||
| "a.mjs": `await new Promise(r => setTimeout(r, 50));\nconsole.log("done");\n`, | ||
| }); | ||
| const { stdout, stderr, exitCode, signalCode } = await run([bunExe(), "a.mjs"], String(dir)); | ||
| expect(stdout).toBe("done\n"); | ||
| expect(stderr).not.toContain("Detected unsettled top-level await"); | ||
| expect({ signalCode, exitCode }).toEqual({ signalCode: null, exitCode: 0 }); | ||
| }); | ||
|
|
||
| test.concurrent("top-level await resolved by a microtask exits 0", async () => { | ||
| using dir = tempDir("tla-micro", { | ||
| "a.mjs": `await new Promise(r => queueMicrotask(r));\nconsole.log("done");\n`, | ||
| }); | ||
| const { stdout, stderr, exitCode, signalCode } = await run([bunExe(), "a.mjs"], String(dir)); | ||
| expect(stdout).toBe("done\n"); | ||
| expect(stderr).not.toContain("Detected unsettled top-level await"); | ||
| expect({ signalCode, exitCode }).toEqual({ signalCode: null, exitCode: 0 }); | ||
| }); | ||
|
|
||
| test.concurrent("unsettled top-level await preserves a user-set process.exitCode", async () => { | ||
| using dir = tempDir("tla-exitcode", { | ||
| "a.mjs": `process.exitCode = 7;\nawait new Promise(() => {});\n`, | ||
| }); | ||
| const { stderr, exitCode, signalCode } = await run([bunExe(), "a.mjs"], String(dir)); | ||
| expect(stderr).toContain("Detected unsettled top-level await"); | ||
| expect({ signalCode, exitCode }).toEqual({ signalCode: null, exitCode: 7 }); | ||
| }); | ||
|
|
||
| test.concurrent("beforeExit fires with 0 before the unsettled-TLA warning", async () => { | ||
| using dir = tempDir("tla-beforeexit", { | ||
| "a.mjs": `process.on("beforeExit", c => console.error("beforeExit", c));\n` + `await new Promise(() => {});\n`, | ||
| }); | ||
| const { stderr, exitCode, signalCode } = await run([bunExe(), "a.mjs"], String(dir)); | ||
| const before = stderr.indexOf("beforeExit 0"); | ||
| const warn = stderr.indexOf("Detected unsettled top-level await"); | ||
| expect(before).toBeGreaterThanOrEqual(0); | ||
| expect(warn).toBeGreaterThan(before); | ||
| expect({ signalCode, exitCode }).toEqual({ signalCode: null, exitCode: 13 }); | ||
| }); | ||
|
|
||
| test.concurrent("bun -e with a never-settling top-level await exits 13", async () => { | ||
| const { stderr, exitCode, signalCode } = await run([bunExe(), "-e", "await new Promise(() => {})"]); | ||
| expect(stderr).toContain("Detected unsettled top-level await"); | ||
| expect({ signalCode, exitCode }).toEqual({ signalCode: null, exitCode: 13 }); | ||
| }); | ||
|
|
||
| test.concurrent("uncaught exception during top-level await does not print a spurious TLA warning", async () => { | ||
| using dir = tempDir("tla-uncaught", { | ||
| "a.mjs": `setImmediate(() => { throw new Error("boom"); });\nawait new Promise(r => setTimeout(r, 100));\n`, | ||
| }); | ||
| const { stderr, exitCode, signalCode } = await run([bunExe(), "a.mjs"], String(dir)); | ||
| expect(stderr).toContain("boom"); | ||
| expect(stderr).not.toContain("Detected unsettled top-level await"); | ||
| expect({ signalCode, exitCode }).toEqual({ signalCode: null, exitCode: 1 }); | ||
| }); | ||
|
|
||
| test.concurrent("top-level await rejected during beforeExit is reported (exit 1, not swallowed)", async () => { | ||
| using dir = tempDir("tla-reject-beforeexit", { | ||
| "a.mjs": | ||
| `const { promise, reject } = Promise.withResolvers();\n` + | ||
| `process.once("beforeExit", () => setImmediate(() => reject(new Error("db never connected"))));\n` + | ||
| `await promise;\n`, | ||
| }); | ||
| const { stderr, exitCode, signalCode } = await run([bunExe(), "a.mjs"], String(dir)); | ||
| expect(stderr).toContain("db never connected"); | ||
| expect(stderr).not.toContain("Detected unsettled top-level await"); | ||
| expect({ signalCode, exitCode }).toEqual({ signalCode: null, exitCode: 1 }); | ||
| }); | ||
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.