From fd95cce56411a7b0d381f6859d1670c2c731df53 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 9 Mar 2026 17:23:36 -0400 Subject: [PATCH] Fix: only abandon Initializing sessions older than 60 s at startup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MatchmakerService is registered before GameStartupCleanupService in Program.cs. BackgroundService.StartAsync fires ExecuteAsync on a background thread and returns immediately, so the matchmaker's first tick runs concurrently with the cleanup's StartAsync. If players were already in the queue when the process restarted, the matchmaker creates a fresh Initializing session within the first milliseconds of startup — and the cleanup's ToListAsync would include it, abandoning a brand-new session before game.ready could be sent. Adding a 60-second staleness threshold: sessions younger than 60 s are left alone; only genuinely stale sessions (from the previous process) are abandoned. A legitimate game.ready exchange completes in < 10 s, so 60 s is a safe lower bound. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude Co-Authored-By: Happy --- .../src/SOR.Game/GameStartupCleanupService.cs | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Server/src/SOR.Game/GameStartupCleanupService.cs b/Server/src/SOR.Game/GameStartupCleanupService.cs index e59545b..8972859 100644 --- a/Server/src/SOR.Game/GameStartupCleanupService.cs +++ b/Server/src/SOR.Game/GameStartupCleanupService.cs @@ -17,11 +17,22 @@ using SOR.Shared.Networking; /// 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. +/// This service abandons Initializing sessions that are clearly stale (older than +/// StaleThresholdSeconds) and pushes game.session_abandoned to both players so +/// their clients can return to the main menu. +/// +/// The staleness threshold exists to avoid a race condition: MatchmakerService is +/// registered before GameStartupCleanupService and its BackgroundService.ExecuteAsync +/// fires immediately (no leading delay) on a background thread. If queued players +/// were waiting when the process restarted, the matchmaker can create a brand-new +/// Initializing session within the first few milliseconds of startup — before this +/// service's query runs. Excluding sessions younger than 60 s ensures those fresh +/// sessions are left alone so game.ready can proceed normally. /// internal sealed class GameStartupCleanupService : IHostedService { + private const int StaleThresholdSeconds = 60; + private readonly IServiceScopeFactory _scopeFactory; private readonly ILogger _logger; @@ -39,8 +50,13 @@ internal sealed class GameStartupCleanupService : IHostedService var gameDb = scope.ServiceProvider.GetRequiredService(); var clientPush = scope.ServiceProvider.GetRequiredService(); + // Only target sessions old enough to be definitely stuck. + // Sessions created in the last 60 s may still be mid-handshake. + var staleThreshold = DateTimeOffset.UtcNow.AddSeconds(-StaleThresholdSeconds); + var stuck = await gameDb.GameSessions - .Where(s => s.Status == GameSessionStatus.Initializing) + .Where(s => s.Status == GameSessionStatus.Initializing + && s.CreatedAt <= staleThreshold) .ToListAsync(cancellationToken); if (stuck.Count == 0)