Skip to content
New issue

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

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

Already on GitHub? # to your account

events: fix TypeError in EventEmitter warning #9021

Closed
wants to merge 1 commit into from
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
5 changes: 3 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ function _addListener(target, type, listener, prepend) {
if (m && m > 0 && existing.length > m) {
existing.warned = true;
const w = new Error('Possible EventEmitter memory leak detected. ' +
`${existing.length} ${type} listeners added. ` +
'Use emitter.setMaxListeners() to increase limit');
`${existing.length} ${String(type)} listeners ` +
'added. Use emitter.setMaxListeners() to ' +
'increase limit');
w.name = 'MaxListenersExceededWarning';
w.emitter = target;
w.type = type;
Expand Down
12 changes: 10 additions & 2 deletions test/parallel/test-event-emitter-check-listener-leaks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
require('../common');
var assert = require('assert');
var events = require('events');
const assert = require('assert');
const events = require('events');

var e = new events.EventEmitter();

Expand All @@ -13,6 +13,14 @@ assert.ok(!e._events['default'].hasOwnProperty('warned'));
e.on('default', function() {});
assert.ok(e._events['default'].warned);

// symbol
const symbol = Symbol('symbol');
e.setMaxListeners(1);
e.on(symbol, function() {});
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
e.on(symbol, function() {});
assert.ok(e._events[symbol].hasOwnProperty('warned'));

// specific
e.setMaxListeners(5);
for (let i = 0; i < 5; i++) {
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-event-emitter-max-listeners-warning-for-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Flags: --no-warnings
// The flag suppresses stderr output but the warning event will still emit
'use strict';

const common = require('../common');
const events = require('events');
const assert = require('assert');

const e = new events.EventEmitter();
e.setMaxListeners(1);

process.on('warning', common.mustCall((warning) => {
assert.ok(warning instanceof Error);
assert.strictEqual(warning.name, 'MaxListenersExceededWarning');
assert.strictEqual(warning.emitter, e);
assert.strictEqual(warning.count, 2);
assert.strictEqual(warning.type, null);
assert.ok(warning.message.includes('2 null listeners added.'));
}));

e.on(null, function() {});
e.on(null, function() {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Flags: --no-warnings
// The flag suppresses stderr output but the warning event will still emit
'use strict';

const common = require('../common');
const events = require('events');
const assert = require('assert');

const symbol = Symbol('symbol');

const e = new events.EventEmitter();
e.setMaxListeners(1);

process.on('warning', common.mustCall((warning) => {
assert.ok(warning instanceof Error);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an assertion with something like this:

assert.ok(warning.message.includes('2 Symbol(symbol) listeners added.'));

assert.strictEqual(warning.name, 'MaxListenersExceededWarning');
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.'));
}));

e.on(symbol, function() {});
e.on(symbol, function() {});
1 change: 1 addition & 0 deletions test/parallel/test-event-emitter-max-listeners-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ 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.'));
}));

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