diff --git a/lib/console.js b/lib/console.js index b5ff18fcc1911e..d2562df7c60dd5 100644 --- a/lib/console.js +++ b/lib/console.js @@ -82,7 +82,10 @@ function createWriteErrorHandler(stream) { // an `error` event. Adding a `once` listener will keep that error // from becoming an uncaught exception, but since the handler is // removed after the event, non-console.* writes won’t be affected. - stream.once('error', noop); + // we are only adding noop if there is no one else listening for 'error' + if (stream.listenerCount('error') === 0) { + stream.on('error', noop); + } } }; } diff --git a/test/parallel/test-console-log-stdio-broken-dest.js b/test/parallel/test-console-log-stdio-broken-dest.js new file mode 100644 index 00000000000000..d29e8ee5173be7 --- /dev/null +++ b/test/parallel/test-console-log-stdio-broken-dest.js @@ -0,0 +1,24 @@ +'use strict'; + +const common = require('../common'); +const { Writable } = require('stream'); +const { Console } = require('console'); +const { EventEmitter } = require('events'); + +const stream = new Writable({ + write(chunk, enc, cb) { + cb(); + }, + writev(chunks, cb) { + setTimeout(cb, 10, new Error('kaboom')); + } +}); +const myConsole = new Console(stream, stream); + +process.on('warning', common.mustNotCall); + +stream.cork(); +for (let i = 0; i < EventEmitter.defaultMaxListeners + 1; i++) { + myConsole.log('a message'); +} +stream.uncork();