dotnet / dotnet/wpf

Invalid monitor handle Win32 exception

Open
#11,608 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

Our crash reporting software reported an unhandled Win32 exception. Upon further inspection, the PresentationFramework native code wrappers throw an exception when looking up a monitor handle that is not null but not valid: https://github.com/dotnet/wpf/blob/239f8da8fbf8cf2a6cd0c793f0d02679bf4ccf6a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L4590
Working up the stack trace (attached below), there is a try finally block with the catch part missing, not sure if it was intentional given the lack of a break statement: https://github.com/dotnet/wpf/blob/239f8da8fbf8cf2a6cd0c793f0d02679bf4ccf6a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L4590
Despite being unable to reproduce it, there should be enough information here to understand why it crashed and how to fix it.

### Reproduction Steps

We discovered this via our crash reporting software, but are unable to reproduce it. All we know is that the user was minimizing and restoring the app a lot, and that the app was running for a long time (10 hours). We are pretty sure the action which caused the crash was a window minimize. The app was using WPF 8.0.0, but it seems that newer versions have not changed the relevant code. The app is not in the foreground at the time of the crash.

### Expected behavior

When native code indicates that a monitor handle is not valid, even if the monitor handle is not null, WPF should handle this at some point. Whatever operation needed a monitor should fall back to some reasonable behavior.

### Actual behavior

WPF checks that a monitor handle is not null before using it. If the monitor handle is not valid, it throws an exception. When an invalid, non-null monitor handle is returned, the exception goes unhandled and crashes the app.

### Regression?

Unsure

### Known Workarounds

None

### Impact

This caused a crash for a user. We've only seen it happen once, but we do not have many users at this point. The root cause may not be very common since we cannot reproduce it by messing with monitors while rapidly minimizing and restoring.

### Configuration

WPF: 8.0.0
.NET: 8.0.25
OS: Windows 11 (win-x64, build 26100)

We do not know if it is specific to this setup.

### Other information

Stack trace:
System.ComponentModel.Win32Exception: Invalid monitor handle.
at Point Window.TransformWorkAreaScreenArea(Point pt, TransformType transformType)()
at Rect Window.GetNormalRectDeviceUnits(IntPtr hwndHandle)()
at Rect Window.GetNormalRectLogicalUnits(IntPtr hwndHandle)()
at void Window.UpdateDimensionsToRestoreBounds()()
at bool Window.WmSizeChanged(IntPtr wParam)()
at IntPtr Window.WindowFilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)()
at IntPtr HwndWrapper.WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)()
at object ExceptionWrapper.InternalRealCall(Delegate callback, object args, int numArgs)()
at object ExceptionWrapper.TryCatchWhen(object source, Delegate callback, object args, int numArgs, Delegate catchHandler)()
at object Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, object args, int numArgs)()
at IntPtr HwndSubclass.SubclassWndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam)()
at IntPtr UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)()
at IntPtr HwndSubclass.DefWndProcWrapper(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam)()
at IntPtr UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)()
at IntPtr HwndSubclass.SubclassWndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam)()

I was able to follow the native code to determine that it occurs at line 5859: https://github.com/dotnet/wpf/blob/239f8da8fbf8cf2a6cd0c793f0d02679bf4ccf6a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L5859. That allows us to say that the handle is not null. ReactOS, a reverse engineer of Windows, has useful source code for reference.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.