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 <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -14,7 +14,10 @@ using SOR.Shared.Networking;
|
||||
internal abstract class GameHandlerBase
|
||||
{
|
||||
/// <summary>
|
||||
/// 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 <see cref="SaveStateAsync"/>.
|
||||
/// Throws GameException on any validation failure.
|
||||
/// </summary>
|
||||
protected static async Task<GameSession> 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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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 <see cref="LoadActiveSessionAsync"/>.
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user