stream: emit 'pause' on unpipe

unpipe should use pause() instead of mutating
state.flowing directly so that pausing side
effects such as emitting 'pause' are properly
performed.

Fixes: https://github.com/nodejs/node/issues/32470

PR-URL: https://github.com/nodejs/node/pull/32476
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Robert Nagy 2020-03-25 00:43:18 +01:00 committed by Myles Borins
parent b2e1a01516
commit 0d0f151a46
No known key found for this signature in database
GPG Key ID: 933B01F40B5CA946
2 changed files with 12 additions and 2 deletions

View File

@ -819,7 +819,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 });
@ -833,7 +833,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);

View File

@ -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);
}));
}