Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

events: Fix checking of max listeners #2490

Closed
wants to merge 1 commit into from

2 participants

@tricknotes

EventEmitter.prototype.setMaxListeners(1) does not work as expected.
It works correctly with the argument(ex. 5, 100, ...) other than 1.

var EventEmitter = require('events').EventEmitter;
var ee = new EventEmitter();
ee.setMaxListeners(1);
ee.on('message', function() {});
ee.on('message', function() {}); // doesn't show warning against my expectation.
ee.on('message', function() {}); // show warning.

I fixed this issue by the way to check for max listeners after it set.

Thank you!

@koichik
Owner

@tricknotes - Thanks! Can you sign the CLA?

@tricknotes

I haven't signed yet.
Please wait a few days because I need to take counsel with my company.

I want to sign as soon as possible.

@tricknotes

I have submitted!

@koichik koichik closed this in 22d7fe1
@koichik
Owner

@tricknotes - Thanks! It has landed in v0.6 branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 8, 2012
  1. @tricknotes
This page is out of date. Refresh to see the latest.
View
38 lib/events.js
@@ -115,27 +115,29 @@ EventEmitter.prototype.addListener = function(type, listener) {
// If we've already got an array, just append.
this._events[type].push(listener);
- // Check for listener leak
- if (!this._events[type].warned) {
- var m;
- if (this._maxListeners !== undefined) {
- m = this._maxListeners;
- } else {
- m = defaultMaxListeners;
- }
-
- if (m && m > 0 && this._events[type].length > m) {
- this._events[type].warned = true;
- console.error('(node) warning: possible EventEmitter memory ' +
- 'leak detected. %d listeners added. ' +
- 'Use emitter.setMaxListeners() to increase limit.',
- this._events[type].length);
- console.trace();
- }
- }
} else {
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
+
+ }
+
+ // Check for listener leak
+ if (isArray(this._events[type]) && !this._events[type].warned) {
+ var m;
+ if (this._maxListeners !== undefined) {
+ m = this._maxListeners;
+ } else {
+ m = defaultMaxListeners;
+ }
+
+ if (m && m > 0 && this._events[type].length > m) {
+ this._events[type].warned = true;
+ console.error('(node) warning: possible EventEmitter memory ' +
+ 'leak detected. %d listeners added. ' +
+ 'Use emitter.setMaxListeners() to increase limit.',
+ this._events[type].length);
+ console.trace();
+ }
}
return this;
View
7 test/simple/test-event-emitter-check-listener-leaks.js
@@ -41,6 +41,13 @@ assert.ok(!e._events['specific'].hasOwnProperty('warned'));
e.on('specific', function() {});
assert.ok(e._events['specific'].warned);
+// only one
+e.setMaxListeners(1);
+e.on('only one', function() {});
+assert.ok(!e._events['only one'].hasOwnProperty('warned'));
+e.on('only one', function() {});
+assert.ok(e._events['only one'].hasOwnProperty('warned'));
+
// unlimited
e.setMaxListeners(0);
for (var i = 0; i < 1000; i++) {
Something went wrong with that request. Please try again.