Skip to content
This repository
Browse code

Fix error handling bug in stream.pipe()

Problem: Since stream.pipe() is registering it's own error handlers on
the source and destination stream, it needs to replicate the
EventEmitter 'error' emitting semantics of throwing an error if there
are no other listeners. However, there was a off-by-one error because
the check for remaining listeners was done after cleanup() which means
the pipe's own listener was no longer included.

This would cause 'error' events on either the dest or the source to
throw if there was one other error listener, and while swallowing
the 'error' event if there was no other listener.

Solution: I added a test demonstrating the two issues and fixed the
problem by correcting the off-by-one error.

Fixes #1095.
commit 2b91256c6185fbcaaa5fc49a3a541e5ed0ec0e75 1 parent d222594
Felix Geisendörfer authored May 23, 2011 ry committed May 24, 2011
2  lib/stream.js
@@ -95,7 +95,7 @@ Stream.prototype.pipe = function(dest, options) {
95 95
   // don't leave dangling pipes when there are errors.
96 96
   function onerror(er) {
97 97
     cleanup();
98  
-    if (this.listeners('error').length === 1) {
  98
+    if (this.listeners('error').length === 0) {
99 99
       throw er; // Unhandled stream error in pipe.
100 100
     }
101 101
   }
58  test/simple/test-stream-pipe-error-handling.js
... ...
@@ -0,0 +1,58 @@
  1
+// Copyright Joyent, Inc. and other Node contributors.
  2
+//
  3
+// Permission is hereby granted, free of charge, to any person obtaining a
  4
+// copy of this software and associated documentation files (the
  5
+// "Software"), to deal in the Software without restriction, including
  6
+// without limitation the rights to use, copy, modify, merge, publish,
  7
+// distribute, sublicense, and/or sell copies of the Software, and to permit
  8
+// persons to whom the Software is furnished to do so, subject to the
  9
+// following conditions:
  10
+//
  11
+// The above copyright notice and this permission notice shall be included
  12
+// in all copies or substantial portions of the Software.
  13
+//
  14
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21
+
  22
+var common = require('../common');
  23
+var assert = require('assert');
  24
+var Stream = require('stream').Stream;
  25
+
  26
+(function testErrorListenerCatches() {
  27
+  var source = new Stream();
  28
+  var dest = new Stream();
  29
+
  30
+  source.pipe(dest);
  31
+
  32
+  var gotErr = null;
  33
+  source.on('error', function(err) {
  34
+    gotErr = err;
  35
+  });
  36
+
  37
+  var err = new Error('This stream turned into bacon.');
  38
+  source.emit('error', err);
  39
+  assert.strictEqual(gotErr, err);
  40
+})();
  41
+
  42
+(function testErrorWithoutListenerThrows() {
  43
+  var source = new Stream();
  44
+  var dest = new Stream();
  45
+
  46
+  source.pipe(dest);
  47
+
  48
+  var err = new Error('This stream turned into bacon.');
  49
+
  50
+  var gotErr = null;
  51
+  try {
  52
+    source.emit('error', err);
  53
+  } catch (e) {
  54
+    gotErr = e;
  55
+  }
  56
+
  57
+  assert.strictEqual(gotErr, err);
  58
+})();

0 notes on commit 2b91256

Aria Stewart

I laughed way harder than I expected to here.

Please sign in to comment.
Something went wrong with that request. Please try again.