Skip to content

fix: prevent data race on room.Clients in WebSocket join handler - #577

Open
RounakKumarAgarwal wants to merge 1 commit into
AOSSIE-Org:mainfrom
RounakKumarAgarwal:fix/websocket-clients-race
Open

RounakKumarAgarwal wants to merge 1 commit into
AOSSIE-Org:mainfrom
RounakKumarAgarwal:fix/websocket-clients-race

Conversation

@RounakKumarAgarwal

@RounakKumarAgarwal RounakKumarAgarwal commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #568

The WebSocket join handler iterated room.Clients without holding room.Mutex (the lock was released just before the loop), while calling SafeWriteJSON inside the loop. Since each connection runs in its own goroutine, a concurrent join/leave on the same room could trigger fatal error: concurrent map iteration and map write, crashing the backend.

Fixed using the snapshot pattern already used elsewhere in the codebase:

  • Build the participant detail payloads into a local slice under room.Mutex, then release the lock.
  • Perform the network writes (SafeWriteJSON) after releasing the lock.

This closes the race and avoids holding the mutex during network I/O. Behavior is unchanged — both original branches wrote the same detailMessage to the new client, so collapsing them is equivalent.

Changes

  • backend/websocket/websocket.go: snapshot room.Clients under lock, write outside it.

Testing

  • go build ./websocket/ passes.
  • Iteration over room.Clients now happens under room.Mutex (snapshot under lock, writes after release), matching the safe pattern at every other access site in this file — removing the unguarded concurrent read.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when sending participant details to newly connected clients.
    • Ensured each client receives consistent participant information during connection setup.

@coderabbitai

coderabbitai Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: aa0c8d10-d3ce-4713-9a51-4577602bcbc6

📥 Commits

Reviewing files that changed from the base of the PR and between 9f90f9b and ee89877.

📒 Files selected for processing (1)
  • backend/websocket/websocket.go

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The WebSocket join handler now snapshots participant detail messages while holding room.Mutex. It releases the mutex before sending messages and removes redundant payload branching.

Changes

WebSocket concurrency

Layer / File(s) Summary
Protected participant snapshot
backend/websocket/websocket.go
The handler locks room.Mutex while iterating over room.Clients, stores detail messages in a slice, unlocks the room, and then sends each message. The redundant self-versus-existing-client branch was removed.

Priority: ⬆️ High

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: High

Suggested reviewers: priyanshunitr

Merge Risk: ⚪ Minimal · up to ee898

The join flow now protects participant snapshots from concurrent map access while keeping network writes outside the room lock. No merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing a data race on room.Clients in the WebSocket join handler.
Linked Issues check ✅ Passed Issue #568 requires synchronized access to room.Clients in the new-client join handler. The PR locks room.Mutex, snapshots participant detail messages, unlocks the mutex, and then performs `SafeWr…
Out of Scope Changes check ✅ Passed The PR changes only backend/websocket/websocket.go. The changes support Issue #568 by restructuring the affected iteration and preserving the participant-message behavior. The removal of redundant b…
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

[BUG]: Data race — room.Clients iterated without holding room.Mutex can crash the server

1 participant