From 2de5d806ae2529fd801437310fa780511b22c216 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Sat, 21 Nov 2026 14:45:59 -0400 Subject: [PATCH] fix(runtime): treat a WinUI host as a XAML host everywhere needs_win32_pump() was true under WinUI 3 as well (the runtime makes its own DispatcherQueueController there, since WinUI's dispatcher isn't a Windows.System.DispatcherQueue), so code keyed on it acted as if there were no XAML host: - defer_microtask_drain never deferred: JS delegates, rAF callbacks and the other native-to-JS entry points drained Promise continuations inline, inside XAML callouts such as the render walk (the 0xC000027B fail-fast the deferral exists to prevent) - __nsWorkerPushesMessages was false: every worker.postMessage blocked the UI thread polling for replies (up to 100 x 5 ms), though replies are posted to the UI thread's dispatcher, which XAML's loop runs needs_win32_pump() is now false when host_pumps_messages(), so all of these follow from one definition, and runtime_pump_timers needs no separate check. --- nativescript/src/lib.rs | 3 +-- runtime/src/ui_dispatcher.rs | 15 +++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/nativescript/src/lib.rs b/nativescript/src/lib.rs index fd6cc07..3969bdb 100644 --- a/nativescript/src/lib.rs +++ b/nativescript/src/lib.rs @@ -443,8 +443,7 @@ pub extern "C" fn runtime_pump_timers() { runtime::node_api::drain(); // This frame's requestAnimationFrame callbacks, then their microtasks. runtime::animation_frames::pump(); - // (a WinUI host has a runtime-made dispatcher too: see `host_pumps_messages`) - if runtime::ui_dispatcher::needs_win32_pump() && !runtime::ui_dispatcher::host_pumps_messages() { + if runtime::ui_dispatcher::needs_win32_pump() { runtime::pump_messages(); } else { runtime::pump_dispatcher(); diff --git a/runtime/src/ui_dispatcher.rs b/runtime/src/ui_dispatcher.rs index 56fafd2..eb43be1 100644 --- a/runtime/src/ui_dispatcher.rs +++ b/runtime/src/ui_dispatcher.rs @@ -210,11 +210,14 @@ pub fn get_ui_thread_rust_tid() -> Option { UI_THREAD_ID.get().map(|id| format!("{:?}", id)) } -/// Returns true when the runtime owns its own `DispatcherQueueController` (i.e. no XAML -/// host is present). In that case the caller is responsible for pumping Win32 messages -/// so that WinRT async `Completed` callbacks can fire. +/// Returns true when the runtime owns its own `DispatcherQueueController` and no XAML host +/// runs the thread's message loop (console hosts, tests). In that case the caller is responsible +/// for pumping Win32 messages so that WinRT async `Completed` callbacks can fire. +/// +/// Under WinUI the runtime owns a controller too (see `host_pumps_messages`), but XAML's loop +/// dispatches its work: the deferred microtask drain and pushed worker replies rely on that. pub fn needs_win32_pump() -> bool { - UI_DQ_CONTROLLER.get().is_some() + UI_DQ_CONTROLLER.get().is_some() && !host_pumps_messages() } /// True when a XAML host runs the UI thread's message loop, so the runtime's pump must not drain @@ -231,8 +234,8 @@ pub fn needs_win32_pump() -> bool { /// all stop while the UI thread sits idle in `GetMessage`. Console hosts have no such loop and /// keep pumping. /// -/// Settled on the first call. Every caller is a pump, and a WinUI host only starts pumping from -/// inside `Application.Start`, after XAML has loaded. +/// Settled on the first call. A WinUI host creates the runtime inside `Application.Start` (the +/// template does it in `OnLaunched`), so XAML has loaded before anything asks. pub fn host_pumps_messages() -> bool { static XAML_HOST: OnceLock = OnceLock::new(); *XAML_HOST.get_or_init(xaml_loaded)