Skip to content

stream: prevent pipeline() callback after sync throw - #65165

Closed
pacocartones wants to merge 1 commit into
nodejs:mainfrom
pacocartones:fix/pipeline-sync-throw-double-report
Closed

stream: prevent pipeline() callback after sync throw#65165
pacocartones wants to merge 1 commit into
nodejs:mainfrom
pacocartones:fix/pipeline-sync-throw-double-report

Conversation

@pacocartones

Copy link
Copy Markdown

Fixes: #65127

Summary

When pipeline() throws synchronously while wiring streams together β€” for
example, when a middle stage returns an invalid value (ERR_INVALID_RETURN_VALUE)
β€” the stages already wired have already incremented finishCount. When those
stages 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)

const { pipeline, Readable, Transform } = require('node:stream');
const r = Readable.from(['a']);
const t = new Transform({ transform(c, e, cb) { cb(null, c); } });
try {
  pipeline(r, t, () => 42, (err) => console.log('callback:', err ?? 'NO ERROR'));
} catch (err) {
  console.log('threw:', err.code);
}

Before: threw: ERR_INVALID_RETURN_VALUE and callback: NO ERROR (double report).
After: threw: ERR_INVALID_RETURN_VALUE only; the callback is never invoked.

Verification

  • Regression test added to test/parallel/test-stream-pipeline.js:
    • fails on the unfixed code (common.mustNotCall() gets invoked with success),
    • passes with the fix.
  • Behavior of normal success, stream-error propagation, generator sources and
    abort paths verified unchanged against the pristine implementation.
Checklist

@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 the stream subsystem. labels Aug 9, 2026
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>
@pacocartones
pacocartones force-pushed the fix/pipeline-sync-throw-double-report branch from 88532ab to 4c49ce4 Compare August 9, 2026 17:17
MILLERMARRU

This comment was marked as low quality.

@pacocartones

Copy link
Copy Markdown
Author

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 destroys, dispose the abort listener, ac.abort(), rethrow. @ronag requested changes on it with:

I would say that ownership is not taken until pipeline succeeds...

and CI then confirmed it concretely. @shani-singh1's follow-up:

That line is assert.strictEqual(s.destroyed, false) inside the ERR_INVALID_RETURN_VALUE block, so "ownership is not taken until pipeline succeeds" is already asserted behaviour and my patch was contradicting it.

So the proactive-destroy behaviour you liked here is the part that's actually wrong. There are four blocks in test/parallel/test-stream-pipeline.js (~L871-916) that assert it explicitly, e.g. L890:

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, s is already in destroys, and my catch block runs destroys.shift()(err) before rethrowing. destroy() sets .destroyed synchronously, so s.destroyed is true by the time the validator runs. I checked on v24.14.1 that main gives false there today, so this PR would turn three of those four assertions red. CI hasn't caught it because every workflow run on this PR is still action_required β€” nothing has actually run.

Two more things worth putting on the record:

  • The abort-listener half of my catch block (disposable?.[SymbolDispose]()) is legitimate, but it's not mine to add: that's exactly what stream: clean up when pipeline throws synchronouslyΒ #65064 was narrowed down to, and it's already approved.
  • Where the two differ on callback suppression, lazerg's is the better shape. My syncThrow guard returns at the top of finishImpl, which also skips lastStreamCleanup.forEach(...) and leaves the onError listeners pipeline() attached in place. The wired gate sits directly on process.nextTick(callback, error, value), so that cleanup still runs.

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 β€” wired = true will need to move after the try/catch, depending on merge order.

@nodejs/streams β€” could someone confirm that split? And #65128 is still carrying needs-ci with no Jenkins run; a CI start there would be more useful than one here.

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stream.pipeline() can both throw synchronously and invoke the callback reporting success

3 participants