Skip to content

stream/web internal function correct implementation leads to debuggers incorrectly detecting an unhandled rejection #51093

Description

@r-cyr

Version

v20.10.0, v22.3.0

Platform

MacOS/WSL

Subsystem

No response

What steps will reproduce the bug?

Run the following code into a debugger (Inside VSCode or in Chrome) with Pause on uncaught exceptions checked/enabled:

import { ReadableStream, WritableStream } from "stream/web";

await new ReadableStream({
  async start(controller) {
    controller.enqueue('Hello world');
    controller.close();
  },
}).pipeTo(
  new WritableStream({
    write(elem) {
      console.log(elem);
    },
  })
);

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

Always

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

When run in a debugger with Pause on uncaught exceptions checked, program should print "Hello world" then quit.

What do you see instead?

The program will execute as expected then when the stream gets destroyed at the end, the debugger will pause after a second and show the following error as an unhandled rejection:

TypeError [ERR_INVALID_STATE]: Invalid state: Writer has been released

The program runs as expected if Pause on uncaught exceptions is unchecked/disabled.

Additional information

The call stack shows that the error comes from https://github.com/nodejs/node/blob/main/lib/internal/webstreams/writablestream.js#L1033 but we can see that setPromiseHandled gets called a few lines after so I don't believe that error to be correct. While looking for possible reasons, I ended up finding this which may be the root cause

I also tried listening to process events 'uncaughtException' and 'unhandledrejection' but those were never triggered.

If it's really caused by the debugger, then I don't know if it's possible to refactor the function in a way that would make debuggers happy... (and me too since we started using webstreams a lot at work... and getting that error all the time when debugging is painful)

Worth noting that the function below it, named writableStreamDefaultWriterEnsureClosedPromiseRejected is written similarly to writableStreamDefaultWriterEnsureReadyPromiseRejected and could, maybe, produce the same issue. (I didn't try to trigger it)

Activity

  1. added
    web streamsIssues and PRs related to the Web Streams API.
    debuggerIssues and PRs related to the Node.js command-line debugger.
    on Dec 8, 2023
  2. r-cyr commented on Dec 8, 2023

    @r-cyr
    Author

    I decided to dive in the code and found that if I replace the PromiseReject implementation from the default one in primordials to the following hack (shown in the chromium bug tracker link in my previous post), then it works as expected. I don't know anything about the NodeJS codebase so I have no idea if using the hack has possible unintended side-effects.

    The hack being simply:

    function PromiseReject(e) {
      try {
        return primordials.PromiseReject(e);
      } catch {
        // Empty on purpose
      }
    }
  3. ljharb commented on Dec 8, 2023

    @ljharb
    SponsorMember

    By spec, Promise.reject should never, ever, ever synchronously throw under any circumstances, and that hack implies it does, so that sounds like a pretty bad scenario; https://bugs.chromium.org/p/chromium/issues/detail?id=465666&q=uncaught%20promise%20pause&can=2 is closed as wontfix. I do see that it's just about doing "catch detection", so it's not that it actually throws.

    Another alternative could be async function PromiseReject(e) { throw e; } but i think that'd mess with node's callsite detection; and another might be creating the rejected promise in C++ so that the callsite info is preserved?

  4. rotu commented on May 14, 2024

    @rotu

    This is an incredibly annoying error.
    I'm managing to work around it by adding this condition to the uncaught exception breakpoint:

    !error.stack.match('node:internal/webstreams/writablestream:101') &&
    !error.stack.match('node:internal/webstreams/adapters:110') &&
    !error.stack.match('node:internal/webstreams/readablestream:158')

    image

    Are there any better userland workarounds?

  5. rotu commented on May 14, 2024

    @rotu

    The hack being simply:

    function PromiseReject(e) {
      try {
        return primordials.PromiseReject(e);
      } catch {
        // Empty on purpose
      }
    }

    By spec, Promise.reject should never, ever, ever synchronously throw under any circumstances, and that hack implies it does, so that sounds like a pretty bad scenario; https://bugs.chromium.org/p/chromium/issues/detail?id=465666&q=uncaught%20promise%20pause&can=2 is closed as wontfix. I do see that it's just about doing "catch detection", so it's not that it actually throws.

    It looks like this hack "worked" because of this catch detection issue in particular (now fixed):
    https://issues.chromium.org/issues/40283987

  6. r-cyr commented on Jun 19, 2024

    @r-cyr
    Author

    According to @rotu comment above, the problem has been fixed in V8 version 12.3. I'm going to close this issue as the "fix" is to upgrade to Node 22 (with V8 12.4) which is scheduled to become the next LTS version this October
    Misunderstood what the fix was doing, the original problem is unfortunately, not fixed. (Reopening)

  7. rotu commented on Jun 19, 2024

    @rotu

    @r-cyr sorry I was not clear. The catch{…} should no longer affect debugger behavior. But I don’t know if the actual issue (the debugger spuriously thinking a rejection is unhandled) is fixed.

  8. reopened this on Jun 19, 2024
  9. rotu commented on Jun 19, 2024

    @rotu

    I just retried your original repro code.

    It is NOT treated as an uncaught exception by the VSCode debugger (VSCode 1.91.0-insider). 🎉
    It is still show a problem in Chrome (Version 128.0.6544.0 (Official Build) canary (64-bit). 😞

  10. avivkeller commented on Jul 11, 2024

    @avivkeller
    Member

    May be fixed by #53800

  11. avivkeller commented on Jul 12, 2024

    @avivkeller
    Member

    May be fixed by #53800

    Closing, as that PR has landed. If you disagree, feel free to reopen.

  12. rotu commented on Jul 18, 2024

    @rotu

    I still see "uncaught" rejections from writableStreamDefaultWriterEnsureReadyPromiseRejected when closing a TCP socket with wrapped in stream.Duplex.toWeb

    promise: PromiseReject(error),

    Any reason this wasn't included in #53800?

  13. LeonardoDaLuz commented on Jan 31, 2025

    @LeonardoDaLuz

    Still happening in version 22.13.1

  14. rotu commented on Jul 2, 2025

    @rotu

    @avivkeller Could you please reopen? This appears to still be an issue.

  15. avivkeller commented on Jul 2, 2025

    @avivkeller
    Member

    Unfortunately, I'm no longer a member of the team that oversees triage in this repository, cc @nodejs/issue-triage.

  16. github-actions commented on May 25, 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.

  17. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 25, 2026
  18. github-actions commented on Jun 28, 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

    debuggerIssues and PRs related to the Node.js command-line debugger.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.web streamsIssues and PRs related to the Web Streams API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions