Skip to content

Commit 1158f44

Browse files
jseagullMylesBorins
authored andcommitted
events,test: fix TypeError in EventEmitter warning
Allows Symbol to be converted to String so it can be included in the error. Conflicts: lib/events.js Fixes: #9003 Backport-PR-URL: #12459 PR-URL: #9021 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
1 parent 7d4941f commit 1158f44

5 files changed

Lines changed: 58 additions & 2 deletions

‎lib/events.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ function _addListener(target, type, listener, prepend) {
257257
if(m&&m>0&&existing.length>m){
258258
existing.warned=true;
259259
constw=newError('Possible EventEmitter memory leak detected. '+
260-
`${existing.length}${type} listeners added. `+
261-
'Use emitter.setMaxListeners() to increase limit');
260+
`${existing.length}${String(type)} listeners `+
261+
'added. Use emitter.setMaxListeners() to '+
262+
'increase limit');
262263
w.name='Warning';
263264
w.emitter=target;
264265
w.type=type;

‎test/parallel/test-event-emitter-check-listener-leaks.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ assert.ok(!e._events['default'].hasOwnProperty('warned'));
1313
e.on('default',function(){});
1414
assert.ok(e._events['default'].warned);
1515

16+
// symbol
17+
constsymbol=Symbol('symbol');
18+
e.setMaxListeners(1);
19+
e.on(symbol,function(){});
20+
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
21+
e.on(symbol,function(){});
22+
assert.ok(e._events[symbol].hasOwnProperty('warned'));
23+
1624
// specific
1725
e.setMaxListeners(5);
1826
for(leti=0;i<5;i++){
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Flags: --no-warnings
2+
// The flag suppresses stderr output but the warning event will still emit
3+
'use strict';
4+
5+
constcommon=require('../common');
6+
constevents=require('events');
7+
constassert=require('assert');
8+
9+
conste=newevents.EventEmitter();
10+
e.setMaxListeners(1);
11+
12+
process.on('warning',common.mustCall((warning)=>{
13+
assert.ok(warninginstanceofError);
14+
assert.strictEqual(warning.name,'Warning');
15+
assert.strictEqual(warning.emitter,e);
16+
assert.strictEqual(warning.count,2);
17+
assert.strictEqual(warning.type,null);
18+
assert.ok(warning.message.includes('2 null listeners added.'));
19+
}));
20+
21+
e.on(null,function(){});
22+
e.on(null,function(){});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Flags: --no-warnings
2+
// The flag suppresses stderr output but the warning event will still emit
3+
'use strict';
4+
5+
constcommon=require('../common');
6+
constevents=require('events');
7+
constassert=require('assert');
8+
9+
constsymbol=Symbol('symbol');
10+
11+
conste=newevents.EventEmitter();
12+
e.setMaxListeners(1);
13+
14+
process.on('warning',common.mustCall((warning)=>{
15+
assert.ok(warninginstanceofError);
16+
assert.strictEqual(warning.name,'Warning');
17+
assert.strictEqual(warning.emitter,e);
18+
assert.strictEqual(warning.count,2);
19+
assert.strictEqual(warning.type,symbol);
20+
assert.ok(warning.message.includes('2 Symbol(symbol) listeners added.'));
21+
}));
22+
23+
e.on(symbol,function(){});
24+
e.on(symbol,function(){});

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ process.on('warning', common.mustCall((warning) => {
1515
assert.strictEqual(warning.emitter,e);
1616
assert.strictEqual(warning.count,2);
1717
assert.strictEqual(warning.type,'event-type');
18+
assert.ok(warning.message.includes('2 event-type listeners added.'));
1819
}));
1920

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

0 commit comments

Comments
 (0)