Fix: only abandon Initializing sessions older than 60 s at startup
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 <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -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.
|
||||
/// </summary>
|
||||
internal sealed class GameStartupCleanupService : IHostedService
|
||||
{
|
||||
private const int StaleThresholdSeconds = 60;
|
||||
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly ILogger<GameStartupCleanupService> _logger;
|
||||
|
||||
@@ -39,8 +50,13 @@ internal sealed class GameStartupCleanupService : IHostedService
|
||||
var gameDb = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
var clientPush = scope.ServiceProvider.GetRequiredService<IClientPush>();
|
||||
|
||||
// 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)
|
||||
|
||||
Reference in New Issue
Block a user