Skip to content
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

Some async code doesn't produce stack trace on error #2704

Open
dhbaird opened this issue Apr 20, 2023 · 4 comments
Open

Some async code doesn't produce stack trace on error #2704

dhbaird opened this issue Apr 20, 2023 · 4 comments
Labels
bug Something isn't working jsc Something related to JavaScriptCore

Comments

@dhbaird
Copy link

dhbaird commented Apr 20, 2023

What version of Bun is running?

0.5.9

What platform is your computer?

Linux

What steps can reproduce the bug?

async function aBroken() {
}

async function bBroken() {
    await aBroken();
    throw new Error("Oops"); // <-- no stack trace for this throw
}

async function cBroken() {
    await bBroken();
}

async function aOkay() {
}

async function bOkay() {
    throw new Error("Oops");
    await aOkay();
}

async function cOkay() {
    await bOkay();
}

async function main() {
    try {
        await cBroken();
    }
    catch (e) {
        console.log(e);

        // >>>(bun)
        // 1 | async function aBroken() {
        // 2 | }
        // 3 | 
        // 4 | async function bBroken() {
        // 5 |     await aBroken();
        // 6 |     throw new Error("Oops");
        //               ^
        // error: Oops
        //       at /.../throwy.ts:6:10

        // >>>(node)
        // Error: Oops
        //     at bBroken (/.../throwy.js:6:11)
        //     at async cBroken (/.../throwy.js:10:5)
        //     at async main (/.../throwy.js:27:9)
    }
    try {
        await cOkay();
    }
    catch (e) {
        console.log(e);
        // 
        // >>>(bun)
        // 12 | 
        // 13 | async function aOkay() {
        // 14 | }
        // 15 | 
        // 16 | async function bOkay() {
        // 17 |     throw new Error("Oops");
        //               ^
        // error: Oops
        //       at /.../throwy.ts:17:10
        //       at bOkay (/.../throwy.ts:16:20)
        //       at /.../throwy.ts:22:10
        //       at cOkay (/.../throwy.ts:21:20)
        //       at /.../throwy.ts:50:14

        // >>>(node)
        // Error: Oops
        //     at bOkay (/.../throwy.js:17:11)
        //     at cOkay (/.../throwy.js:22:11)
        //     at main (/.../throwy.js:50:15)
    }
}

main();

What is the expected behavior?

Expect stack trace for the first case.

What do you see instead?

Stack trace is missing.

        // error: Oops
        //       at /.../throwy.ts:6:10

Additional information

Thanks!! :) (Thought that maybe this should be a feature - but the bug reporting template was more useful, so I picked that.)

@dhbaird dhbaird added the bug Something isn't working label Apr 20, 2023
@dhbaird dhbaird changed the title Async code sometimes doesn't produce stack trace on error Some async code doesn't produce stack trace on error Apr 20, 2023
@ayonli
Copy link

ayonli commented Oct 20, 2023

It seems this is a JavaScriptCode bug, if the function is async, JSC cannot produce the full stack trace.

For example:

async function foo() {
    await Promise.resolve(null);
    throw new Error("something went wrong");
}

async function bar() {
    await foo();
}

(async () => {
    try {
        await bar();
    } catch (err) {
        console.log(err);
    }
})();

In Bun, it logs like this:

1 | async function foo() {
2 |     await Promise.resolve(null);
3 |     throw new Error("something went wrong");
              ^
error: something went wrong
      at /Users/ayon/Workspace/try-out/log.js:3:10

And in Safari (Console Snippets), it logs like this:

Error: something went wrong
(anonymous function)

The stack trace of the async functions are all lost.

@lsnow99
Copy link

lsnow99 commented Apr 1, 2024

It seems this is a JavaScriptCode bug, if the function is async, JSC cannot produce the full stack trace.

This is a significant annoyance for my application that runs using Bun. I assume this is a pretty deep issue seeing as it comes from JSC, but if someone could point towards a potential fix I'd like to help work on/support this feature. If it seems intractable due to structural reasons in JSC, that's also helpful to know and I can just workaround by adding more try/catches in userspace.

@AlbertMarashi
Copy link

+1 pretty annoying

@arthurvanl
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working jsc Something related to JavaScriptCore
Projects
None yet
Development

No branches or pull requests

6 participants