8000 Add ScopedPresence to fix memory leaks with room-scoped presence subs… by sebinseban · Pull Request #855 · colyseus/colyseus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add ScopedPresence to fix memory leaks with room-scoped presence subs… #855

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sebinseban
Copy link

Room-scoped presence subscriptions auto-unsubscribe when room is disposed

This PR addresses the memory leak issue described in #843 where room-scoped presence subscriptions are not automatically cleaned up when a room is disposed. This particularly affects Redis Presence in multi-process setups, causing memory usage to grow over time.

Implementation
The solution implements the approach suggested in #843 by creating a ScopedPresence class that:

Wraps the global presence instance
Tracks all subscriptions made through it
Provides a dispose() method to clean up all subscriptions when a room is disposed
Changes include:

New ScopedPresence class that implements the Presence interface
Modified MatchMaker to create a ScopedPresence instance for each room
Modified Room to dispose the ScopedPresence instance when the room is disposed
Added export for ScopedPresence in the package index
How it fixes the issue
When a room is disposed, all presence subscriptions created by that room are automatically unsubscribed, preventing memory leaks. This is achieved by:

Each room gets its own ScopedPresence instance that wraps the global presence
All subscriptions made through the room's presence are tracked
When the room is disposed, all tracked subscriptions are automatically unsubscribed
Testing
The implementation has been tested with:

Manual testing with both LocalPresence and RedisPresence
Verification that subscriptions are properly cleaned up when rooms are disposed
Simulation of the exact scenario described in the issue:
Backward compatibility
This implementation maintains backward compatibility:

Existing code that uses this.presence will continue to work without changes
The ScopedPresence class delegates all methods to the global presence instance
No changes to the public API
Related issues
Fixes #843
Related to #842

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.

[Feature]: Room-scoped presence subscriptions should auto-unsubscribe when room is disposed
1 participant
0