Repository navigation
fix(runtime): don't pump messages inside XAML's dispatcher; keep Application.Current an Application - #58
Merged
Conversation
…ication.Current an Application WinUI 3 apps froze a few seconds after they started rendering every frame: timers, requestAnimationFrame and native callbacks all stopped while the UI thread sat idle in GetMessage. WinUI's dispatcher is a Microsoft.UI.Dispatching.DispatcherQueue, so init_ui_dispatcher finds no Windows.System.DispatcherQueue and makes its own controller. needs_win32_pump() was then true under XAML too, and runtime_pump_timers (and the napi engines' host pump) drained the thread's messages with PeekMessage/DispatchMessage from inside the host's DispatcherQueue work item. Dispatching the queues' wake messages re-entrantly can lose a wake, after which XAML's queue, its timers and the runtime's own queue never run again. A frame loop makes it happen within seconds. host_pumps_messages() now detects a XAML host (Microsoft.UI.Xaml.dll loaded), and the host pumps leave messages to XAML's loop there. Console executables and workers keep pumping. Also: Application.Current came back as Microsoft.UI.Xaml. IApplicationOverrides, with no Exit, so core could never end the app when its last window closed. The template's C# App has no WinMD, so CsWinRT reports one of its interfaces as the runtime class name. resolve_runtime_class no longer swaps a declared class for an interface name.
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
WinUI 3 apps freeze. A few seconds after an app starts rendering every frame, timers,
requestAnimationFrameand native callbacks (such as afetchcompleting) all stop for good. The window never recovers, and the UI thread sits idle inGetMessage.The process outlives its window.
Application.Currentcame back asMicrosoft.UI.Xaml.IApplicationOverrides, which only hasOnLaunched. So core'sApplication.Current.Exit()couldn't work.Cause
Freeze:
Microsoft.UI.Dispatching.DispatcherQueue, soinit_ui_dispatcherfinds noWindows.System.DispatcherQueueand creates its own controller.needs_win32_pump()true under XAML too.runtime_pump_timers(classic V8) andhost_abi::pump_once(napi engines) then drained the thread's messages withPeekMessage/DispatchMessagewhile running inside the host'sDispatcherQueuework item.DispatcherQueueTimerheartbeat and the runtime's own queue never run again.Application.Current:App : Applicationhas no WinMD entry, so CsWinRT reports one of its interfaces as the runtime class name.resolve_runtime_classaccepted that name and replaced the declaredApplicationclass with it.Fix
ui_dispatcher::host_pumps_messages()is true whenMicrosoft.UI.Xaml.dllis loaded. It's settled on the first call, and every caller is a pump that a WinUI host only starts afterApplication.Start.runtime_pump_timersandhost_abi::pump_onceleave Win32 messages to XAML's loop in that case.event_loop::run_event_loop(console executables) and worker threads keep pumping as before.resolve_runtime_classno longer replaces a declared class with an interface name. An interface name can still replace a declared interface or an unknown type.Testing
Tested on a WinUI 3 app (a pixi.js WebGPU game) with the classic V8
release-with-devtoolsbuild:PeekMessageWbeing called fromPumpTimersinside theSchedulePumpwork item. Making those calls return FALSE under the debugger removed the stall.Application.Current:Not run: the new
console_host_keeps_pumping_messagesunit test, and builds of the Hermes, QuickJS and JSC engines. Those engines share the changedhost_abi::pump_once.Follow-up (separate PR)
defer_on_ui_threadandhandle_worker_pushes_messagesstill key onneeds_win32_pump(), so under WinUI they act as if there's no XAML host.