From dc9b64dc6fab21f0af7e5973b986cc1e55413d23 Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 7 Mar 2026 13:12:28 -0500 Subject: [PATCH] Serialize concurrent session mutations with SELECT FOR UPDATE; board 9x9 - LoadActiveSessionAsync: begin explicit transaction + lock session row with SELECT FOR UPDATE so concurrent handlers for the same session queue up instead of racing - SaveStateAsync: commit (and release) the transaction after EF SaveChanges fixes duplicate key on IX_action_logs_GameSessionId_SequenceNumber when both players act simultaneously (e.g. both placing ArchSpirits) - GameRules: default board size 9x9 (was 8x8) Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- Server/src/SOR.Game/GameRules.cs | 4 +-- .../src/SOR.Game/Handlers/GameHandlerBase.cs | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Server/src/SOR.Game/GameRules.cs b/Server/src/SOR.Game/GameRules.cs index 07e9740..3cd4635 100644 --- a/Server/src/SOR.Game/GameRules.cs +++ b/Server/src/SOR.Game/GameRules.cs @@ -20,8 +20,8 @@ public sealed record GameRules public int DrawPerTurn { get; init; } = 1; public bool SpHasCap { get; init; } = false; public int SpCap { get; init; } = 0; - public int BoardWidth { get; init; } = 8; - public int BoardHeight { get; init; } = 8; + public int BoardWidth { get; init; } = 9; + public int BoardHeight { get; init; } = 9; public static readonly GameRules Default = new(); } diff --git a/Server/src/SOR.Game/Handlers/GameHandlerBase.cs b/Server/src/SOR.Game/Handlers/GameHandlerBase.cs index 54c1930..bb6dc2a 100644 --- a/Server/src/SOR.Game/Handlers/GameHandlerBase.cs +++ b/Server/src/SOR.Game/Handlers/GameHandlerBase.cs @@ -14,7 +14,10 @@ using SOR.Shared.Networking; internal abstract class GameHandlerBase { /// - /// Load and validate an active game session for the calling player. + /// Load and validate an active game session for the calling player, + /// acquiring a row-level lock (SELECT FOR UPDATE) inside an explicit + /// transaction so that concurrent mutations to the same session are + /// serialized. The transaction is committed by . /// Throws GameException on any validation failure. /// protected static async Task LoadActiveSessionAsync( @@ -23,7 +26,19 @@ internal abstract class GameHandlerBase string actorId, CancellationToken ct) { - var session = await gameDb.GameSessions.FindAsync([sessionId], ct) + // Begin a transaction so the FOR UPDATE lock is held until SaveStateAsync commits. + if (gameDb.Database.CurrentTransaction == null) + await gameDb.Database.BeginTransactionAsync(ct); + + // Lock the row so concurrent handlers for the same session queue up. + var session = await gameDb.GameSessions + .FromSqlInterpolated( + $""" + SELECT * FROM game.game_sessions + WHERE "Id" = {sessionId} + FOR UPDATE + """) + .SingleOrDefaultAsync(ct) ?? throw new GameException("SESSION_NOT_FOUND"); if (session.PlayerOneAccountId.ToString() != actorId && @@ -37,7 +52,8 @@ internal abstract class GameHandlerBase } /// - /// Persist the updated state JSON and append an action log entry. + /// Persist the updated state JSON and append an action log entry, + /// then commit the transaction opened by . /// protected static async Task SaveStateAsync( GameDbContext gameDb, @@ -62,6 +78,10 @@ internal abstract class GameHandlerBase }); await gameDb.SaveChangesAsync(ct); + + // Commit and release the FOR UPDATE lock. + if (gameDb.Database.CurrentTransaction != null) + await gameDb.Database.CommitTransactionAsync(ct); } ///