From 0d0f151a46b36483af15e355afc81d1693c98fb0 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 25 Mar 2020 00:43:18 +0100 Subject: [PATCH] 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 Reviewed-By: Matteo Collina --- lib/_stream_readable.js | 4 ++-- test/parallel/test-stream-pipe-unpipe-streams.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 1df50ba200fe0f..0d7724efc08f1a 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -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); diff --git a/test/parallel/test-stream-pipe-unpipe-streams.js b/test/parallel/test-stream-pipe-unpipe-streams.js index 4cb8413af22f18..b1a673d9450ce8 100644 --- a/test/parallel/test-stream-pipe-unpipe-streams.js +++ b/test/parallel/test-stream-pipe-unpipe-streams.js @@ -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); + })); +}