Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃攪 Fix sensitive memory leak warnings when using presence #588

Merged
merged 1 commit into from
Feb 7, 2023

Conversation

alecgibson
Copy link
Collaborator

Fixes #584

At the moment, we attach Doc listeners for every remote client in a document (as well as every local user).

This means Docs can quickly hit the default of 10 maximum event listeners.

This change adds a DocPresenceEmitter class, which only registers any given Doc listener once, regardless of how many presence instances there are. It then forwards events on through its own emitters, which have a much higher value of 1000 max listeners set, in order to avoid too much alert spam, but also keep some checking for memory leaks.

@coveralls
Copy link

coveralls commented Feb 1, 2023

Coverage Status

Coverage: 97.471% (+0.03%) from 97.437% when pulling 2c04a08 on presence-events into cc0e338 on master.

Fixes #584

At the moment, we [attach `Doc` listeners][1] for every remote client in
a document (as well as every local user).

This means `Doc`s can quickly hit the [default of 10][2] maximum event
listeners.

This change adds a `DocPresenceEmitter` class, which only registers any
given `Doc` listener once, regardless of how many presence instances
there are. It then forwards events on through its own emitters, which
have a much higher value of 1000 max listeners set, in order to avoid
too much alert spam, but also keep *some* checking for memory leaks.

[1]: https://github.com/share/sharedb/blob/cc0e3382bf3df4c38c7ccc6bc52da71aff8c9f74/lib/client/presence/remote-doc-presence.js#L42-L47
[2]: https://nodejs.org/api/events.html#eventsdefaultmaxlisteners
Comment on lines 54 to 57
return function() {
Array.prototype.unshift.call(arguments, event);
emitter.emit.apply(emitter, arguments);
};
Copy link
Contributor

Choose a reason for hiding this comment

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

May be more readable to use emitter.emit.bind(emitter, event) here (already done while discussing)

@alecgibson alecgibson merged commit a321275 into master Feb 7, 2023
@alecgibson alecgibson deleted the presence-events branch February 7, 2023 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EventEmitter memory leak warning for too many remote presence clients
3 participants