Compare commits

...

3 Commits

4 changed files with 140 additions and 3 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 as System.Runtime.InteropServices.COMException))
if (ExceptionHelper.IsRecoverableDwmCompositionException(e))
{
var logger = LogManager.GetLogger(LoggerName);
logger.Error($"From {(isNotUIThread ? "non" : string.Empty)} UI thread's exception: {ExceptionFormatter.FormatException(e)}");

View File

@@ -22,6 +22,13 @@ namespace PowerLauncher.Helper
/// </summary>
internal static bool IsRecoverableDwmCompositionException(Exception exception)
{
// Unwrap TargetInvocationException: WPF raises theme-change events via reflection,
// so DWM COMExceptions (0x80263001) surface as TargetInvocationException.InnerException.
if (exception is System.Reflection.TargetInvocationException tie && tie.InnerException != null)
{
return IsRecoverableDwmCompositionException(tie.InnerException);
}
if (exception is not COMException comException)
{
return false;

View File

@@ -160,12 +160,14 @@ namespace PowerLauncher.Helper
return;
}
catch (COMException ex) when (ExceptionHelper.IsRecoverableDwmCompositionException(ex))
catch (Exception ex) when (ExceptionHelper.IsRecoverableDwmCompositionException(ex))
{
// Unwrap TargetInvocationException to extract the DWM HRESULT for logging.
var dwmEx = ex is System.Reflection.TargetInvocationException { InnerException: COMException inner } ? inner : ex as COMException;
switch (attempt)
{
case 1:
Log.Warn($"Desktop composition is disabled (HRESULT: 0x{ex.HResult:X}). Scheduling retries for theme update.", typeof(ThemeManager));
Log.Warn($"Desktop composition is disabled (HRESULT: 0x{dwmEx?.HResult ?? ex.HResult:X}). Scheduling retries for theme update.", typeof(ThemeManager));
delayMs = InitialDelayMs;
break;
case < maxAttempts:

View File

@@ -0,0 +1,128 @@
// 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]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Test code intentionally constructs COMException instances to verify exception-handling logic")]
public class ExceptionHelperTest
{
private const int DwmCompositionDisabledHResult = unchecked((int)0x80263001);
private const int StatusMessageLostHResult = unchecked((int)0xD0000701);
private const int UnrelatedHResult = unchecked((int)0x80004005); // E_FAIL
/// <summary>
/// A direct <see cref="COMException"/> with HRESULT 0x80263001 (DWM_E_COMPOSITIONDISABLED)
/// must be identified as recoverable.
/// </summary>
[TestMethod]
public void IsRecoverableDwmCompositionException_DirectCOMException_ReturnsTrue()
{
var ex = new COMException("Desktop composition is disabled", DwmCompositionDisabledHResult);
Assert.IsTrue(ExceptionHelper.IsRecoverableDwmCompositionException(ex));
}
/// <summary>
/// A <see cref="TargetInvocationException"/> wrapping the DWM <see cref="COMException"/> must
/// be identified as recoverable. WPF raises theme-change events via reflection, so the
/// 0x80263001 <see cref="COMException"/> surfaces wrapped in a
/// <see cref="TargetInvocationException"/>.
/// </summary>
[TestMethod]
public void IsRecoverableDwmCompositionException_TargetInvocationWrappingDwmCOMException_ReturnsTrue()
{
var inner = new COMException("Desktop composition is disabled", DwmCompositionDisabledHResult);
var ex = new TargetInvocationException(inner);
Assert.IsTrue(ExceptionHelper.IsRecoverableDwmCompositionException(ex));
}
/// <summary>
/// A <see cref="TargetInvocationException"/> wrapping an unrelated exception must NOT be
/// identified as recoverable.
/// </summary>
[TestMethod]
public void IsRecoverableDwmCompositionException_TargetInvocationWrappingUnrelatedException_ReturnsFalse()
{
var inner = new COMException("Some other COM error", UnrelatedHResult);
var ex = new TargetInvocationException(inner);
Assert.IsFalse(ExceptionHelper.IsRecoverableDwmCompositionException(ex));
}
/// <summary>
/// A <see cref="TargetInvocationException"/> with a null inner exception must NOT be
/// identified as recoverable.
/// </summary>
[TestMethod]
public void IsRecoverableDwmCompositionException_TargetInvocationWithNullInner_ReturnsFalse()
{
var ex = new TargetInvocationException(null);
Assert.IsFalse(ExceptionHelper.IsRecoverableDwmCompositionException(ex));
}
/// <summary>
/// A <see cref="COMException"/> with HRESULT 0xD0000701 (STATUS_MESSAGE_LOST) and Source
/// "PresentationFramework" must be identified as recoverable.
/// </summary>
[TestMethod]
public void IsRecoverableDwmCompositionException_StatusMessageLostFromPresentationFramework_ReturnsTrue()
{
var ex = new COMException("Status message lost", StatusMessageLostHResult)
{
Source = "PresentationFramework",
};
Assert.IsTrue(ExceptionHelper.IsRecoverableDwmCompositionException(ex));
}
/// <summary>
/// A <see cref="COMException"/> with HRESULT 0xD0000701 (STATUS_MESSAGE_LOST) but a
/// non-PresentationFramework source must NOT be identified as recoverable.
/// </summary>
[TestMethod]
public void IsRecoverableDwmCompositionException_StatusMessageLostFromOtherSource_ReturnsFalse()
{
var ex = new COMException("Status message lost", StatusMessageLostHResult)
{
Source = "SomeOtherAssembly",
};
Assert.IsFalse(ExceptionHelper.IsRecoverableDwmCompositionException(ex));
}
/// <summary>
/// A <see cref="COMException"/> with an unrelated HRESULT must NOT be identified as
/// recoverable.
/// </summary>
[TestMethod]
public void IsRecoverableDwmCompositionException_UnrelatedCOMException_ReturnsFalse()
{
var ex = new COMException("Some other COM error", UnrelatedHResult);
Assert.IsFalse(ExceptionHelper.IsRecoverableDwmCompositionException(ex));
}
/// <summary>
/// A non-COM exception must NOT be identified as recoverable.
/// </summary>
[TestMethod]
public void IsRecoverableDwmCompositionException_NonCOMException_ReturnsFalse()
{
var ex = new InvalidOperationException("Not a COM exception");
Assert.IsFalse(ExceptionHelper.IsRecoverableDwmCompositionException(ex));
}
/// <summary>
/// Null input must NOT be identified as recoverable.
/// </summary>
[TestMethod]
public void IsRecoverableDwmCompositionException_NullException_ReturnsFalse()
{
Assert.IsFalse(ExceptionHelper.IsRecoverableDwmCompositionException(null));
}
}