diff --git a/MainWindow.Behaviors.partial.cs b/MainWindow.Behaviors.partial.cs index d56ad6d..5342057 100644 --- a/MainWindow.Behaviors.partial.cs +++ b/MainWindow.Behaviors.partial.cs @@ -710,7 +710,7 @@ namespace ThreadPilot this.LogDebug("Starting graceful shutdown..."); this.selfResourceManagementService.RestoreForegroundMode(); - // 1. Stop monitoring services; Game Boost first restores what it changed + // 1. Game Boost restores what it changed try { await this.serviceProvider.GetRequiredService().StopAsync(); @@ -720,18 +720,7 @@ namespace ThreadPilot this.LogDebug($"Error stopping Game Boost: {ex.Message}"); } - try - { - this.LogDebug("Stopping process monitoring manager..."); - await this.processMonitorManagerService.StopAsync(); - this.LogDebug("Process monitoring manager stopped"); - } - catch (Exception ex) - { - this.LogDebug($"Error stopping process monitoring: {ex.Message}"); - } - - // 2. Cleanup applied CPU masks (like CPU Set Setter's ClearAllProcessMasksNoSave) + // 2. Reset tracked masks/priorities before the monitor stops: stopping it untracks every process if (this.settingsService.Settings.ClearMasksOnClose) { try @@ -760,6 +749,17 @@ namespace ThreadPilot } } + try + { + this.LogDebug("Stopping process monitoring manager..."); + await this.processMonitorManagerService.StopAsync(); + this.LogDebug("Process monitoring manager stopped"); + } + catch (Exception ex) + { + this.LogDebug($"Error stopping process monitoring: {ex.Message}"); + } + // 3. Restore default power plan if configured if (this.settingsService.Settings.RestoreDefaultPowerPlanOnExit) { diff --git a/Tests/ThreadPilot.Core.Tests/SystemTweaksStartupAndStyleTests.cs b/Tests/ThreadPilot.Core.Tests/SystemTweaksStartupAndStyleTests.cs index 584edfe..0436b23 100644 --- a/Tests/ThreadPilot.Core.Tests/SystemTweaksStartupAndStyleTests.cs +++ b/Tests/ThreadPilot.Core.Tests/SystemTweaksStartupAndStyleTests.cs @@ -16,6 +16,21 @@ namespace ThreadPilot.Core.Tests Assert.Contains("this.initializedSections.Add(\"Tweaks\");", startupSection, StringComparison.Ordinal); } + [Fact] + public void Shutdown_ResetsTrackedChangesBeforeTheMonitorForgetsThem() + { + // ProcessMonitorManagerService.StopAsync untracks every process it changed, so + // "reset process changes on exit" found nothing to reset when it ran afterwards. + var source = File.ReadAllText(GetRepositoryFilePath("MainWindow.Behaviors.partial.cs")); + var start = source.IndexOf("private async Task PerformGracefulShutdownAsync(", StringComparison.Ordinal); + var shutdown = source[start..source.IndexOf("// 3. Restore default power plan", start, StringComparison.Ordinal)]; + + var reset = shutdown.IndexOf("await processService.ClearAllAppliedMasksAsync();", StringComparison.Ordinal); + var stopMonitor = shutdown.IndexOf("await this.processMonitorManagerService.StopAsync();", StringComparison.Ordinal); + Assert.True(reset >= 0 && stopMonitor >= 0); + Assert.True(reset < stopMonitor); + } + [Fact] public void TweaksView_UsesWindows11ToggleWithStateLabel() {