Skip to content

.pipeTo(Writable.toWeb(process.stdout)) returns a never-settling Promise #56139

Description

@mikaelkaron

Version

  • v22.9.0
  • v22.12.0
  • v23.3.0

Platform

Linux a54ff73afbfe 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 GNU/Linux
Linux SURFACE9PRO 5.15.153.1-microsoft-standard-WSL2 #1 SMP Fri Mar 29 23:14:13 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

node:stream

What steps will reproduce the bug?

create file test.mjs

import { Readable, Writable } from 'node:stream'

await Readable.toWeb(process.stdin).pipeTo(Writable.toWeb(process.stdout))

run

echo test | node test.mjs 
test
Warning: Detected unsettled top-level await at file:///workspace/test.mjs:2
await Readable.toWeb(process.stdin).pipeTo(Writable.toWeb(process.stdout))
^

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

always

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

no warning, await to work as expected

What do you see instead?

warning about unsettled top-level await is shown

Additional information

No response

Activity

  1. mikaelkaron commented on Dec 5, 2024

    @mikaelkaron
    Author

    Some (small followups):

    Simplified to get rid of Readable.toWeb

    import { Writable } from 'node:stream'
    import { ReadableStream } from 'node:stream/web'
    
    await ReadableStream.from(["test"]).pipeTo(Writable.toWeb(process.stdout))
    node test.mjs 
    testWarning: Detected unsettled top-level await at file:///workspace/test.mjs:4
    await ReadableStream.from(["test"]).pipeTo(Writable.toWeb(process.stdout))
    ^
    

    Without Writable.toWeb there's no error:

    import { ReadableStream } from 'node:stream/web'
    
    await ReadableStream.from(["test"]).pipeTo(new WritableStream())

    And just to make sure let's implement a naive implementation of a WritableStream

    import { ReadableStream } from 'node:stream/web'
    import { setTimeout } from 'node:timers/promises'
    
    const streamWritable = process.stdout
    
    await ReadableStream.from(["test"]).pipeTo(new WritableStream({
      write(chunk) {
        if (streamWritable.writableNeedDrain || !streamWritable.write(chunk)) {
          return new Promise(resolve => streamWritable.once('drain', resolve))
        }
      },
      close() {
        return setTimeout(1000)
      }
    }))
  2. aduh95 commented on Dec 5, 2024

    @aduh95
    Contributor

    The problem is not that there's a warning, it's that the promise never resolves:

    $ node -e '
    const {Writable} = require("node:stream");
    const {ReadableStream} = require("node:stream/web");
    ReadableStream.from(["test\n"]).pipeTo(Writable.toWeb(process.stdout)).then(() => console.log("Done"), console.error)'
    test
    $ echo $?
    0

    As you can see, the console.log("Done") is never reached. The warning is a mere consequence of awaiting a never-settling promise on the top-level scope.

  3. changed the title [-]awaiting `.pipeTo(Writable.toWeb(process.stdout))` results in warning[/-] [+]`.pipeTo(Writable.toWeb(process.stdout))` returns a never-settling Promise[/+] on Dec 5, 2024
  4. mikaelkaron commented on Dec 7, 2024

    @mikaelkaron
    Author

    The problem is not that there's a warning, it's that the promise never resolves:

    Indeed. I had a few extra minutes to do some initial testing. I looks like when the code gets here closed is undefined which returns a promise here.

    I'm guessing closed is supposed to be handled in cleanup but when I debug I never get there which leads me to suspect that eos is not running the supplied callback for some reason.

    So that was me not setting up my debugging env propperly, the callback is executed, so I'll have to dig some more.

  5. mikaelkaron commented on Dec 8, 2024

    @mikaelkaron
    Author

    I decided to go in a slightly different direction and started testing with different node version, and it look like this works in v21.7.3, but is broken in v22.0.0. Also tested in in v23.3.0 where it's also broken.

  6. aduh95 commented on Dec 9, 2024

    @aduh95
    Contributor

    It could be related to a V8 update, or another semver-major commit of v22.x but I don't see any related

  7. jakecastelli commented on Dec 9, 2024

    @jakecastelli
    Member

    The reason you won't see the warning in v21.7.3 but see in v22.0.0 is because since v22.0.0 it starts to detect unsettled top-level await - see this commit - more detail please take a look at this PR #51999.

    I have tested v21.7.3 locally and the issue described by @aduh95 (here) still exists.

  8. mikaelkaron commented on Dec 9, 2024

    @mikaelkaron
    Author

    Oh well, I guess it's nice to have found something new rather than a regression 🤷

  9. 029A-h commented on Apr 17, 2025

    @029A-h

    The issue persists in node 23.11.0

  10. 029A-h commented on Apr 17, 2025

    @029A-h

    I found a workaround which prevents throwing Warning: Detected unsettled top-level await.

    await ReadableStream.from(['test']).pipeTo(Writable.toWeb(process.stdout), { preventClose: true })
    
  11. github-actions commented on Apr 26, 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 Apr 26, 2026
  13. github-actions commented on May 26, 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.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