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:
@@ -2,6 +2,7 @@ namespace SOR.Game.Handlers;
|
||||
|
||||
using System.Text.Json;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Enums;
|
||||
using SOR.Networking.Abstractions.EventBus;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Push;
|
||||
@@ -47,14 +48,22 @@ internal sealed class GameConcedeHandler : GameHandlerBase, IMessageHandler
|
||||
var state = GameEngine.Deserialize(session.StateJson!);
|
||||
var newState = GameEngine.Concede(state, actorId);
|
||||
|
||||
var dto = GameEngine.ToDto(newState, actorId);
|
||||
await SaveStateAsync(_gameDb, session, newState, actorId, "game.concede",
|
||||
JsonSerializer.Serialize(req), ct, JsonSerializer.Serialize(dto));
|
||||
|
||||
var winnerId = session.PlayerOneAccountId.ToString() == actorId
|
||||
? session.PlayerTwoAccountId
|
||||
: session.PlayerOneAccountId;
|
||||
|
||||
// Set completion fields BEFORE SaveStateAsync so they are committed atomically
|
||||
// with the StateJson update. This prevents the race window where StateJson has
|
||||
// Phase=GameOver but Status is still Active (causing ghost-match on next login).
|
||||
session.Status = GameSessionStatus.Completed;
|
||||
session.WinnerAccountId = winnerId;
|
||||
session.WinCondition = "concede";
|
||||
session.EndedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
var dto = GameEngine.ToDto(newState, actorId);
|
||||
await SaveStateAsync(_gameDb, session, newState, actorId, "game.concede",
|
||||
JsonSerializer.Serialize(req), ct, JsonSerializer.Serialize(dto));
|
||||
|
||||
await FinalizeGameAsync(
|
||||
_gameDb, _identityDb, _clientPush, _eventBus,
|
||||
session,
|
||||
|
||||
Reference in New Issue
Block a user