Repository navigation
fix: prevent data race on room.Clients in WebSocket join handler - #577
RounakKumarAgarwal wants to merge 1 commit into
Conversation
|
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 configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe WebSocket join handler now snapshots participant detail messages while holding ChangesWebSocket concurrency
Priority: ⬆️ High Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: High Suggested reviewers: Merge Risk: ⚪ Minimal · up to 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)
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. Comment |
Description
Fixes #568
The WebSocket join handler iterated
room.Clientswithout holdingroom.Mutex(the lock was released just before the loop), while callingSafeWriteJSONinside the loop. Since each connection runs in its own goroutine, a concurrent join/leave on the same room could triggerfatal error: concurrent map iteration and map write, crashing the backend.Fixed using the snapshot pattern already used elsewhere in the codebase:
room.Mutex, then release the lock.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
detailMessageto the new client, so collapsing them is equivalent.Changes
backend/websocket/websocket.go: snapshotroom.Clientsunder lock, write outside it.Testing
go build ./websocket/passes.room.Clientsnow happens underroom.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