Fix: ghost-match bug — session stays Active after game ends
Two root causes fixed: 1. Race condition (primary cause): SaveStateAsync committed the transaction with Phase=GameOver in StateJson, but session.Status was not set to Completed until a separate SaveChangesAsync in FinalizeGameAsync. If the server crashed or any exception was thrown between those two calls, the session was left Active with a GameOver state — causing the "active game" prompt on next login. Fix: each game-over handler (concede, force_concede, attack) now sets session.Status/WinnerAccountId/WinCondition/EndedAt BEFORE calling SaveStateAsync, so all fields commit atomically in the same transaction. 2. Wrong save order in FinalizeGameAsync: identityDb.SaveChangesAsync (ELO update) ran before gameDb.SaveChangesAsync (Status=Completed). Any transient identity-DB error left the session permanently Active. Fix: gameDb is saved first; ELO runs after, so a failed ELO update never causes a ghost match. 3. Defensive startup heal (GameStartupCleanupService): on server start, Active sessions whose StateJson contains Phase=GameOver are now detected and marked Completed. game.over is pushed to any still-connected clients so they can exit cleanly. 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:
@@ -131,11 +131,19 @@ internal abstract class GameHandlerBase
|
||||
string winCondition,
|
||||
CancellationToken ct)
|
||||
{
|
||||
// These fields may already be set by the calling handler (so they are committed
|
||||
// atomically with the StateJson update in SaveStateAsync). Setting them here again
|
||||
// is idempotent and ensures correctness even if called without the pre-set pattern.
|
||||
session.Status = GameSessionStatus.Completed;
|
||||
session.WinnerAccountId = winnerAccountId;
|
||||
session.WinCondition = winCondition;
|
||||
session.EndedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
// Save game session status FIRST — this must succeed before we touch ELO.
|
||||
// If identity DB fails below, the session is still correctly marked Completed
|
||||
// so players won't be stuck in a ghost match on next login.
|
||||
await gameDb.SaveChangesAsync(ct);
|
||||
|
||||
// Load both accounts for ELO + OP adjustment.
|
||||
var accounts = await identityDb.Users
|
||||
.Where(a => a.Id == winnerAccountId || a.Id == loserAccountId)
|
||||
@@ -167,7 +175,6 @@ internal abstract class GameHandlerBase
|
||||
loser.OriginPoints += loserOp;
|
||||
|
||||
await identityDb.SaveChangesAsync(ct);
|
||||
await gameDb.SaveChangesAsync(ct);
|
||||
|
||||
var push = new GameOverPush(
|
||||
SessionId : session.Id,
|
||||
|
||||
Reference in New Issue
Block a user