Skip to content

Commit 1f935f8

Browse files
BridgeARtargos
authored andcommitted
events: improve max listeners warning
This adds the constructor name of the event target to the emitted warning. Right now it's difficult to identify where the leak is actually coming from and having some further information about the source will likely help to identify the source. PR-URL: #27694 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 3ac4a71 commit 1f935f8

4 files changed

+20
-6
lines changed

lib/events.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const {
3131
ERR_UNHANDLED_ERROR
3232
} = require('internal/errors').codes;
3333

34+
const {
35+
inspect
36+
} = require('internal/util/inspect');
37+
3438
function EventEmitter() {
3539
EventEmitter.init.call(this);
3640
}
@@ -253,8 +257,8 @@ function _addListener(target, type, listener, prepend) {
253257
// eslint-disable-next-line no-restricted-syntax
254258
const w = new Error('Possible EventEmitter memory leak detected. ' +
255259
`${existing.length} ${String(type)} listeners ` +
256-
'added. Use emitter.setMaxListeners() to ' +
257-
'increase limit');
260+
`added to ${inspect(target, { depth: -1 })}. Use ` +
261+
'emitter.setMaxListeners() to increase limit');
258262
w.name = 'MaxListenersExceededWarning';
259263
w.emitter = target;
260264
w.type = type;

test/parallel/test-event-emitter-max-listeners-warning-for-null.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ process.on('warning', common.mustCall((warning) => {
1515
assert.strictEqual(warning.emitter, e);
1616
assert.strictEqual(warning.count, 2);
1717
assert.strictEqual(warning.type, null);
18-
assert.ok(warning.message.includes('2 null listeners added.'));
18+
assert.ok(warning.message.includes(
19+
'2 null listeners added to [EventEmitter].'));
1920
}));
2021

2122
e.on(null, () => {});

test/parallel/test-event-emitter-max-listeners-warning-for-symbol.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ process.on('warning', common.mustCall((warning) => {
1717
assert.strictEqual(warning.emitter, e);
1818
assert.strictEqual(warning.count, 2);
1919
assert.strictEqual(warning.type, symbol);
20-
assert.ok(warning.message.includes('2 Symbol(symbol) listeners added.'));
20+
assert.ok(warning.message.includes(
21+
'2 Symbol(symbol) listeners added to [EventEmitter].'));
2122
}));
2223

2324
e.on(symbol, () => {});

test/parallel/test-event-emitter-max-listeners-warning.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ const common = require('../common');
66
const events = require('events');
77
const assert = require('assert');
88

9-
const e = new events.EventEmitter();
9+
class FakeInput extends events.EventEmitter {
10+
resume() {}
11+
pause() {}
12+
write() {}
13+
end() {}
14+
}
15+
16+
const e = new FakeInput();
1017
e.setMaxListeners(1);
1118

1219
process.on('warning', common.mustCall((warning) => {
@@ -15,7 +22,8 @@ process.on('warning', common.mustCall((warning) => {
1522
assert.strictEqual(warning.emitter, e);
1623
assert.strictEqual(warning.count, 2);
1724
assert.strictEqual(warning.type, 'event-type');
18-
assert.ok(warning.message.includes('2 event-type listeners added.'));
25+
assert.ok(warning.message.includes(
26+
'2 event-type listeners added to [FakeInput].'));
1927
}));
2028

2129
e.on('event-type', () => {});

0 commit comments

Comments
 (0)