Skip to content

Async stack traces not produced within AsyncLocalStorage #24003

Description

@dodas

What version of Bun is running?

1.3.1+89fa0f343

What platform is your computer?

Darwin 24.6.0 arm64 arm

What steps can reproduce the bug?

Run this script:

import { AsyncLocalStorage } from "node:async_hooks";

const asyncLocalStorage = new AsyncLocalStorage();

async function fn1() {
  await Bun.sleep(10);
  await fn2();
  return "ok";
}

async function fn2() {
  await Bun.sleep(10);
  await fn3();
  return "ok";
}

async function fn3() {
  await Bun.sleep(10);
  throw new Error("Some error occurred in fn3");
}

async function main() {
  try {
    await fn1();
  } catch (error) {
    console.error("Caught error outside of async context", error);
  }

  try {
    await asyncLocalStorage.run(null, async () => {
      await fn1();
    });
  } catch (error) {
    console.error("Caught error inside of async context", error);
  }
}

main();

What is the expected behavior?

There should be full stack trace visible for both invocations of fn1().

What do you see instead?

Error from invocation wrapped in an AsyncLocalStorage doesn't include stack trace across async boundaries:

Caught error outside of async context 14 |   return "ok";
15 | }
16 | 
17 | async function fn3() {
18 |   await Bun.sleep(10);
19 |   throw new Error("Some error occurred in fn3");
                 ^
error: Some error occurred in fn3
      at fn3 (~/Desktop/test/stack.ts:19:13)
      at async fn2 (~/Desktop/test/stack.ts:13:9)
      at async fn1 (~/Desktop/test/stack.ts:7:9)
      at async main (~/Desktop/test/stack.ts:24:11)

Caught error inside of async context 14 |   return "ok";
15 | }
16 | 
17 | async function fn3() {
18 |   await Bun.sleep(10);
19 |   throw new Error("Some error occurred in fn3");
                 ^
error: Some error occurred in fn3
      at fn3 (~/Desktop/test/stack.ts:19:13)

Additional information

Related: #2704

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingruntime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions