Compare commits

..

3 Commits

Author SHA1 Message Date
Muyuan Li (from Dev Box)
f9679b937d Address review: handle NavigationFailed gracefully without rethrowing
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-14 16:20:42 +08:00
copilot-swe-agent[bot]
36300d3c75 Fix NullReferenceException in Frame_NavigationFailed when e.Exception is null
Agent-Logs-Url: https://github.com/microsoft/PowerToys/sessions/af982aa1-504b-47f1-9ec0-93b29602b2af

Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
2026-04-29 08:49:32 +00:00
copilot-swe-agent[bot]
1cde68ae04 Initial plan 2026-04-29 08:48:31 +00:00
6 changed files with 31 additions and 102 deletions

View File

@@ -61,7 +61,7 @@ namespace PowerLauncher.Helper
// Many bug reports because users see the "Report problem UI" after "the" crash with System.Runtime.InteropServices.COMException 0xD0000701 or 0x80263001.
// However, displaying this "Report problem UI" during WPF crashes, especially when DWM composition is changing, is not ideal; some users reported it hangs for up to a minute before the "Report problem UI" appears.
// This change modifies the behavior to log the exception instead of showing the "Report problem UI".
if (ExceptionHelper.IsRecoverableDwmCompositionException(e))
if (ExceptionHelper.IsRecoverableDwmCompositionException(e as System.Runtime.InteropServices.COMException))
{
var logger = LogManager.GetLogger(LoggerName);
logger.Error($"From {(isNotUIThread ? "non" : string.Empty)} UI thread's exception: {ExceptionFormatter.FormatException(e)}");

View File

@@ -19,36 +19,26 @@ namespace PowerLauncher.Helper
/// <summary>
/// Returns true if the exception is a recoverable DWM composition exception.
/// Also checks inner exceptions to handle cases where a DWM error is wrapped by another exception type.
/// </summary>
internal static bool IsRecoverableDwmCompositionException(Exception exception)
{
if (exception == null)
if (exception is not COMException comException)
{
return false;
}
// Walk the exception chain (up to a reasonable depth) to detect DWM composition errors at any level
const int maxDepth = 10;
var current = exception;
for (var depth = 0; current != null && depth < maxDepth; depth++, current = current.InnerException)
if (comException.HResult is DWM_E_COMPOSITIONDISABLED)
{
if (current is COMException comException)
{
if (comException.HResult is DWM_E_COMPOSITIONDISABLED)
{
return true;
}
if (comException.HResult is STATUS_MESSAGE_LOST_HR && comException.Source == PresentationFrameworkExceptionSource)
{
return true;
}
}
return true;
}
// Check for common DWM composition changed patterns in the stack trace of the outermost exception
var stackTrace = exception.StackTrace;
if (comException.HResult is STATUS_MESSAGE_LOST_HR && comException.Source == PresentationFrameworkExceptionSource)
{
return true;
}
// Check for common DWM composition changed patterns in the stack trace
var stackTrace = comException.StackTrace;
return !string.IsNullOrEmpty(stackTrace) &&
stackTrace.Contains("DwmCompositionChanged");
}

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
@@ -159,16 +160,12 @@ namespace PowerLauncher.Helper
return;
}
catch (Exception ex) when (ExceptionHelper.IsRecoverableDwmCompositionException(ex))
catch (COMException ex) when (ExceptionHelper.IsRecoverableDwmCompositionException(ex))
{
var hresult = ex is System.Reflection.TargetInvocationException { InnerException: not null }
? ex.InnerException.HResult
: ex.HResult;
switch (attempt)
{
case 1:
Log.Warn($"Desktop composition is disabled (HRESULT: 0x{hresult:X}). Scheduling retries for theme update.", typeof(ThemeManager));
Log.Warn($"Desktop composition is disabled (HRESULT: 0x{ex.HResult:X}). Scheduling retries for theme update.", typeof(ThemeManager));
delayMs = InitialDelayMs;
break;
case < maxAttempts:

View File

@@ -196,17 +196,10 @@ namespace PowerLauncher
if (OSVersionHelper.IsGreaterThanWindows11_21H2())
{
// ResizeMode="NoResize" removes rounded corners. So force them to rounded.
try
{
IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle();
DWMWINDOWATTRIBUTE attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE;
DWM_WINDOW_CORNER_PREFERENCE preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND;
DwmSetWindowAttribute(hWnd, attribute, ref preference, sizeof(uint));
}
catch (Exception ex) when (ExceptionHelper.IsRecoverableDwmCompositionException(ex))
{
Log.Warn($"Desktop composition is disabled. Unable to set window corner preference (cosmetic only). HRESULT: 0x{ex.HResult:X}", typeof(MainWindow));
}
IntPtr hWnd = new WindowInteropHelper(GetWindow(this)).EnsureHandle();
DWMWINDOWATTRIBUTE attribute = DWMWINDOWATTRIBUTE.DWMWA_WINDOW_CORNER_PREFERENCE;
DWM_WINDOW_CORNER_PREFERENCE preference = DWM_WINDOW_CORNER_PREFERENCE.DWMWCP_ROUND;
DwmSetWindowAttribute(hWnd, attribute, ref preference, sizeof(uint));
}
else
{

View File

@@ -1,63 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PowerLauncher.Helper;
namespace Wox.Test;
[TestClass]
public class ExceptionHelperTest
{
private const int DwmCompositionDisabledHResult = unchecked((int)0x80263001);
[TestMethod]
public void IsRecoverableDwmCompositionException_ReturnsTrue_ForDirectRecoverableComException()
{
var exception = CreateComException(DwmCompositionDisabledHResult);
Assert.IsTrue(ExceptionHelper.IsRecoverableDwmCompositionException(exception));
}
[TestMethod]
public void IsRecoverableDwmCompositionException_ReturnsTrue_ForWrappedRecoverableComException()
{
var innerException = CreateComException(DwmCompositionDisabledHResult);
var exception = new TargetInvocationException(innerException);
Assert.IsTrue(ExceptionHelper.IsRecoverableDwmCompositionException(exception));
}
[TestMethod]
public void IsRecoverableDwmCompositionException_ReturnsFalse_ForWrappedNonRecoverableException()
{
var exception = new TargetInvocationException(new InvalidOperationException("Not a DWM composition failure."));
Assert.IsFalse(ExceptionHelper.IsRecoverableDwmCompositionException(exception));
}
[TestMethod]
public void IsRecoverableDwmCompositionException_ReturnsTrue_ForDwmCompositionChangedInStackTrace()
{
var exception = Assert.ThrowsException<InvalidOperationException>(DwmCompositionChangedThrower);
Assert.IsTrue(ExceptionHelper.IsRecoverableDwmCompositionException(exception));
}
private static COMException CreateComException(int hresult)
{
var exception = Marshal.GetExceptionForHR(hresult);
Assert.IsNotNull(exception);
Assert.IsInstanceOfType<COMException>(exception);
return (COMException)exception;
}
private static void DwmCompositionChangedThrower()
{
throw new InvalidOperationException("Stack trace fallback path.");
}
}

View File

@@ -9,6 +9,7 @@ using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Helpers;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
@@ -135,7 +136,18 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private void Frame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
throw e.Exception;
var sourcePage = e.SourcePageType?.FullName ?? "<unknown>";
if (e.Exception is null)
{
Logger.LogWarning($"Navigation to '{sourcePage}' failed without an exception.");
}
else
{
Logger.LogError($"Navigation to '{sourcePage}' failed.", e.Exception);
}
e.Handled = true;
}
private void Frame_Navigated(object sender, NavigationEventArgs e)