diff options
author | isaacs <i@izs.me> | 2013-08-19 07:59:39 -0700 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-08-19 09:26:49 -0700 |
commit | 545807918ee72f55dfefc6a4c557e3d4e2018ee1 (patch) | |
tree | ae535469537950441d45c521c14da252ec807182 /lib/_stream_readable.js | |
parent | 0c2960ef4ab83f0eef2fc60c2575403c33ba4c6b (diff) | |
download | node-545807918ee72f55dfefc6a4c557e3d4e2018ee1.tar.gz |
stream: Throw on 'error' if listeners removed
In this situation:
writable.on('error', handler);
readable.pipe(writable);
writable.removeListener('error', handler);
writable.emit('error', new Error('boom'));
there is actually no error handler, but it doesn't throw, because of the
fix for stream.once('error', handler), in 23d92ec.
Note that simply reverting that change is not valid either, because
otherwise this will emit twice, being handled the first time, and then
throwing the second:
writable.once('error', handler);
readable.pipe(writable);
writable.emit('error', new Error('boom'));
Fix this with a horrible hack to make the stream pipe onerror handler
added before any other userland handlers, so that our handler is not
affected by adding or removing any userland handlers.
Closes #6007.
Diffstat (limited to 'lib/_stream_readable.js')
-rwxr-xr-x | lib/_stream_readable.js | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index f35599f45..368c923b6 100755 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -511,14 +511,22 @@ Readable.prototype.pipe = function(dest, pipeOpts) { // if the dest has an error, then stop piping into it. // however, don't suppress the throwing behavior for this. - // check for listeners before emit removes one-time listeners. - var errListeners = EE.listenerCount(dest, 'error'); function onerror(er) { unpipe(); - if (errListeners === 0 && EE.listenerCount(dest, 'error') === 0) + dest.removeListener('error', onerror); + if (EE.listenerCount(dest, 'error') === 0) dest.emit('error', er); } - dest.once('error', onerror); + // This is a brutally ugly hack to make sure that our error handler + // is attached before any userland ones. NEVER DO THIS. + if (!dest._events.error) + dest.on('error', onerror); + else if (Array.isArray(dest._events.error)) + dest._events.error.unshift(onerror); + else + dest._events.error = [onerror, dest._events.error]; + + // Both close and finish should trigger unpipe, but only once. function onclose() { |