From 88bb2ec1a98a89f29e79ac73ff9f400ea3c46ff7 Mon Sep 17 00:00:00 2001 From: PrimeBuild-pc Date: Thu, 22 Oct 2026 10:45:32 +0200 Subject: [PATCH] fix: Game Boost no longer ignores a game that reuses a just-rejected PID for 30 s The memory of a rejected foreground process was keyed by PID only; it now also compares the process start time. Found by the independent Codex review. --- Services/GameSessionService.cs | 7 +++++-- .../GameSessionServiceTests.cs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Services/GameSessionService.cs b/Services/GameSessionService.cs index fdcf6d0..bde1940 100644 --- a/Services/GameSessionService.cs +++ b/Services/GameSessionService.cs @@ -240,7 +240,7 @@ namespace ThreadPilot.Services // Set by StopAsync: a tick already queued when the timer was disposed must not begin a session. private bool stopped; - private (int ProcessId, long Timestamp)? rejectedForeground; + private (int ProcessId, long StartTicks, long Timestamp)? rejectedForeground; private GameSession? session; private IGameProcessHandle? gameHandle; private GameBoostDecision? cachedDecision; @@ -454,8 +454,11 @@ namespace ThreadPilot.Services // The browser or desktop usually stays in front for minutes: building its full process model every // 3 s only to reject it again costs ~8 process queries and a window enumeration per tick. + // Keyed by start time too: Windows soon reuses the PID of an exited process, possibly for a game. + this.processProbe.TryGetIdentity(processId, out var foregroundStartTicks, out _); if (this.rejectedForeground is { } rejected && rejected.ProcessId == processId + && rejected.StartTicks == foregroundStartTicks && this.timeProvider.GetElapsedTime(rejected.Timestamp) < RejectedForegroundMemory) { return; @@ -468,7 +471,7 @@ namespace ThreadPilot.Services // Remembered only when the path was readable: a game just starting may not expose it yet. if (!string.IsNullOrEmpty(game.ExecutablePath)) { - this.rejectedForeground = (processId, this.timeProvider.GetTimestamp()); + this.rejectedForeground = (processId, foregroundStartTicks, this.timeProvider.GetTimestamp()); } return; diff --git a/Tests/ThreadPilot.Core.Tests/GameSessionServiceTests.cs b/Tests/ThreadPilot.Core.Tests/GameSessionServiceTests.cs index 274dd68..0e5df94 100644 --- a/Tests/ThreadPilot.Core.Tests/GameSessionServiceTests.cs +++ b/Tests/ThreadPilot.Core.Tests/GameSessionServiceTests.cs @@ -61,6 +61,24 @@ namespace ThreadPilot.Core.Tests Assert.All(this.processes.CpuSets.Values, Assert.Empty); } + [Fact] + public async Task RejectedPid_ReusedByAGame_StartsASessionAtTheNextTick() + { + // The 30 s memory of a rejected foreground process was keyed by PID only. + this.processes.Add(700, "notepad", @"C:\Windows\notepad.exe"); + this.foregroundPid = 700; + using var service = this.CreateService(); + await service.TickAsync(); + Assert.Null(service.ActiveGameName); + + this.processes.Add(700, "Game2", @"D:\SteamLibrary\steamapps\common\Game2\Game2.exe"); + this.processes.Restart(700); + this.clock.Advance(TimeSpan.FromSeconds(3)); + await service.TickAsync(); + + Assert.Equal("Game2", service.ActiveGameName); + } + [Fact] public async Task NonGameForeground_IsNotReexaminedEveryTick_ButIsAfterAWhile() {