stream: prevent pipeline() callback after sync throw - #65165
Conversation
|
Review requested:
|
When a stage in the middle of pipeline() throws synchronously (e.g. an invalid return value), the stages already wired have already incremented finishCount. When those stages later complete, their finish callbacks invoke the completion callback with no error β double-reporting the failure that the caller already received as an exception. Track synchronous throws while wiring, destroy the stages wired so far, and skip the completion callback in that case. Fixes: nodejs#65127 Signed-off-by: pacocartones <pacocartones@users.noreply.github.com>
88532ab to
4c49ce4
Compare
|
Thanks for the careful trace on both PRs β you're right that these need reconciling, and having looked into it properly, I think the answer is that this one should be closed in favour of #65128. Here's what I found after digging into the history, because it changes the picture: There's a third PR against this code path, #65064 by @shani-singh1 (opened 2026-08-06, before both of these). Its original version did essentially what this PR does: wrap the wiring loop in try/catch, drain
and CI then confirmed it concretely. @shani-singh1's follow-up:
So the proactive-destroy behaviour you liked here is the part that's actually wrong. There are four blocks in const s = new PassThrough();
assert.throws(() => {
pipeline(s, function(source) {
}, s, () => {});
}, (err) => {
assert.strictEqual(err.code, 'ERR_INVALID_RETURN_VALUE');
assert.strictEqual(s.destroyed, false); // L890
return true;
});At the point that throws, Two more things worth putting on the record:
So the split I'd propose is: #65128 fixes #65127 (the double report), #65064 fixes #65063 (the abort-listener leak), and this PR gets closed. The only thing to watch is that they'll conflict textually at the end of the wiring loop β @nodejs/streams β could someone confirm that split? And #65128 is still carrying @lazerg sorry for the duplicate work β I opened this without spotting #65064 and the ownership discussion on it, which would have saved us both some time. Closing this one. |
Fixes: #65127
Summary
When
pipeline()throws synchronously while wiring streams together β forexample, when a middle stage returns an invalid value (
ERR_INVALID_RETURN_VALUE)β the stages already wired have already incremented
finishCount. When thosestages complete afterwards, their finish callbacks invoke the completion
callback with no error, double-reporting the failure that the caller
already received as a synchronous exception.
This change tracks synchronous throws while wiring, destroys the stages wired
so far, and skips the completion callback in that case.
Repro (issue case A)
Before:
threw: ERR_INVALID_RETURN_VALUEandcallback: NO ERROR(double report).After:
threw: ERR_INVALID_RETURN_VALUEonly; the callback is never invoked.Verification
test/parallel/test-stream-pipeline.js:common.mustNotCall()gets invoked with success),abort paths verified unchanged against the pristine implementation.
Checklist