From f8f6a21580544146d5a8527333e1130b336dc094 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 1 Oct 2019 23:59:59 +0200 Subject: [PATCH] stream: throw unhandled error for readable with autoDestroy If autoDestroy then we should still throw on unhandled errors. PR-URL: https://github.com/nodejs/node/pull/29806 Reviewed-By: Matteo Collina Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Co-Authored-By: Trivikram Kamat --- lib/_stream_readable.js | 9 ++++++-- .../test-stream-pipe-error-unhandled.js | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-stream-pipe-error-unhandled.js diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 996a14ddc0dc7d..5d2300cf891bca 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -777,8 +777,13 @@ Readable.prototype.pipe = function(dest, pipeOpts) { debug('onerror', er); unpipe(); dest.removeListener('error', onerror); - if (EE.listenerCount(dest, 'error') === 0) - errorOrDestroy(dest, er); + if (EE.listenerCount(dest, 'error') === 0) { + if (!dest.destroyed) { + errorOrDestroy(dest, er); + } else { + dest.emit('error', er); + } + } } // Make sure our error handler is attached before userland ones. diff --git a/test/parallel/test-stream-pipe-error-unhandled.js b/test/parallel/test-stream-pipe-error-unhandled.js new file mode 100644 index 00000000000000..42c1ce77fe4878 --- /dev/null +++ b/test/parallel/test-stream-pipe-error-unhandled.js @@ -0,0 +1,21 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const { Readable, Writable } = require('stream'); + +process.on('uncaughtException', common.mustCall((err) => { + assert.strictEqual(err.message, 'asd'); +})); + +const r = new Readable({ + read() { + this.push('asd'); + } +}); +const w = new Writable({ + autoDestroy: true, + write() {} +}); + +r.pipe(w); +w.destroy(new Error('asd'));