From 4785af24255f6cb22353a44f48de56142ab5b38f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pol=C3=A1=C5=A1ek?= Date: Mon, 28 Jul 2025 23:41:27 +0200 Subject: [PATCH] CmdPal: Handle exceptions when enqueuing callbacks to UI thread in IconCacheService (#40716) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary of the Pull Request Handle exceptions thrown in TryEnqueue callbacks so they don’t crash the app (as they cannot be caught by the global exception handler). Any exceptions are now returned to the caller for handling. Additionally, a failure to enqueue the operation onto the dispatcher will also result in an exception. This is not a breaking change, as exceptions only propagate within the class and do not affect external callers. Ref: #38260 ## PR Checklist - [ ] **Closes:** #38260 - [ ] **Communication:** I've discussed this with core contributors already. If the 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 --- .../Helpers/IconCacheService.cs | 34 +++++++++++++++---- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/IconCacheService.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/IconCacheService.cs index 3b4bc56ffd..59a6d04ca4 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/IconCacheService.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI/Helpers/IconCacheService.cs @@ -34,9 +34,9 @@ public sealed class IconCacheService(DispatcherQueue dispatcherQueue) { return await StreamToIconSource(icon.Data.Unsafe!); } - catch + catch (Exception ex) { - Debug.WriteLine("Failed to load icon from stream"); + Debug.WriteLine("Failed to load icon from stream: " + ex); } } } @@ -63,17 +63,37 @@ public sealed class IconCacheService(DispatcherQueue dispatcherQueue) { // Return the bitmap image via TaskCompletionSource. Using WCT's EnqueueAsync does not suffice here, since if // we're already on the thread of the DispatcherQueue then it just directly calls the function, with no async involved. - var completionSource = new TaskCompletionSource(); - dispatcherQueue.TryEnqueue(async () => + return await TryEnqueueAsync(dispatcherQueue, async () => { using var bitmapStream = await iconStreamRef.OpenReadAsync(); var itemImage = new BitmapImage(); await itemImage.SetSourceAsync(bitmapStream); - completionSource.TrySetResult(itemImage); + return itemImage; + }); + } + + private static Task TryEnqueueAsync(DispatcherQueue dispatcher, Func> function) + { + var completionSource = new TaskCompletionSource(); + + var enqueued = dispatcher.TryEnqueue(DispatcherQueuePriority.Normal, async void () => + { + try + { + var result = await function(); + completionSource.SetResult(result); + } + catch (Exception ex) + { + completionSource.SetException(ex); + } }); - var bitmapImage = await completionSource.Task; + if (!enqueued) + { + completionSource.SetException(new InvalidOperationException("Failed to enqueue the operation on the UI dispatcher")); + } - return bitmapImage; + return completionSource.Task; } }