Skip to content

Commit da102cd

Browse files
cjihrigtargos
authored andcommitted
console: don't attach unnecessary error handlers
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]>
1 parent 6984ca1 commit da102cd

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/internal/console/constructor.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ Console.prototype[kWriteToConsole] = function(streamSymbol, string) {
231231
// handle both situations.
232232
try {
233233
// Add and later remove a noop error handler to catch synchronous errors.
234-
stream.once('error', noop);
234+
if (stream.listenerCount('error') === 0)
235+
stream.once('error', noop);
235236

236237
stream.write(string, errorHandler);
237238
} catch (e) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Worker, isMainThread } = require('worker_threads');
4+
const EventEmitter = require('events');
5+
6+
if (isMainThread) {
7+
process.on('warning', common.mustNotCall('unexpected warning'));
8+
9+
for (let i = 0; i < EventEmitter.defaultMaxListeners; ++i) {
10+
const worker = new Worker(__filename);
11+
12+
worker.on('exit', common.mustCall(() => {
13+
console.log('a'); // This console.log() is part of the test.
14+
}));
15+
}
16+
}

0 commit comments

Comments
 (0)