From 764a47b7d8a96d80abe2dd22e8b1e13454afb66d Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 9 Mar 2026 18:06:00 -0400 Subject: [PATCH] Fix: serialize concurrent game.ready with FOR UPDATE to prevent stuck Initializing sessions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-Authored-By: Happy --- .../src/SOR.Game/Handlers/GameReadyHandler.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Server/src/SOR.Game/Handlers/GameReadyHandler.cs b/Server/src/SOR.Game/Handlers/GameReadyHandler.cs index 72a274f..3ff4d07 100644 --- a/Server/src/SOR.Game/Handlers/GameReadyHandler.cs +++ b/Server/src/SOR.Game/Handlers/GameReadyHandler.cs @@ -48,15 +48,32 @@ internal sealed class GameReadyHandler : GameHandlerBase, IMessageHandler var req = _serializer.DeserializePayload(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);