From 5b8f34e7940047ca2003f13b085340203c96bfec Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 9 Mar 2026 17:12:54 -0400 Subject: [PATCH] Fix: abandon stale Initializing sessions on Game startup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When SOR.Game is redeployed, sessions in Initializing status are unreachable — clients already sent game.ready to the old process and won't retry. GameStartupCleanupService runs once at startup, marks all Initializing sessions as Abandoned (WinCondition = "server_restart"), and pushes game.session_abandoned to both players so clients can return to the main menu instead of hanging on the loading screen. Also fix MatchmakerService to include WinnerOpReward, LoserOpReward, and RankDiffOpMultiplierPct in the rule snapshot so FinalizeGameAsync picks up the correct OP rewards from the snapshot. 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/GameMessages.cs | 7 ++ .../src/SOR.Game/GameStartupCleanupService.cs | 93 +++++++++++++++++++ Server/src/SOR.Game/ServiceRegistration.cs | 3 + .../src/SOR.Matchmaking/MatchmakerService.cs | 3 + 4 files changed, 106 insertions(+) create mode 100644 Server/src/SOR.Game/GameStartupCleanupService.cs diff --git a/Server/src/SOR.Game/GameMessages.cs b/Server/src/SOR.Game/GameMessages.cs index cb294ed..f9ed7d0 100644 --- a/Server/src/SOR.Game/GameMessages.cs +++ b/Server/src/SOR.Game/GameMessages.cs @@ -180,6 +180,13 @@ public sealed record GameOverPush( int WinnerOpDelta, int LoserOpDelta); +/// +/// Pushed to both players when a session that was Initializing is abandoned +/// (e.g., server restart before both players sent game.ready). +/// Clients should return to the main menu and show an appropriate message. +/// +public sealed record GameSessionAbandonedPush(Guid SessionId, string Reason); + /// /// Pushed to both players when a custom lobby is full and a GameSession has been created. /// Players should navigate to the game screen and send game.ready. diff --git a/Server/src/SOR.Game/GameStartupCleanupService.cs b/Server/src/SOR.Game/GameStartupCleanupService.cs new file mode 100644 index 0000000..e59545b --- /dev/null +++ b/Server/src/SOR.Game/GameStartupCleanupService.cs @@ -0,0 +1,93 @@ +namespace SOR.Game; + +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using SOR.Database.Contexts; +using SOR.Database.Enums; +using SOR.Networking.Abstractions.Push; +using SOR.Shared.Networking; + +/// +/// Runs once at startup before the service begins accepting WebSocket messages. +/// +/// Any GameSession stuck in the Initializing state means the previous Game process +/// died (redeploy, crash) before both players sent game.ready. The clients on +/// those sessions are stuck on a loading screen and will never recover on their own +/// because they already sent game.ready to the old process. +/// +/// This service abandons every Initializing session and pushes game.session_abandoned +/// to both players so their clients can return to the main menu. +/// +internal sealed class GameStartupCleanupService : IHostedService +{ + private readonly IServiceScopeFactory _scopeFactory; + private readonly ILogger _logger; + + public GameStartupCleanupService( + IServiceScopeFactory scopeFactory, + ILogger logger) + { + _scopeFactory = scopeFactory; + _logger = logger; + } + + public async Task StartAsync(CancellationToken cancellationToken) + { + await using var scope = _scopeFactory.CreateAsyncScope(); + var gameDb = scope.ServiceProvider.GetRequiredService(); + var clientPush = scope.ServiceProvider.GetRequiredService(); + + var stuck = await gameDb.GameSessions + .Where(s => s.Status == GameSessionStatus.Initializing) + .ToListAsync(cancellationToken); + + if (stuck.Count == 0) + return; + + _logger.LogWarning( + "Startup cleanup: abandoning {Count} Initializing session(s) left over from previous process.", + stuck.Count); + + var now = DateTimeOffset.UtcNow; + var push = new GameSessionAbandonedPush(Guid.Empty, "server_restart"); + + foreach (var session in stuck) + { + session.Status = GameSessionStatus.Abandoned; + session.EndedAt = now; + session.WinCondition = "server_restart"; + + // Best-effort push — clients on the loading screen will get a signal to + // return to menu. Ignore errors so a bad Redis connection doesn't block + // the rest of the cleanup or prevent the service from starting. + try + { + var sessionPush = push with { SessionId = session.Id }; + await Task.WhenAll( + clientPush.PushAsync( + session.PlayerOneAccountId.ToString(), + ServerMessage.Push("game.session_abandoned", sessionPush), + cancellationToken), + clientPush.PushAsync( + session.PlayerTwoAccountId.ToString(), + ServerMessage.Push("game.session_abandoned", sessionPush), + cancellationToken)); + } + catch (Exception ex) + { + _logger.LogWarning(ex, + "Could not push session_abandoned for session {SessionId}; clients will time out naturally.", + session.Id); + } + } + + await gameDb.SaveChangesAsync(cancellationToken); + + _logger.LogInformation( + "Startup cleanup complete: {Count} session(s) abandoned.", stuck.Count); + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; +} diff --git a/Server/src/SOR.Game/ServiceRegistration.cs b/Server/src/SOR.Game/ServiceRegistration.cs index cddd8e0..ec8dfd7 100644 --- a/Server/src/SOR.Game/ServiceRegistration.cs +++ b/Server/src/SOR.Game/ServiceRegistration.cs @@ -21,6 +21,9 @@ public static class ServiceRegistration services.AddCollectionDatabase(configuration); services.AddCatalogDatabase(configuration); + // Abandons any Initializing sessions left over from a previous process (redeploy/crash). + services.AddHostedService(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/Server/src/SOR.Matchmaking/MatchmakerService.cs b/Server/src/SOR.Matchmaking/MatchmakerService.cs index 97171f1..91325c8 100644 --- a/Server/src/SOR.Matchmaking/MatchmakerService.cs +++ b/Server/src/SOR.Matchmaking/MatchmakerService.cs @@ -128,6 +128,9 @@ internal sealed class MatchmakerService : BackgroundService rankedRules.DrawPerTurn, rankedRules.SpHasCap, rankedRules.SpCap, + rankedRules.WinnerOpReward, + rankedRules.LoserOpReward, + rankedRules.RankDiffOpMultiplierPct, }) : null, };