Repository navigation
Description
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
- Start the tray app
- Click tray icon - menu appears (works)
- Click away to dismiss menu
- Wait a few seconds OR open Settings window and close it
- 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:
- GC collecting something the native windowing layer still references after idle
- COM apartment threading issue when tray icon callback fires
- WindowEx/WinUIEx issue with rapid window creation/destruction
- Windows App SDK bug with unpackaged apps and system tray interaction
Potential Solutions to Explore
- MenuFlyout approach: Use
e.Flyout = BuildMenuFlyout()in tray callbacks instead of custom window - Keep-alive window: Create an invisible window to keep WinUI runtime active
- GC.KeepAlive: Pin critical objects to prevent collection
Files of Interest
src/OpenClaw.Tray.WinUI/App.xaml.cs- Main app, tray icon handlingsrc/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
Assignees
Labels
Type
Fields
Priority
Activity
WIP: Attempting MenuFlyout approach to fix tray crash
shanselman commentedon Jan 30, 2026
Deep Analysis for Cloud Agent
The Core Problem
WinUI 3 was designed assuming a main window exists. This tray-only app (no main window) is an edge case where:
Exception Code Analysis
0xe0464645- The bytes46 46 45spell "FFE" which is the CLR exception marker. This means a .NET exception propagated into native code and wasn't caught. The crash is happening during native window initialization, after our managed code callsnew TrayMenuWindow()but before control returns.Why Window Reuse Showed Black Square
When we tried
Hide()instead ofClose(), the window reappeared as black. This suggests:This is actually a clue - the window DOES exist, but its rendering pipeline is broken.
WinUIEx TrayIcon Architecture
WinUIEx's
TrayIconcreates an internal hidden message-only window to receiveShell_NotifyIconcallbacks. When user clicks, this fires on whatever thread the message pump runs on. The question: is this thread the same STA thread where we created the DispatcherQueue?Most Promising Solutions (Priority Order)
1. MenuFlyout Approach (Partially Implemented)
Use
e.Flyout = BuildMenuFlyout()in tray callbacks. This attaches a flyout to WinUIEx's internal window instead of creating our own. No window creation = no crash.Status: I started this in App.xaml.cs (
BuildTrayMenuFlyoutmethod). Needs testing.2. Keep-Alive Hidden Window
Create a hidden window at startup that stays alive forever:
This keeps the WinUI runtime properly initialized.
3. Pin DispatcherQueue Reference
The crash might be GC collecting the DispatcherQueue after idle:
4. Investigate WinUIEx Threading
Look at WinUIEx source: https://github.com/dotMorten/WinUIEx
Check
TrayIcon.csfor how it handles the Shell_NotifyIcon callback threading.Key Files to Modify
src/OpenClaw.Tray.WinUI/App.xaml.cslines 178-290 (tray handling)src/OpenClaw.Tray.WinUI/Windows/TrayMenuWindow.xaml.cs(popup window)Testing Approach
shanselman commentedon Feb 8, 2026
Fixed in v0.3.0. The crash was caused by WinUI 3's MenuFlyout requiring a valid UIElement context when showing from a tray icon callback. The fix uses a TrayMenuAnchorWindow (invisible 1x1 window) to anchor the MenuFlyout, preventing the native crash in Microsoft.UI.Windowing.Core.dll. See \src/OpenClaw.Tray.WinUI/Windows/TrayMenuAnchorWindow.xaml.cs\ and the updated tray menu code in \App.xaml.cs. Has been stable through extensive testing across multiple sessions.
Merge pull request #3 from NichUK/copilot/sub-pr-2
security: harden path and token parsing in ExecCommandResolver
audio: address rubber-duck review (Highs openclaw#2-openclaw#5, Mediums
audio: don't drop final utterance on stop or timeout; bound transcrip…
fix(quicksend): resolve gateway client per-send to avoid stale-snapsh…
fix(onboarding): gate easy-button local setup behind existing-config …
audio: Whisper STT + Piper TTS as MCP-callable node capabilities (#288)
fix(uninstall): clear GatewayRegistry local records + gate Success on…
fix(connection): Hanselman review findings — defensive guards on Ensu…
Rebuild Debug page as task-oriented Diagnostics page (rebased on sing…
Hanselman Round 1 fixes (13/14, skipping openclaw#12)