Skip to content

Prototype bleeding in events.js

Inheriting from EventEmitter using .prototype-old-school-notation like this:

var MyClass = function() {};
MyClass.prototype = new EventEmitter();

will result in the listeners added to one instance being executed by listeners of another instance.

var ok = new MyClass();
var broken = new MyClass();
broken.on('x', function () { console.log('b'); });
ok.on('x', function () { console.log('a'); });
ok.emit('x'); // b a

This can be quite easily resolved by changing the inheritance to:

util.inherits(MyClass, EventEmitter);

or

MyClass.prototype = Object.create(EventEmitter.prototype);

but it still is a problem because even developers like substack seem to not be aware of this: https://github.com/substack/node-charm/blob/c6646deed2dcee7f512bbbfac6d2ab58fb528a68/index.js#L67

This PR - at the time of writing this - still just contains just the test. I am not sure how to fix this. Any help welcome.

Note: This fixes a problem that exists in older node versions as well, however: I found this bug/problem while investigating a different bug that started to appear in io.js after version 1.3.0 either due to https://github.com/nodejs/io.js/commit/7061669dba or https://github.com/nodejs/io.js/commit/630f636334 However my other bug was too hard to properly reproduce. Fixing this will fix the other bug as well.

Merge request reports

Loading