Skip to content

lib: callbacks firing twice if first attempt throws #58419

Description

@LiviaMedeiros

There are a few places where code flows like this:

try {
  // some code that produces `result` and can potentially throw
  callback(null, result); // no error, return result
} catch(error) {
  callback(error);
}

These are usually (if not always) bugs, because it wraps the callback call itself in the try block, and if that callback throws, it gets caught and called again, but this time with its own error.

For example:

Some of these bugs might be a good first issue Issues that are suitable for first-time contributors. candidates.

Activity

  1. bnoordhuis commented on May 23, 2025

    @bnoordhuis
    Member

    What happens with the exception when it's no longer caught but bubbles upwards instead? It's important that it doesn't just disappear into the void.

  2. 0hmX commented on May 23, 2025

    @0hmX
    Contributor

    @bnoordhuis I don't think the errors will go unnoticed. I shared a piece of code included in the patch: https://github.com/0hmX/node/blob/2fdc8ee07ff51c532162bc34614251cbcbd1ee43/lib/internal/fs/dir.js#L148-L158

    We simply check if an error has already occurred; if so, we call the callback with the error; otherwise, we make a regular success callback.

  3. LiviaMedeiros commented on May 23, 2025

    @LiviaMedeiros
    MemberAuthor

    What happens with the exception when it's no longer caught but bubbles upwards instead?

    If an exception happens in the callback and is not caught inside the callback, it should bubble up and crash the program (unless something like process.on('uncaughtException') or even domain intercepts it), just like any other unhandled error.

  4. bnoordhuis commented on May 24, 2025

    @bnoordhuis
    Member

    I agree that's what should happen but it's important to check (not assume) it really happens.

  5. added a commit that references this issue on Jun 17, 2025
  6. added a commit that references this issue on Jun 23, 2025
  7. github-actions commented on Apr 19, 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.

  8. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 19, 2026
  9. github-actions commented on May 20, 2026

    @github-actions
    Contributor

    This issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
    If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.

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

    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions