Skip to content

piping to a stream with highWaterMark: 0 and objectMode: false is broken since node v20.10.0 #51930

Description

@dy-dx

Version

v21.6.2

Platform

Darwin x86_64

Subsystem

stream

What steps will reproduce the bug?

const { Readable, Writable } = require('stream');

const r = new Readable({
  read() {}
});

const w = new Writable({
  highWaterMark: 0,
  write(chunk, encoding, callback) {
    console.log(chunk.toString());
    callback();
  },
});

r.pipe(w);

r.push('hello');
r.push('world');
r.push(null);

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

Always

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

In node v20.9.0 and earlier:

hello
world

What do you see instead?

In node v20.10.0 and later, only the first chunk ever arrives at the destination:

hello

Additional information

The issue is present on node v20.10.0, but is not present on v20.9.0. The issue is also present on v21.0.0.

A workaround is to use highWaterMark: 1.

Activity

  1. dy-dx commented on Feb 29, 2024

    @dy-dx
    Author

    After building nodejs locally, I can confirm this change caused the issue: #50014

  2. added
    streamIssues and PRs related to Node.js streams.
    on Mar 2, 2024
  3. IlyasShabi commented on Mar 4, 2024

    @IlyasShabi
    Member

    I will investigate this later today

  4. changed the title [-]piping to a stream with `highWaterMark: 0` and `objectMode: false` does not work as expected, since node v20.10.0[/-] [+]piping to a stream with `highWaterMark: 0` and `objectMode: false` is broken since node v20.10.0[/+] on Mar 12, 2024
  5. added a commit that references this issue on Apr 28, 2024
  6. orgads commented on May 27, 2024

    @orgads
    Contributor

    @IlyasShabi any progress with this? We hit a similar issue with a custom Writable that calls its callback with an Error.

  7. ronag commented on May 29, 2024

    @ronag
    Member

    Here is a possible fix if someone wants to take a look at this:

    diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js
    index 0dbf56d7a69..848247c96e5 100644
    --- a/lib/internal/streams/writable.js
    +++ b/lib/internal/streams/writable.js
    @@ -565,7 +565,7 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
         state[kState] &= ~kSync;
       }
     
    -  const ret = state.length < state.highWaterMark;
    +  const ret = state.length === 0 || state.length < state.highWaterMark;
     
       if (!ret) {
         state[kState] |= kNeedDrain;
  8. ronag commented on May 29, 2024

    @ronag
    Member

    In general I would say that a highWaterMark of 0 doesn't make much sense...

  9. jakecastelli commented on Jun 2, 2024

    @jakecastelli
    Member

    I will take a look at this 👀

  10. jakecastelli commented on Jun 17, 2024

    @jakecastelli
    Member

    In general I would say that a highWaterMark of 0 doesn't make much sense...

    I have linked the spec related discussion here just for visibility and potentially future discussion, thanks @vweevers

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

    streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions