Skip to content

Commit 2b91256

Browse files
felixgery
authored andcommitted
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 nodejs#1095.
1 parent d222594 commit 2b91256

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Stream.prototype.pipe = function(dest, options) {
9595
// don't leave dangling pipes when there are errors.
9696
function onerror(er) {
9797
cleanup();
98-
if (this.listeners('error').length === 1) {
98+
if (this.listeners('error').length === 0) {
9999
throw er; // Unhandled stream error in pipe.
100100
}
101101
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)