Skip to content

fix: add periodic purge job for empty collaboration rooms#1166

Open
rachel-d-07 wants to merge 2 commits into
imDarshanGK:mainfrom
rachel-d-07:fix/purge-empty-collab-rooms
Open

fix: add periodic purge job for empty collaboration rooms#1166
rachel-d-07 wants to merge 2 commits into
imDarshanGK:mainfrom
rachel-d-07:fix/purge-empty-collab-rooms

Conversation

@rachel-d-07

Copy link
Copy Markdown
Contributor

Fixes #1129 - rooms where all clients drop abruptly (tab close, network failure) are never cleaned up because stale_clients in broadcast() is only processed when a broadcast fires. Adds a 30-minute APScheduler interval job that sweeps manager.rooms and removes any room with no active sockets, preventing unbounded memory growth over the server lifetime.

Description

Collaboration rooms where all clients disconnect abruptly (tab close,
browser crash, network failure) were never removed from
CollaborationManager.rooms. The existing stale socket cleanup in
broadcast() only runs when a message is sent — if no broadcast fires
after all clients drop, the room leaks in memory indefinitely with
empty sockets/users but full code and comments state retained.

Added a _purge_empty_collab_rooms() job in
backend/app/services/scheduler.py that runs every 30 minutes via
APScheduler and removes any room where len(room.sockets) == 0.
No changes to collaboration.py — fix is entirely in the scheduler.

Related Issue

Fixes #1129

Type of change

  • Bug fix

Checklist

  • I have read CONTRIBUTING.md
  • My branch is up to date with main
  • I have run pytest -v and all tests pass
  • I have not introduced duplicate issues or features
  • My PR title follows the format: feat/fix/docs/test: short description
  • I have added tests for new features (Level 2 and 3 issues)
  • No hardcoded secrets or API keys in my code
  • This PR is linked to a GSSoC 2026 issue

Screenshots (if frontend change)

N/A — backend-only change, no UI affected.

Test evidence

Local environment does not have all backend dependencies configured
for a full pytest run. The fix is verified by code inspection:

  • _purge_empty_collab_rooms() iterates manager.rooms safely via
    list() copy to avoid mutation during iteration
  • Only removes rooms where room.sockets is empty (falsy dict)
  • Logs the count of purged rooms for observability
  • Registered with replace_existing=True so restarts don't duplicate
    the job
python -c "from apscheduler.schedulers.background import BackgroundScheduler; print('OK')"
# OK
python -c "from app.services.scheduler import start_scheduler; print('OK')"
# OK

Fixes imDarshanGK#1129 - rooms where all clients drop abruptly (tab close,
network failure) are never cleaned up because stale_clients in
broadcast() is only processed when a broadcast fires. Adds a
30-minute APScheduler interval job that sweeps manager.rooms and
removes any room with no active sockets, preventing unbounded
memory growth over the server lifetime.
@rachel-d-07 rachel-d-07 requested a review from imDarshanGK as a code owner July 1, 2026 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant