Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dy-dx opened this issue Feb 29, 2024 · 6 comments · Fixed by #53261
Closed
Labels
stream Issues and PRs related to the stream subsystem.

Comments

@dy-dx
Copy link

dy-dx commented Feb 29, 2024

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.

@dy-dx
Copy link
Author

dy-dx commented Feb 29, 2024

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

@lpinca lpinca added the stream Issues and PRs related to the stream subsystem. label Mar 2, 2024
@IlyasShabi
Copy link
Contributor

I will investigate this later today

@dy-dx dy-dx 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 Mar 12, 2024
james58899 added a commit to james58899/MusicBot that referenced this issue Apr 28, 2024
@orgads
Copy link
Contributor

orgads commented May 27, 2024

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

@ronag
Copy link
Member

ronag commented May 29, 2024

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;

@ronag
Copy link
Member

ronag commented May 29, 2024

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

@jakecastelli
Copy link
Contributor

I will take a look at this 👀

nodejs-github-bot pushed a commit that referenced this issue Jun 8, 2024
Co-authored-by: Robert Nagy <ronagy@icloud.com>
PR-URL: #53261
Fixes: #51930
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants