Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: improve max listeners warning #27694

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const {
ERR_UNHANDLED_ERROR
} = require('internal/errors').codes;

const {
inspect
} = require('internal/util/inspect');

function EventEmitter() {
EventEmitter.init.call(this);
}
Expand Down Expand Up @@ -253,8 +257,8 @@ function _addListener(target, type, listener, prepend) {
// eslint-disable-next-line no-restricted-syntax
const w = new Error('Possible EventEmitter memory leak detected. ' +
`${existing.length} ${String(type)} listeners ` +
'added. Use emitter.setMaxListeners() to ' +
'increase limit');
`added to ${inspect(target, { depth: -1 })}. Use ` +
'emitter.setMaxListeners() to increase limit');
w.name = 'MaxListenersExceededWarning';
w.emitter = target;
w.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.emitter, e);
assert.strictEqual(warning.count, 2);
assert.strictEqual(warning.type, null);
assert.ok(warning.message.includes('2 null listeners added.'));
assert.ok(warning.message.includes(
'2 null listeners added to [EventEmitter].'));
}));

e.on(null, () => {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.emitter, e);
assert.strictEqual(warning.count, 2);
assert.strictEqual(warning.type, symbol);
assert.ok(warning.message.includes('2 Symbol(symbol) listeners added.'));
assert.ok(warning.message.includes(
'2 Symbol(symbol) listeners added to [EventEmitter].'));
}));

e.on(symbol, () => {});
Expand Down
12 changes: 10 additions & 2 deletions test/parallel/test-event-emitter-max-listeners-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ const common = require('../common');
const events = require('events');
const assert = require('assert');

const e = new events.EventEmitter();
class FakeInput extends events.EventEmitter {
resume() {}
pause() {}
write() {}
end() {}
}

const e = new FakeInput();
e.setMaxListeners(1);

process.on('warning', common.mustCall((warning) => {
Expand All @@ -15,7 +22,8 @@ process.on('warning', common.mustCall((warning) => {
assert.strictEqual(warning.emitter, e);
assert.strictEqual(warning.count, 2);
assert.strictEqual(warning.type, 'event-type');
assert.ok(warning.message.includes('2 event-type listeners added.'));
assert.ok(warning.message.includes(
'2 event-type listeners added to [FakeInput].'));
}));

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