镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Promise rejection in timeout (versus module-level) treated as unhandled by debugger #53732

Description

@rotu

Version

v22.4.0

Platform

Darwin Hypothesis.local 24.0.0 Darwin Kernel Version 24.0.0: Thu Jun 20 20:36:19 PDT 2024; root:xnu-11215.0.115.501.3~1/RELEASE_ARM64_T8103 arm64

Subsystem

No response

What steps will reproduce the bug?

Create this script:

setTimeout(() => {
  Promise.reject().catch(() => { })
}, 1)

And run it in the debugger with break on Uncaught enabled.

How often does it reproduce? Is there a required condition?

Every time. Note this does not happen if Promise.reject() is at top level instead of wrapped in setTimeout().

What is the expected behavior? Why is that the expected behavior?

It is expected that the debugger does (1) not pause on such a promise or (2) only pauses on such a promise if breakOnException is enabled.

The HTML spec guarantees that promise rejections are not considered unhandled if a handler is then synchronously attached.

What do you see instead?

Node breaks synchronously on the creation of the rejected promise. Similar issues happen when using the VSCode debugger and Chrome debugger.

node inspect promisetest.js
< Debugger listening on ws://127.0.0.1:9229/d18cff1f-6886-4ff6-9a4f-404128bd150a
< For help, see: https://nodejs.org/en/docs/inspector
< 
< Debugger attached.
< 
 ok
Break on start in promisetest.js:1
> 1 setTimeout(() => {
  2   Promise.reject().catch(() => { })
  3 }, 1)
debug> breakOnUncaught
debug> c
promiseRejection in promisetest.js:2
  1 setTimeout(() => {
> 2   Promise.reject().catch(() => { })
  3 }, 1)
  4 

Additional information

A downstream issue where this interferes with usage of the web streams API: #51093

I too have been incredibly confused by this, as it makes it seem like even correct usage of promises is developer error.

VSCode reports this as "Exception has occurred" instead of "promiseRejection".

Activity

  1. added
    promisesIssues and PRs related to ECMAScript promises.
    debuggerIssues and PRs related to the Node.js command-line debugger.
    on Jul 4, 2024
  2. avivkeller commented on Jul 4, 2024

    @avivkeller
    Member

    It's reproducible via the VSCode debugger.
    image

    The debugger treats it as both a caught exception, and an uncaught exception.


    It appears to be a debugger specific issue, as it does not emit an uncaughtException (or unhandledRejection) event on the process:

    process.on("uncaughtException", () => console.log("uncaughtException")) // This is never called
    process.on("unhandledRejection", () => console.log("unhandledRejection")) // This is never called
    
    setTimeout(() => {
        Promise
            .reject("This is a promise rejection")
            .catch(() => console.log("Caught promise rejection")) // This is called
    }, 1)
  3. rotu commented on Jul 4, 2024

    @rotu
    Author

    It appears to be a debugger specific issue, as it does not emit an uncaughtException (or unhandledRejection) event on the process:

    Yes, it is a debugger-specific issue caused by V8 "catch prediction". The debugger breaks synchronously when the promise is rejected.

    It would be okay if the debugger paused here when breakOnException was specified, but it's inappropriate for breakOnUncaught.

    Related discussion:
    https://issues.chromium.org/issues/41161875

  4. avivkeller commented on Jul 4, 2024

    @avivkeller
    Member

    @nodejs/v8

  5. rotu commented on Jul 5, 2024

    @rotu
    Author

    Relatedly, the below code does not trip the debugger (with breakOnUncaught enabled) and does not print anything. I think this situation is dual to the originally reported issue:

    • the rejected promise never has a handler attached
    • but catch prediction incorrectly expects the surrounding try statement to apply to the rejection (it doesn't because the promise is never awaited).
    setTimeout(async () => {
      try {
        Promise.reject()
      } catch (e) { }
    })
  6. rotu commented on Jul 22, 2024

    @rotu
    Author

    I think I figured out the setTimeout part of this mystery. When running a module as the main entry point, the code is within in a try block in an async function (in module_job.js).

    async run(isEntryPoint = false) {
    await this.instantiate();
    if (isEntryPoint) {
    globalThis[entry_point_module_private_symbol] = this.module;
    }
    const timeout = -1;
    const breakOnSigint = false;
    setHasStartedUserESMExecution();
    try {
    await this.module.evaluate(timeout, breakOnSigint);
    } catch (e) {

    Because of this try/catch block, NO promise rejection at module level is regarded by the inspector as an "uncaught exception", REGARDLESS OF WHETHER OR NOT IT HAS A HANDLER ATTACHED.

    I raised an issue for this catch prediction false positive but it was regarded as "infeasible" here: https://issues.chromium.org/issues/352455689

    When code escapes this call stack by running asynchronously, the inspector no longer regards it as handled by that try/catch so it may or may not be considered an "uncaught exception" (based on V8's designed behavior).

    Here's a demonstration:

    // assume this is in a .mjs file
    
    // this function creates a Promise which is rejected and handled
    // This promise is NOT regarded as "caught" by the V8's catch prediction
    const makeRejectedPromise = (reason) => {
       Promise.reject(reason).catch(() => {});
    };
    
    // rejection regarded as handled created synchronously:
    makeRejectedPromise(1);
    (async () => {
       makeRejectedPromise(2);
    })();
    await null;
    // or asynchronously after a module-level await
    makeRejectedPromise(3);
    
    // rejection regarded as unhandled when created asynchronously:
    (async () => {
       await null;
       makeRejectedPromise(4);
    })();
    queueMicrotask(() => makeRejectedPromise(5));
    process.nextTick(() => makeRejectedPromise(6));
    setTimeout(() => makeRejectedPromise(7));
  7. changed the title [-]Promise rejection created in timeout treated as unhandled by debugger[/-] [+]Promise rejection created in timeout treated as unhandled by debugger (versus module-level)[/+] on Jul 22, 2024
  8. changed the title [-]Promise rejection created in timeout treated as unhandled by debugger (versus module-level)[/-] [+]Promise rejection in timeout (versus module-level) treated as unhandled by debugger[/+] on Jul 22, 2024
  9. rotu commented on Jan 22, 2025

    @rotu
    Author

    This appears to be fixed in Node 23.0.0.
    The converse example given above still seems to be a problem: #53732 (comment)

  10. kapouer commented on May 9, 2025

    @kapouer
    Contributor

    This also stops the inspector with unhandled rejection with node 20.19.0

    
    async function test() {
    	await Promise.race([
    		Promise.resolve().then(() => {
    			return Promise.reject("error");
    		})
    	]);
    }
    
    try {
    	await test();
    } catch (err) {
    	console.error("caught", err);
    }
    
  11. github-actions commented on May 4, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  12. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 4, 2026
  13. rotu commented on May 4, 2026

    @rotu
    Author

    I don't think this is stale

  14. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 5, 2026
  15. github-actions commented on Aug 3, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 90 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  16. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Aug 3, 2026
  17. rotu commented on Aug 3, 2026

    @rotu
    Author

    not stale

  18. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    debuggerIssues and PRs related to the Node.js command-line debugger.promisesIssues and PRs related to ECMAScript promises.v8 engineIssues and PRs related to the V8 dependency.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions