Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

events: fix: shared object references #25467

Closed
wants to merge 1 commit into from
Closed

events: fix: shared object references #25467

wants to merge 1 commit into from

Conversation

samhmills
Copy link

Fixes the issue: #25466

When emitting to multiple listeners for the same event type, the for
loop iteration over these listeners shares the same arguments. This
means (for objects), the copy is only shallow, and changes made by one
listener are overwritten on the object for the next listener.

For example, if we pass an object with arrays: as below

EventEmitter.emit("event", {
  data: [{
      users: [9237895]
  }]
});

And the first listener sets the users array as null like so:

EventEmitter.on('event', function(packet) {
    var data = packet.data[0];
    data.users = null;
});

The second event listener will ALWAYS show the array as null. This is
because only a shallow copy of the arguments is made and not a deep
copy.

This fixes that issue, by enforcing a deep copy and removing all
references from the new arguments array passed to each listener.

Link to code used to reproduce this issue:
https://ghostbin.com/paste/v4ova

Link to issue:
#25466

@jasnell
Copy link
Member

jasnell commented Jun 1, 2015

@Hunchmun ... you should be able to squash these commits down easily by doing a git reset --soft HEAD~3 then redoing the final commit followed by a git push -f to force the update on your branch. Makes things a bit cleaner when landing.

@samhmills samhmills closed this Jun 1, 2015
@samhmills samhmills reopened this Jun 1, 2015
@samhmills
Copy link
Author

@jasnell Thanks for your help, completed.

@@ -44,6 +44,11 @@ calls passing the same combination of `event` and `listener` will result in the

Returns emitter, so calls can be chained.

**Note:** Due to Pass by Reference, any objects passed to event listeners will be
Copy link

Choose a reason for hiding this comment

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

How about this?

Note: Objects are passed by reference, meaning that the same object can be passed to multiple listeners. If a listener needs to modify a shared object, a copy of the object should be made.

Copy link

Choose a reason for hiding this comment

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

I wonder if this note should go on emit() instead of addListener().

Copy link
Member

Choose a reason for hiding this comment

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

Likely. and +1 on the revision.

@samhmills samhmills closed this Jun 1, 2015
Updated documentation as per the issue below:
#25466

Event listeners can alter parts of the passed object, in some
circumstances the changes are passed to the next listeners
due to pass by reference. This is documentation of that behavior.
@jasnell
Copy link
Member

jasnell commented Jun 4, 2015

@Hunchmun ... did you mean to close this?

@samhmills samhmills reopened this Jun 4, 2015
@samhmills
Copy link
Author

@jasnell nope, my apologies, this was as a result of resetting the branch, forgot to reopen,

Thanks,

@jasnell
Copy link
Member

jasnell commented Jun 4, 2015

Thanks @Hunchmun! ... LGTM

jasnell pushed a commit that referenced this pull request Aug 27, 2015
Updated documentation as per the issue below:
#25466

Event listeners can alter parts of the passed object, in some
circumstances the changes are passed to the next listeners
due to pass by reference. This is documentation of that behavior.

PR-URL: #25467
Reviewed-By: jasnell - James M Snell <[email protected]>
@jasnell
Copy link
Member

jasnell commented Aug 27, 2015

Landed in a24db43

@jasnell jasnell closed this Aug 27, 2015
jBarz pushed a commit to ibmruntimes/node that referenced this pull request Nov 4, 2016
Updated documentation as per the issue below:
nodejs#25466

Event listeners can alter parts of the passed object, in some
circumstances the changes are passed to the next listeners
due to pass by reference. This is documentation of that behavior.

PR-URL: nodejs#25467
Reviewed-By: jasnell - James M Snell <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants