Fixing issue #301#302
Conversation
📝 WalkthroughWalkthroughIntroduces an Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
backend/services/matchmaking.go (1)
210-215:⚠️ Potential issue | 🔴 CriticalCritical bug: rollback never resets
IsMatching = false— users are stuck in limbo again.When
InsertOnefails, you re-assign the user pointers into the pool map (which is redundant since they were never removed), but you never resetIsMatchingtofalse. Both users will remain permanently skipped byfindMatch(line 133 and 145 guard againstIsMatching == true). This reintroduces the exact "Matchmaking Limbo" the PR aims to fix.🐛 Proposed fix
if err != nil { ms.mutex.Lock() - ms.pool[user1.UserID] = user1 - ms.pool[user2.UserID] = user2 + user1.IsMatching = false + user2.IsMatching = false ms.mutex.Unlock() return }The users are already in the pool (they were never deleted), so re-assigning them is unnecessary. The only thing the rollback needs to do is clear the reservation flag.
🧹 Nitpick comments (3)
backend/services/matchmaking.go (3)
268-275: Minor:periodicMatchmakingfires goroutines for already-reserved users.The
StartedMatchmakingcheck on line 272 doesn't filter out users withIsMatching == true. Each tick will spawn afindMatchgoroutine for reserved users that immediately returns at line 133. Not a bug, but wasteful under load.♻️ Suggested filter
for userID, poolEntry := range ms.pool { - if poolEntry.StartedMatchmaking { + if poolEntry.StartedMatchmaking && !poolEntry.IsMatching { usersToMatch = append(usersToMatch, userID) }
228-244: Edge case:cleanupInactiveUserscan delete a reserved user mid-match.If a user's
LastActivityexceeds 5 minutes whileIsMatching == true(e.g., slow DB), the cleanup goroutine willdeletethem from the pool. A subsequent successfulRemoveFromPoolwould be a no-op, which is fine. However, if room creation fails, the rollback (once the critical bug above is fixed) would resetIsMatchingon a user pointer that's no longer in the map — silently losing them.Consider skipping
IsMatchingusers during cleanup:♻️ Suggested guard
for userID, poolEntry := range ms.pool { - if now.Sub(poolEntry.LastActivity) > 5*time.Minute { + if now.Sub(poolEntry.LastActivity) > 5*time.Minute && !poolEntry.IsMatching { delete(ms.pool, userID) }
220-226: Stray formatting: extra blank lines and closing brace alignment.Lines 220 and 225 introduce extra blank lines inside the function. This is cosmetic but mildly inconsistent with the rest of the file. Also verify that line 226 is the function's closing brace and not a stray — the indentation in the diff looks off compared to other function closings.
|
you need to include evidences of testing |
Solved issue : the "Matchmaking Limbo" race condition, we need to change the logic so users are not removed from the pool until the room is successfully created in the database. Instead, we will mark them as IsMatching.
Fix Strategy: "Reserve" before "Remove"
Reserve: In findMatch, set IsMatching = true (locking the users) instead of deleting them.
Commit/Rollback: In createRoomForMatch, delete them on success, or reset IsMatching = false on failure.
Video or screenshot : N/A logical changes
@bhavik-mangla closes #301
Summary by CodeRabbit