From 7457ff5202396bb9097634775e78b0ded34e8b7f Mon Sep 17 00:00:00 2001 From: Ani <115020168+drawbyperpetual@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:09:00 +0200 Subject: [PATCH] [Settings]Fix crash on dashboard when KBM settings update (#33872) ## Summary of the Pull Request Fixed crash of Settings when KBM settings update by any mechanism. ## Detailed Description of the Pull Request / Additional comments To show updated KBM remappings on the dashboard, the Settings app watches for changes to the KBM settings file and reloads the file on change. Reloading the file can fail (most likely because the writing process takes an exclusive lock and isn't yet done writing) and the process crashes when this happens. This change guards against this. --- .../Settings.UI/ViewModels/DashboardViewModel.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs b/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs index 90f4640ea3..e5924efb9c 100644 --- a/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs +++ b/src/settings-ui/Settings.UI/ViewModels/DashboardViewModel.cs @@ -88,10 +88,17 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels private void LoadKBMSettingsFromJson() { - KeyboardManagerProfile kbmProfile = GetKBMProfile(); - _kbmItem.RemapKeys = kbmProfile?.RemapKeys.InProcessRemapKeys; - _kbmItem.RemapShortcuts = KeyboardManagerViewModel.CombineShortcutLists(kbmProfile?.RemapShortcuts.GlobalRemapShortcuts, kbmProfile?.RemapShortcuts.AppSpecificRemapShortcuts); - dispatcher.Invoke(new Action(() => UpdateKBMItems())); + try + { + KeyboardManagerProfile kbmProfile = GetKBMProfile(); + _kbmItem.RemapKeys = kbmProfile?.RemapKeys.InProcessRemapKeys; + _kbmItem.RemapShortcuts = KeyboardManagerViewModel.CombineShortcutLists(kbmProfile?.RemapShortcuts.GlobalRemapShortcuts, kbmProfile?.RemapShortcuts.AppSpecificRemapShortcuts); + dispatcher.Invoke(new Action(() => UpdateKBMItems())); + } + catch (Exception ex) + { + Logger.LogError($"Failed to load KBM settings: {ex.Message}"); + } } private void UpdateKBMItems()