-
Notifications
You must be signed in to change notification settings - Fork 5k
error: recover async stack frames dropped under AsyncLocalStorage #35772
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
+278
−49
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a4b4fc3
error: recover async stack frames dropped under AsyncLocalStorage
robobun 0c8a853
[autofix.ci] apply automated fixes
autofix-ci[bot] 052714f
Install the hook lazily from jsSetAsyncHooksEnabled and trim comments
robobun 9f710c7
test: install the hook in the no-store baseline subprocesses
robobun ca5c60b
ci: retrigger
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
Some comments aren't visible on the classic Files Changed page.
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,129 @@ | ||
| // https://github.com/oven-sh/bun/issues/24003 | ||
| // Async stack traces were missing their `at async <fn>` frames when an | ||
| // AsyncLocalStorage store was active at the point of `await`. | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe } from "harness"; | ||
|
|
||
| async function run(source: string): Promise<string> { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", source], | ||
| env: bunEnv, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| expect(exitCode).toBe(0); | ||
| return stdout; | ||
| } | ||
|
|
||
| function asyncFrames(stack: string): string[] { | ||
| return stack | ||
| .split("\n") | ||
| .filter(l => l.includes("at async ")) | ||
| .map(l => l.trim().replace(/ \(.*\)$/, "")); | ||
| } | ||
|
|
||
| describe.concurrent("issue #24003", () => { | ||
| test("async stack frames under AsyncLocalStorage.run()", async () => { | ||
| const stdout = await run(` | ||
| const { AsyncLocalStorage } = require("node:async_hooks"); | ||
| const als = new AsyncLocalStorage(); | ||
| async function fn3() { await 0; throw new Error("boom"); } | ||
| async function fn2() { await fn3(); } | ||
| async function fn1() { await fn2(); } | ||
| async function main() { | ||
| try { await als.run({}, () => fn1()); } | ||
| catch (e) { console.log(e.stack); } | ||
| } | ||
| main(); | ||
| `); | ||
| expect(stdout).toContain("at fn3"); | ||
| expect(asyncFrames(stdout)).toEqual(["at async fn2", "at async fn1", "at async main"]); | ||
| }); | ||
|
|
||
| test("async stack frames under AsyncLocalStorage.enterWith()", async () => { | ||
| const stdout = await run(` | ||
| const { AsyncLocalStorage } = require("node:async_hooks"); | ||
| const als = new AsyncLocalStorage(); | ||
| als.enterWith({ x: 1 }); | ||
| async function inner() { await 0; console.log(new Error("boom").stack); } | ||
| async function outer() { await inner(); } | ||
| async function main() { await outer(); } | ||
| main(); | ||
| `); | ||
| expect(stdout).toContain("at inner"); | ||
| expect(asyncFrames(stdout)).toEqual(["at async outer", "at async main"]); | ||
| }); | ||
|
|
||
| test("async stack frames through Promise.race under AsyncLocalStorage", async () => { | ||
| const stdout = await run(` | ||
| const { AsyncLocalStorage } = require("node:async_hooks"); | ||
| const als = new AsyncLocalStorage(); | ||
| async function leaf() { await 0; throw new Error("boom"); } | ||
| async function viaRace() { await Promise.race([leaf()]); } | ||
| async function caller() { await viaRace(); } | ||
| async function main() { | ||
| try { await als.run({}, () => caller()); } | ||
| catch (e) { console.log(e.stack); } | ||
| } | ||
| main(); | ||
| `); | ||
| expect(stdout).toContain("at leaf"); | ||
| expect(asyncFrames(stdout)).toEqual(["at async viaRace", "at async caller", "at async main"]); | ||
| }); | ||
|
|
||
| test("async stack frames without AsyncLocalStorage are not duplicated", async () => { | ||
| const stdout = await run(` | ||
| async function fn3() { await 0; throw new Error("boom"); } | ||
| async function fn2() { await fn3(); } | ||
| async function fn1() { await fn2(); } | ||
| async function main() { | ||
| try { await fn1(); } | ||
| catch (e) { console.log(e.stack); } | ||
| } | ||
| main(); | ||
| `); | ||
| expect(stdout).toContain("at fn3"); | ||
| expect(asyncFrames(stdout)).toEqual(["at async fn2", "at async fn1", "at async main"]); | ||
| }); | ||
|
robobun marked this conversation as resolved.
Outdated
|
||
|
|
||
| test("async stack frames through Promise.race without AsyncLocalStorage are not duplicated", async () => { | ||
| const stdout = await run(` | ||
| async function leaf() { await 0; throw new Error("boom"); } | ||
| async function viaRace() { await Promise.race([leaf()]); } | ||
| async function caller() { await viaRace(); } | ||
| async function main() { | ||
| try { await caller(); } | ||
| catch (e) { console.log(e.stack); } | ||
| } | ||
| main(); | ||
| `); | ||
| expect(stdout).toContain("at leaf"); | ||
| expect(asyncFrames(stdout)).toEqual(["at async viaRace", "at async caller", "at async main"]); | ||
| }); | ||
|
|
||
| test("async stack frames when AsyncLocalStorage is entered mid-chain", async () => { | ||
| // fn2 awaits fn3 before ALS is entered, so JSC's own walk finds fn2; fn1 | ||
| // awaits fn2 after enterWith, so JSC stops there and the hook must supply | ||
| // fn1/main without duplicating fn2. | ||
| const stdout = await run(` | ||
| const { AsyncLocalStorage } = require("node:async_hooks"); | ||
| const als = new AsyncLocalStorage(); | ||
| async function fn3() { await 0; throw new Error("boom"); } | ||
| async function fn2() { await fn3(); } | ||
| async function fn1() { | ||
| const p = fn2(); | ||
| als.enterWith({}); | ||
| await p; | ||
| } | ||
| async function main() { | ||
| try { await fn1(); } | ||
| catch (e) { console.log(e.stack); } | ||
| } | ||
| main(); | ||
| `); | ||
| expect(stdout).toContain("at fn3"); | ||
| expect(asyncFrames(stdout)).toEqual(["at async fn2", "at async fn1", "at async main"]); | ||
| }); | ||
| }); | ||
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.