Skip to content

[Bug] Remove the incorrect event listener. #36522

Description

@nivek3
  • Version: v15.4.0
  • Platform: MacOS V10.15.7
  • Subsystem: lib/events.js

What steps will reproduce the bug?

const EventEmitter = require('events');
const ee = new EventEmitter();
// const symbol = Symbol('listener');
const symbol = 'listener';

const fn2 = function(...args) {
    console.log('Call fn2', ...args)
};

const fn = function (...args) {
    console.log('Call fn', ...args);
};

fn[symbol] = fn2;

ee.on('one', fn2)
ee.on('one', fn);
ee.emit('one');
// Prints: 
//    Call fn2
//    Call fn

console.log('1. remove fn2')
ee.removeListener('one', fn2); // fn is removed, expect remove the fn2 
ee.emit('one');
// Prints: 
//    Call fn2

console.log('2. remove fn2 again')
ee.removeListener('one', fn2) // fn2 is removed
ee.emit('one')
// Prints: 
//    Call fn2

console.log('3. remove fn2 again');
ee.emit('one')

How often does it reproduce? Is there a required condition?

Occurs every time.

What is the expected behavior?

Call fn2
Call fn
1. remove fn2
Call fn
2. remove fn2 again
Call fn
3. remove fn2 again
Call fn

What do you see instead?

Call fn2
Call fn
1. remove fn2
Call fn2
2. remove fn2 again
3. remove fn2 again

Additional information

Replace the listener attribute of event listener with symbol

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    eventsIssues and PRs related to EventEmitter and the events module.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions