Fix: serialize concurrent game.ready with FOR UPDATE to prevent stuck Initializing sessions

Both players navigate to the game screen at the same time after match_found
and both send game.ready concurrently. Without row-level locking, each request
reads the other player's ready flag as false before the other's SaveChanges
commits, so neither handler ever sees both flags set — both return
WaitingForOpponent and the session stays Initializing forever.

Fix: wrap GameReadyHandler in an explicit transaction with SELECT … FOR UPDATE,
matching the same locking pattern already used by LoadActiveSessionAsync for
active-game mutations. The second request now blocks until the first commits,
then reads the updated ready flag and proceeds to initialize.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-09 18:06:00 -04:00
co-authored by Claude Happy
parent fd95cce564
commit 764a47b7d8
@@ -48,15 +48,32 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler
var req = _serializer.DeserializePayload<GameReadyRequest>(message.Payload);
var accountId = Guid.Parse(connection.UserId);
var session = await _gameDb.GameSessions.FindAsync([req.SessionId], ct)
// Serialize concurrent game.ready requests with a row-level lock so that
// exactly one request performs initialization when both players are ready.
// Without this, both players send game.ready at nearly the same time (both
// clients navigate to the game screen after match_found), each reads the
// other's ready flag as false, and neither triggers initialization.
await _gameDb.Database.BeginTransactionAsync(ct);
var session = await _gameDb.GameSessions
.FromSqlInterpolated(
$"""
SELECT * FROM game.game_sessions
WHERE "Id" = {req.SessionId}
FOR UPDATE
""")
.SingleOrDefaultAsync(ct)
?? throw new GameException("SESSION_NOT_FOUND");
if (session.PlayerOneAccountId != accountId && session.PlayerTwoAccountId != accountId)
throw new GameException("NOT_IN_SESSION");
if (session.Status == GameSessionStatus.Active)
{
await _gameDb.Database.CommitTransactionAsync(ct);
return ServerMessage.Reply("game.ready.ack", message.Seq,
new GameReadyResponse(session.Id, "AlreadyStarted"));
}
if (session.Status != GameSessionStatus.Initializing)
throw new GameException("SESSION_NOT_INITIALIZING");
@@ -69,6 +86,7 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler
if (!session.PlayerOneReady || !session.PlayerTwoReady)
{
await _gameDb.Database.CommitTransactionAsync(ct);
return ServerMessage.Reply("game.ready.ack", message.Seq,
new GameReadyResponse(session.Id, "WaitingForOpponent"));
}
@@ -95,6 +113,7 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler
session.StartedAt = DateTimeOffset.UtcNow;
await _gameDb.SaveChangesAsync(ct);
await _gameDb.Database.CommitTransactionAsync(ct);
// Push initial state to both players.
await PushStateToPlayersAsync(_clientPush, session, state, ct);