Skip to content

Commit

Permalink
stream: emit 'pause' on unpipe
Browse files Browse the repository at this point in the history
unpipe should use pause() instead of mutating
state.flowing directly so that pausing side
effects such as emitting 'pause' are properly
performed.

Fixes: #32470

PR-URL: #32476
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
ronag authored and MylesBorins committed Mar 26, 2020
1 parent deab08b commit 56da8df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ Readable.prototype.unpipe = function(dest) {
// remove all.
var dests = state.pipes;
state.pipes = [];
state.flowing = false;
this.pause();

for (const dest of dests)
dest.emit('unpipe', this, { hasUnpiped: false });
Expand All @@ -832,7 +832,7 @@ Readable.prototype.unpipe = function(dest) {

state.pipes.splice(index, 1);
if (state.pipes.length === 0)
state.flowing = false;
this.pause();

dest.emit('unpipe', this, unpipeInfo);

Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-stream-pipe-unpipe-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ assert.strictEqual(source._readableState.pipes.length, 0);
checkDestCleanup(dest2);
source.unpipe();
}

{
const src = Readable({ read: () => {} });
const dst = Writable({ write: () => {} });
src.pipe(dst);
src.on('resume', common.mustCall(() => {
src.on('pause', common.mustCall());
src.unpipe(dst);
}));
}

0 comments on commit 56da8df

Please sign in to comment.