From 3500d5fa5aa62c98be752ba73bace814d889a029 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=88=9A?= <chengang07@meituan.com>
Date: Sat, 20 Jan 2018 11:06:26 +0800
Subject: [PATCH] stream: fix not calling cleanup() when unpiping all streams.

Refs: https://github.com/nodejs/node/pull/12746
---
 lib/_stream_readable.js                       |  2 +-
 .../test-stream-pipe-unpipe-streams.js        | 23 +++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js
index 9cf786a15be847..5761ce3a28615a 100644
--- a/lib/_stream_readable.js
+++ b/lib/_stream_readable.js
@@ -759,7 +759,7 @@ Readable.prototype.unpipe = function(dest) {
     state.flowing = false;
 
     for (var i = 0; i < len; i++)
-      dests[i].emit('unpipe', this, unpipeInfo);
+      dests[i].emit('unpipe', this, { hasUnpiped: false });
     return this;
   }
 
diff --git a/test/parallel/test-stream-pipe-unpipe-streams.js b/test/parallel/test-stream-pipe-unpipe-streams.js
index cd29b66365ae0f..431e34273e2b61 100644
--- a/test/parallel/test-stream-pipe-unpipe-streams.js
+++ b/test/parallel/test-stream-pipe-unpipe-streams.js
@@ -31,3 +31,26 @@ source.unpipe(dest2);
 source.unpipe(dest1);
 
 assert.strictEqual(source._readableState.pipes, null);
+
+{
+  // unpipe all
+  const source = Readable({ read: () => {} });
+  const dest1 = Writable({ write: () => {} });
+  const dest2 = Writable({ write: () => {} });
+
+  source.pipe(dest1);
+  source.pipe(dest2);
+
+  checkDestCleanup(dest1);
+  checkDestCleanup(dest2);
+
+  source.unpipe();
+
+  function checkDestCleanup(dest) {
+    const unpipeChecker = common.mustCall(() => {
+      dest.removeListener('unpipe', unpipeChecker);
+      assert.strictEqual(dest.listenerCount('unpipe'), 0);
+    });
+    dest.on('unpipe', unpipeChecker);
+  }
+}