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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaxListenersExceededWarning due to SSUBSCRIBE/SUNSUBSCRIBE memory leak #528

Closed
RoccoC opened this issue Feb 29, 2024 · 2 comments
Closed
Milestone

Comments

@RoccoC
Copy link
Contributor

RoccoC commented Feb 29, 2024

My project utilizes the ShardedRedisAdapter and Redis v7 to sync state across server instances. I am using the default "dynamic" subscription mode which creates a new channel subscription for each public room.

When a room is created, the new channel subscription is created and the adapter adds a listener to the client's smessageBuffer EventEmitter (by way of SSUBSCRIBE in /lib/util.ts). However, this event listener is not removed when the room is deleted, and consequently I'm encountering a MaxListenersExceededWarning when creating more than 8 rooms regardless of whether the rooms still exist.

I should increase the client's max listener threshold since it will not be uncommon to have >8 rooms at any given time, but it also looks like SUNSUBSCRIBE should be updated to remove the smessageBuffer listener. I'm happy to raise a PR to address this, but wanted to make sure I'm not missing something! Thanks in advance!

@RoccoC
Copy link
Contributor Author

RoccoC commented Mar 1, 2024

Raised a PR here: #529 🙏

darrachequesne pushed a commit that referenced this issue Mar 13, 2024
This pull request introduces a change to the sharded adapter's `SSUBSCRIBE` logic: Previously, for each dynamic channel/room, a unique listener was added to the client's `smessageBuffer`. This approach led to a large number of listeners (resulting in `MaxListenersExceededWarning`), especially in scenarios with many dynamic channels. Further, listeners were not being removed when unsubscribing, leading to a memory leak.

The new implementation replaces the multiple listeners with a single `smessageBuffer` listener. This listener is registered once and handles all dynamic channels by maintaining specific channel handlers in a `Map`. Listeners are added to this `Map` in `SSUBSCRIBE` and removed from the `Map` in `SUNSUBSCRIBE`.

Related: #528
@darrachequesne
Copy link
Member

This should be fixed by 2113e8d, included in version 8.3.0.

@darrachequesne darrachequesne added this to the 8.3.0 milestone Mar 13, 2024
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

No branches or pull requests

2 participants