Skip to content

Commit

Permalink
console: don't attach unnecessary error handlers
Browse files Browse the repository at this point in the history
A noop error handler is attached to the console's stream on
write. The handler is then immediately removed after the write.
This commit skips adding the error handler if one already
exists.

PR-URL: #27691
Fixes: #27687
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
cjihrig authored and targos committed May 17, 2019
1 parent 6984ca1 commit da102cd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ Console.prototype[kWriteToConsole] = function(streamSymbol, string) {
// handle both situations.
try {
// Add and later remove a noop error handler to catch synchronous errors.
stream.once('error', noop);
if (stream.listenerCount('error') === 0)
stream.once('error', noop);

stream.write(string, errorHandler);
} catch (e) {
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-worker-console-listeners.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const { Worker, isMainThread } = require('worker_threads');
const EventEmitter = require('events');

if (isMainThread) {
process.on('warning', common.mustNotCall('unexpected warning'));

for (let i = 0; i < EventEmitter.defaultMaxListeners; ++i) {
const worker = new Worker(__filename);

worker.on('exit', common.mustCall(() => {
console.log('a'); // This console.log() is part of the test.
}));
}
}

0 comments on commit da102cd

Please sign in to comment.