Skip to content

Commit 88bb2ec

Browse files
committed
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.
1 parent 940d83c commit 88bb2ec

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎Services/GameSessionService.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public sealed class GameSessionService : IGameSessionService
240240

241241
// Set by StopAsync: a tick already queued when the timer was disposed must not begin a session.
242242
private bool stopped;
243-
private (int ProcessId, long Timestamp)? rejectedForeground;
243+
private (int ProcessId, long StartTicks, long Timestamp)? rejectedForeground;
244244
private GameSession? session;
245245
private IGameProcessHandle? gameHandle;
246246
private GameBoostDecision? cachedDecision;
@@ -454,8 +454,11 @@ private async Task TryBeginSessionAsync()
454454

455455
// The browser or desktop usually stays in front for minutes: building its full process model every
456456
// 3 s only to reject it again costs ~8 process queries and a window enumeration per tick.
457+
// Keyed by start time too: Windows soon reuses the PID of an exited process, possibly for a game.
458+
this.processProbe.TryGetIdentity(processId, out var foregroundStartTicks, out _);
457459
if (this.rejectedForeground is { } rejected
458460
&& rejected.ProcessId == processId
461+
&& rejected.StartTicks == foregroundStartTicks
459462
&& this.timeProvider.GetElapsedTime(rejected.Timestamp) < RejectedForegroundMemory)
460463
{
461464
return;
@@ -468,7 +471,7 @@ private async Task TryBeginSessionAsync()
468471
// Remembered only when the path was readable: a game just starting may not expose it yet.
469472
if (!string.IsNullOrEmpty(game.ExecutablePath))
470473
{
471-
this.rejectedForeground = (processId, this.timeProvider.GetTimestamp());
474+
this.rejectedForeground = (processId, foregroundStartTicks, this.timeProvider.GetTimestamp());
472475
}
473476

474477
return;

‎Tests/ThreadPilot.Core.Tests/GameSessionServiceTests.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ public async Task NotAGame_NoSession()
6161
Assert.All(this.processes.CpuSets.Values, Assert.Empty);
6262
}
6363

64+
[Fact]
65+
public async Task RejectedPid_ReusedByAGame_StartsASessionAtTheNextTick()
66+
{
67+
// The 30 s memory of a rejected foreground process was keyed by PID only.
68+
this.processes.Add(700, "notepad", @"C:\Windows\notepad.exe");
69+
this.foregroundPid = 700;
70+
using var service = this.CreateService();
71+
await service.TickAsync();
72+
Assert.Null(service.ActiveGameName);
73+
74+
this.processes.Add(700, "Game2", @"D:\SteamLibrary\steamapps\common\Game2\Game2.exe");
75+
this.processes.Restart(700);
76+
this.clock.Advance(TimeSpan.FromSeconds(3));
77+
await service.TickAsync();
78+
79+
Assert.Equal("Game2", service.ActiveGameName);
80+
}
81+
6482
[Fact]
6583
public async Task NonGameForeground_IsNotReexaminedEveryTick_ButIsAfterAWhile()
6684
{

0 commit comments

Comments
 (0)