From df81ba619447af85fbd32cf953ccef993ca52145 Mon Sep 17 00:00:00 2001 From: Davide Giacometti <25966642+davidegiacometti@users.noreply.github.com> Date: Fri, 13 Jun 2025 19:57:40 +0200 Subject: [PATCH] [CmdPal] Close Gracefully (#39589) ## Summary of the Pull Request CmdPal is leaking extensions processes as the process is immediately killed. This PR fixes the 1.5s wait for the process to end correctly and also close extensions. ## PR Checklist - [ ] **Closes:** #xxx - [ ] **Communication:** I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end user facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed - Verified that if CmdPal closes in 1.5s, extensions and process are closed (note that GitHub extension doesn't close probably due to https://github.com/microsoft/PowerToys/pull/39209) - Verified that if CmdPal doesn't close in 1.5s the process is killed and extensions aren't closed (tested adding a delay in the code) --- src/modules/cmdpal/CmdPalModuleInterface/dllmain.cpp | 10 ++++++---- src/modules/cmdpal/Microsoft.CmdPal.UI/App.xaml.cs | 1 + 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/modules/cmdpal/CmdPalModuleInterface/dllmain.cpp b/src/modules/cmdpal/CmdPalModuleInterface/dllmain.cpp index 9b9ce7ec56..7c6a7926db 100644 --- a/src/modules/cmdpal/CmdPalModuleInterface/dllmain.cpp +++ b/src/modules/cmdpal/CmdPalModuleInterface/dllmain.cpp @@ -117,16 +117,18 @@ private: for (DWORD pid : processIds) { - HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); + HANDLE hProcess = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, FALSE, pid); if (hProcess != NULL) { SetEvent(m_hTerminateEvent); - // Wait for 1.5 seconds for the process to end correctly and stop etw tracer - WaitForSingleObject(hProcess, 1500); + // Wait for 1.5 seconds for the process to end correctly, allowing time for ETW tracer and extensions to stop + if (WaitForSingleObject(hProcess, 1500) == WAIT_TIMEOUT) + { + TerminateProcess(hProcess, 0); + } - TerminateProcess(hProcess, 0); CloseHandle(hProcess); } } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/App.xaml.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/App.xaml.cs index 9670c8645f..4dee530c98 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/App.xaml.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/App.xaml.cs @@ -67,6 +67,7 @@ public partial class App : Application "Local\\PowerToysCmdPal-ExitEvent-eb73f6be-3f22-4b36-aee3-62924ba40bfd", () => { EtwTrace?.Dispose(); + AppWindow?.Close(); Environment.Exit(0); }); }