Skip to content

stream: run a replaced write() in the flowing pipe - #66596

Merged
nodejs-github-bot merged 2 commits into
nodejs:mainfrom
nigrosimone:stream-pipe-replaced-write
Nov 23, 2026
Merged

nodejs-github-bot merged 2 commits into
nodejs:mainfrom
nigrosimone:stream-pipe-replaced-write

Conversation

@nigrosimone

Copy link
Copy Markdown
Contributor

pipe() calls dest.write(), but since #66182 a flowing pipe whose source pushes from _read() hands each chunk to dest._write() directly. A write() replaced on the destination, or overridden in a subclass, no longer runs.

Found on 26.11.1 with a framework whose response is a stream.Writable: compression middleware replaces res.write(), and a file piped to the response came out as a gzip body with uncompressed chunks in it (Z_DATA_ERROR on the client). 26.10.0 is fine.

Fix: take the fast path only when write() is Writable.prototype.write.

Test: test/parallel/test-stream-pipe-replaced-write.js, one write() replaced on the instance and one in a subclass. It fails on 26.11.1 (1 call to write() out of 64) and passes with the fix.

cc @anonrig

Disclosure: I used Opus 5.5 (Max) as coding assistant

pipe() calls dest.write(). The fast path of a flowing pipe hands the
chunks of a source that pushes from _read() to dest._write() directly,
so a write() replaced on the instance or overridden in a subclass no
longer runs. A response that is a Writable, wrapped by compression
middleware, sent a gzip body with uncompressed chunks in it.

Take the fast path only when write() is Writable.prototype.write.

Refs: nodejs#66182
Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams. labels Oct 8, 2026
@mcollina

mcollina commented Oct 8, 2026

Copy link
Copy Markdown
Member

@nigrosimone this seems good, why it's draft?

@nigrosimone

nigrosimone commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

@mcollina my local build is still running, I want to see the test pass there first. Ready in about an hour, or I can mark it ready now and let the CI run, if you prefer.

@nigrosimone
nigrosimone marked this pull request as ready for review October 8, 2026 08:53
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.71%. Comparing base (6d5e309) to head (b6195e4).
⚠️ Report is 744 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66596      +/-   ##
==========================================
+ Coverage   90.44%   90.71%   +0.27%     
==========================================
  Files         791      801      +10     
  Lines      276562   280799    +4237     
  Branches    53126    54199    +1073     
==========================================
+ Hits       250130   254729    +4599     
+ Misses      16846    16251     -595     
- Partials     9586     9819     +233     
Files with missing lines Coverage Δ
lib/internal/streams/writable.js 98.17% <100.00%> (+1.75%) ⬆️

... and 287 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

nigrosimone added a commit to nigrosimone/fulmine.js that referenced this pull request Oct 9, 2026
…eased

Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
@nigrosimone

Copy link
Copy Markdown
Contributor Author

@mcollina it is ready now, the test passes also in my local build. Could you take a look again?

Comment thread lib/internal/streams/writable.js Outdated
const kWriteFlowBlock = kObjectMode | kDestroyed | kErrored | kSync |
kEnding | kFinished | kWriting | kCorked | kBuffered | kEnded |
kNeedDrain | kWriteCb | kExpectWriteCb | kBufferProcessing |
kFinalCalled | kPrefinished | kOnFinished | kErrorEmitted;

function writeKnownBuffer(stream, chunk) {
const state = stream._writableState;
if (state == null || state.length !== 0 || !(chunk instanceof Buffer))
if (state == null || state.length !== 0 || !(chunk instanceof Buffer) ||
stream.write !== Writable.prototype.write)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One concern: the check compares against Writable.prototype.write as it is at call time, so patching the prototype itself is still bypassed:

const orig = Writable.prototype.write;

// This is how AMP tools usually hook streams
Writable.prototype.write = function(...args) {
  // instrumentation
  return orig.apply(this, args);
};

Could we compare against the original function in module scope instead?

function write(chunk, encoding, cb) { ... }
Writable.prototype.write = write;

stream.write !== write

otherwise, good fix!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Done by b6195e4

Signed-off-by: Nigro Simone <nigro.simone@gmail.com>

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina added request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. commit-queue PRs queued for automated landing through the Commit Queue. labels Nov 19, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 19, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot nodejs-github-bot added commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. and removed commit-queue PRs queued for automated landing through the Commit Queue. labels Nov 19, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Commit Queue failed

   ✖  GitHub CI is still running
   ✖  Last Jenkins CI still running

The pull request was removed from the Commit Queue and labeled commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. . After resolving the failure, remove that label and add commit-queue PRs queued for automated landing through the Commit Queue. to retry.

Full Commit Queue output
�[36m⠋�[39m Loading data for nodejs/node/pull/66596
�[36m⠋�[39m Loading data for nodejs/node/pull/66596
�[36m⠋�[39m Getting collaborator contacts from README of nodejs/node
�[36m⠋�[39m Getting PR from nodejs/node/pull/66596
�[36m⠋�[39m Getting reviews from nodejs/node/pull/66596
�[36m⠋�[39m Getting comments from nodejs/node/pull/66596
�[36m⠋�[39m Getting commits from nodejs/node/pull/66596
✔  Done loading data for nodejs/node/pull/66596
----------------------------------- PR info ------------------------------------
Title      stream: run a replaced write() in the flowing pipe (#66596)
Author     Nigro Simone <nigro.simone@gmail.com> (@nigrosimone)
Branch     nigrosimone:stream-pipe-replaced-write -> nodejs:main
Labels     stream, needs-ci, commit-queue
Commits    2
 - stream: run a replaced write() in the flowing pipe
 - stream: compare with the original write() in the pipe fast path
Committers 1
 - Nigro Simone <nigro.simone@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66596
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/66596
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Thu, 08 Oct 2026 08:16:30 GMT
   ✔  Approvals: 1
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/66596#pullrequestreview-5971350064
   ✖  GitHub CI is still running
   ℹ  Last Full PR CI on 2026-11-19T08:17:21Z: https://ci.nodejs.org/job/node-test-pull-request/79293/
�[36m⠙�[39m Querying data for job/node-test-pull-request/79293/
�[36m⠙�[39m Querying data for job/node-test-pull-request/79293/
�[36m⠙�[39m Querying API for job/node-test-pull-request/79293/
✔  Build data downloaded
   ✖  Last Jenkins CI still running
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu

View workflow run

@jakecastelli jakecastelli added commit-queue-squash PRs the Commit Queue should land as one squashed commit. and removed commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. labels Nov 19, 2026
@nigrosimone

Copy link
Copy Markdown
Contributor Author

@mcollina the commit queue failed only because CI was still running, and the label was removed. Could you add commit-queue again?

@mcollina mcollina added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 23, 2026
@nodejs-github-bot
nodejs-github-bot merged commit d32f1bd into nodejs:main Nov 23, 2026
76 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in d32f1bd

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 23, 2026
aduh95 pushed a commit that referenced this pull request Nov 23, 2026
pipe() calls dest.write(). The fast path of a flowing pipe hands the
chunks of a source that pushes from _read() to dest._write() directly,
so a write() replaced on the instance or overridden in a subclass no
longer runs. A response that is a Writable, wrapped by compression
middleware, sent a gzip body with uncompressed chunks in it.

Take the fast path only when write() is Writable.prototype.write.

Refs: #66182
Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
PR-URL: #66596
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

commit-queue-squash PRs the Commit Queue should land as one squashed commit. needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants