Skip to content

WinUI tray menu crashes in Microsoft.UI.Windowing.Core.dll after idle/interaction #3

@shanselman

Description

@shanselman

Problem

The WinUI 3 tray app crashes intermittently when clicking the tray icon to show the menu. The crash occurs in the native WinUI windowing layer and bypasses all .NET exception handlers.

Crash Details

  • Faulting module: Microsoft.UI.Windowing.Core.dll (version 10.0.27108.1025)
  • Exception code: 0xe0464645 (CLR exception marker)
  • Fault offset: 0x000000000000a280 (consistent across all crashes)
  • Platform: Windows 11 ARM64
  • Windows App SDK: 1.8.250906003
  • WinUIEx: 2.9.0
  • .NET: 9.0 (targeting windows10.0.19041.0)

Reproduction Steps

  1. Start the tray app
  2. Click tray icon - menu appears (works)
  3. Click away to dismiss menu
  4. Wait a few seconds OR open Settings window and close it
  5. Click tray icon again - CRASH

The crash doesn't happen immediately but becomes more likely after:

  • The app has been idle for a period
  • Other windows have been opened/closed (like Settings)
  • Multiple menu show/hide cycles

What We've Tried

1. Window Reuse Pattern

Instead of creating/destroying TrayMenuWindow each click, tried reusing a single window:

  • Hide() instead of Close() on deactivation
  • ClearItems() and rebuild menu content
  • Result: Black square appeared instead of menu content

2. UI Thread Dispatching

Added checks to ensure tray icon callbacks dispatch to UI thread

  • Result: Still crashes

3. Crash Logging

Added UnhandledException, DomainUnhandledException, UnobservedTaskException handlers

  • Result: No crash log written - crash is in native code

4. Backdrop Removal

Removed DesktopAcrylicBackdrop from TrayMenuWindow

  • Result: Still crashes

5. Async Data Fetch with Timeout

Added brief wait for gateway data before showing menu

  • Result: Still crashes

Current Architecture

TrayMenuWindow extends WinUIEx.WindowEx, configured as popup-style (no title bar, always on top, closes on deactivation). ShowAtCursor() uses P/Invoke to position near taskbar.

App creates new TrayMenuWindow on each tray click, closes previous one first.

Hypothesis

The crash appears to be related to WinUI window lifecycle management:

  1. GC collecting something the native windowing layer still references after idle
  2. COM apartment threading issue when tray icon callback fires
  3. WindowEx/WinUIEx issue with rapid window creation/destruction
  4. Windows App SDK bug with unpackaged apps and system tray interaction

Potential Solutions to Explore

  1. MenuFlyout approach: Use e.Flyout = BuildMenuFlyout() in tray callbacks instead of custom window
  2. Keep-alive window: Create an invisible window to keep WinUI runtime active
  3. GC.KeepAlive: Pin critical objects to prevent collection

Files of Interest

  • src/OpenClaw.Tray.WinUI/App.xaml.cs - Main app, tray icon handling
  • src/OpenClaw.Tray.WinUI/Windows/TrayMenuWindow.xaml.cs - Popup menu window

Windows Event Log Entry

Faulting application name: OpenClaw.Tray.WinUI.exe
Faulting module name: Microsoft.UI.Windowing.Core.dll
Exception code: 0xe0464645
Fault offset: 0x000000000000a280

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions