Skip to content

Commit cca375f

Browse files
committed
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 <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent a92ad36 commit cca375f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/internal/console/constructor.js

Lines changed: 2 additions & 1 deletion
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) {
Lines changed: 16 additions & 0 deletions
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)