From b898e4f4092bc910e634c8a586da0cb19545c983 Mon Sep 17 00:00:00 2001 From: josephcy95 Date: Wed, 9 Sep 2026 07:18:43 +0800 Subject: [PATCH 1/2] fix(windows): avoid WebView polling during voice input --- src-tauri/src/main.rs | 47 ++++++------------------------------------ src-tauri/src/strip.rs | 6 ++++++ 2 files changed, 12 insertions(+), 41 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 7ca1b2d..9eafbbc 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -395,47 +395,12 @@ fn main() { updater::spawn_check(&update_handle, false); }); } - // Session autosave (issue #18) + per-tab profile-dot refresh (#26): - // one 4s timer. `persist` captures windows/tabs so a frame move or - // SPA navigation (no structural event) is reflected; the profile - // sweep re-reads each strip tab's cookie so a profile switched - // *inside* a tab (an SPA re-render) repaints its dot. Both are cheap - // and only act on a real change. - { - let sess_handle = handle.clone(); - std::thread::spawn(move || loop { - std::thread::sleep(std::time::Duration::from_secs(4)); - // Skip this tick's webview work while a native menu modal - // loop is up (the strip's "⋯" popup). Both `persist` (reads - // each tab's URL) and `recapture_profiles` (reads each tab's - // cookie) marshal into the webviews via the runtime - // dispatcher; on Windows that would re-enter the main thread - // from inside the popup's `TrackPopupMenu` modal loop and - // deadlock the UI (#33). The next tick (once the menu closes) - // catches up — both are idempotent and only act on a change. - if sess_handle - .state::() - .menu_open - .load(std::sync::atomic::Ordering::SeqCst) - { - continue; - } - session::persist(&sess_handle); - if strip::enabled() { - strip::recapture_profiles(&sess_handle); - } - // Busy re-poll (issue #74): hidden webviews throttle their - // timers, so BUSY_REPORTER's 700ms interval can go quiet in - // a background tab and leave the spinner stale after the - // session finishes. An eval executes regardless of timer - // throttling and re-reads the live S.busy; the reporter's - // debounce means this emits only on an actual change. - windows::eval_all_content( - &sess_handle, - "window.__hermesReportBusy && window.__hermesReportBusy();", - ); - }); - } + // Windows/WebView2 compatibility: do not periodically poll child + // webviews while a focused input may be receiving synthetic text + // from voice-dictation tools. Synchronous URL/cookie/eval calls can + // re-enter WebView2's UI thread and freeze the host window. + // Session persistence remains event-driven (tab/window/navigation + // changes), and profile state is refreshed on explicit page events. // Chrome poller — the Tauri stand-in for the Swift app's // tabbedWindows KVO: keeps webview layout + tabbed class + // traffic-light var in sync with native tab-bar/fullscreen diff --git a/src-tauri/src/strip.rs b/src-tauri/src/strip.rs index a100364..4447d6a 100644 --- a/src-tauri/src/strip.rs +++ b/src-tauri/src/strip.rs @@ -1283,6 +1283,12 @@ pub fn profile_from_cookies(cookies: &[Cookie<'static>]) -> Option { /// session (#18). Runs on a worker thread — the cookie read marshals via the /// dispatcher, so it must not be called from inside a wry callout (wry#583). fn capture_tab_profile(app: &AppHandle, window_label: &str, tab_label: &str) { + // Avoid synchronous cookie reads from the desktop wrapper. WebView2 cookie + // reads can deadlock while accessibility/voice-typing software is injecting + // text into a focused WebView. Profile dots still update on fresh navigation. + let _ = (app, window_label, tab_label); + return; + // Guard the cookie read at the CHOKEPOINT, not just at the periodic sweep // (#33). `wv.cookies()` is a synchronous WebView2 read that marshals onto — // and pumps a nested loop on — the main thread. While the strip's "⋯" popup From 016b9d8525df7716ff0f15bed0a2431add85758f Mon Sep 17 00:00:00 2001 From: josephcy95 Date: Fri, 27 Nov 2026 15:53:55 +0800 Subject: [PATCH 2/2] fix(windows): scope WebView polling workaround to Windows --- src-tauri/src/main.rs | 53 +++++++++++++++++++--- src-tauri/src/strip.rs | 100 ++++++++++++++++++++++------------------- 2 files changed, 101 insertions(+), 52 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 9eafbbc..32126a0 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -395,12 +395,53 @@ fn main() { updater::spawn_check(&update_handle, false); }); } - // Windows/WebView2 compatibility: do not periodically poll child - // webviews while a focused input may be receiving synthetic text - // from voice-dictation tools. Synchronous URL/cookie/eval calls can - // re-enter WebView2's UI thread and freeze the host window. - // Session persistence remains event-driven (tab/window/navigation - // changes), and profile state is refreshed on explicit page events. + // Session autosave (issue #18) + per-tab profile-dot refresh (#26): + // one 4s timer. `persist` captures windows/tabs so a frame move or + // SPA navigation (no structural event) is reflected; the profile + // sweep re-reads each strip tab's cookie so a profile switched + // *inside* a tab (an SPA re-render) repaints its dot. Both are cheap + // and only act on a real change. + // + // Disabled on Windows: periodic synchronous WebView2 URL/cookie/eval + // calls can re-enter WebView2's UI thread while voice-dictation + // tools inject text into a focused input and deadlock the host + // window. Session persistence remains event-driven on Windows. + #[cfg(not(target_os = "windows"))] + { + let sess_handle = handle.clone(); + std::thread::spawn(move || loop { + std::thread::sleep(std::time::Duration::from_secs(4)); + // Skip this tick's webview work while a native menu modal + // loop is up (the strip's "⋯" popup). Both `persist` (reads + // each tab's URL) and `recapture_profiles` (reads each tab's + // cookie) marshal into the webviews via the runtime + // dispatcher; on Windows that would re-enter the main thread + // from inside the popup's `TrackPopupMenu` modal loop and + // deadlock the UI (#33). The next tick (once the menu closes) + // catches up — both are idempotent and only act on a change. + if sess_handle + .state::() + .menu_open + .load(std::sync::atomic::Ordering::SeqCst) + { + continue; + } + session::persist(&sess_handle); + if strip::enabled() { + strip::recapture_profiles(&sess_handle); + } + // Busy re-poll (issue #74): hidden webviews throttle their + // timers, so BUSY_REPORTER's 700ms interval can go quiet in + // a background tab and leave the spinner stale after the + // session finishes. An eval executes regardless of timer + // throttling and re-reads the live S.busy; the reporter's + // debounce means this emits only on an actual change. + windows::eval_all_content( + &sess_handle, + "window.__hermesReportBusy && window.__hermesReportBusy();", + ); + }); + } // Chrome poller — the Tauri stand-in for the Swift app's // tabbedWindows KVO: keeps webview layout + tabbed class + // traffic-light var in sync with native tab-bar/fullscreen diff --git a/src-tauri/src/strip.rs b/src-tauri/src/strip.rs index 4447d6a..16aa495 100644 --- a/src-tauri/src/strip.rs +++ b/src-tauri/src/strip.rs @@ -1283,58 +1283,65 @@ pub fn profile_from_cookies(cookies: &[Cookie<'static>]) -> Option { /// session (#18). Runs on a worker thread — the cookie read marshals via the /// dispatcher, so it must not be called from inside a wry callout (wry#583). fn capture_tab_profile(app: &AppHandle, window_label: &str, tab_label: &str) { - // Avoid synchronous cookie reads from the desktop wrapper. WebView2 cookie - // reads can deadlock while accessibility/voice-typing software is injecting - // text into a focused WebView. Profile dots still update on fresh navigation. - let _ = (app, window_label, tab_label); - return; - - // Guard the cookie read at the CHOKEPOINT, not just at the periodic sweep - // (#33). `wv.cookies()` is a synchronous WebView2 read that marshals onto — - // and pumps a nested loop on — the main thread. While the strip's "⋯" popup - // owns the main thread (TrackPopupMenu modal loop), that re-entry deadlocks - // the UI. recapture_profiles guards itself, but capture_tab_profile is ALSO - // reached from the `route` bridge handler (recapture_tab_profile, fired by a - // background SSE-driven navigation) and on_page_load — both unguarded - // otherwise. Guarding here covers every caller; the value is re-read on the - // next route/sweep once the menu closes. - if app - .state::() - .menu_open - .load(std::sync::atomic::Ordering::SeqCst) + #[cfg(target_os = "windows")] { - return; + // WebView2 synchronous cookie reads can re-enter the UI thread and + // deadlock while accessibility/voice-dictation software is injecting + // text into a focused WebView. Skip the read on Windows; the existing + // menu_open chokepoint guard (#33) covered the popup case, this covers + // the AX-injection case. + let _ = (app, window_label, tab_label); } - let Some(win) = app.windows().get(window_label).cloned() else { - return; - }; - let Some(wv) = find_webview(&win, tab_label) else { - return; - }; - let profile = match wv.cookies() { - Ok(cs) => profile_from_cookies(&cs), - Err(e) => { - log::debug!("strip: profile read failed for {tab_label}: {e}"); + + #[cfg(not(target_os = "windows"))] + { + // Guard the cookie read at the CHOKEPOINT, not just at the periodic sweep + // (#33). `wv.cookies()` is a synchronous WebView2 read that marshals onto — + // and pumps a nested loop on — the main thread. While the strip's "⋯" popup + // owns the main thread (TrackPopupMenu modal loop), that re-entry deadlocks + // the UI. recapture_profiles guards itself, but capture_tab_profile is ALSO + // reached from the `route` bridge handler (recapture_tab_profile, fired by a + // background SSE-driven navigation) and on_page_load — both unguarded + // otherwise. Guarding here covers every caller; the value is re-read on the + // next route/sweep once the menu closes. + if app + .state::() + .menu_open + .load(std::sync::atomic::Ordering::SeqCst) + { return; } - }; - let changed = { - let state = app.state::(); - let mut strip = state.strip.lock().unwrap(); - match strip - .get_mut(window_label) - .and_then(|e| e.tabs.iter_mut().find(|t| t.label == tab_label)) - { - Some(tab) if tab.profile != profile => { - tab.profile = profile; - true + let Some(win) = app.windows().get(window_label).cloned() else { + return; + }; + let Some(wv) = find_webview(&win, tab_label) else { + return; + }; + let profile = match wv.cookies() { + Ok(cs) => profile_from_cookies(&cs), + Err(e) => { + log::debug!("strip: profile read failed for {tab_label}: {e}"); + return; } - _ => false, + }; + let changed = { + let state = app.state::(); + let mut strip = state.strip.lock().unwrap(); + match strip + .get_mut(window_label) + .and_then(|e| e.tabs.iter_mut().find(|t| t.label == tab_label)) + { + Some(tab) if tab.profile != profile => { + tab.profile = profile; + true + } + _ => false, + } + }; + if changed { + emit_tabs(app, window_label); + crate::session::persist(app); } - }; - if changed { - emit_tabs(app, window_label); - crate::session::persist(app); } } @@ -1345,6 +1352,7 @@ fn capture_tab_profile(app: &AppHandle, window_label: &str, tab_label: &str) { /// periodic sweep plus a re-capture on tab activation covers both without a /// WebUI signal. `capture_tab_profile` only emits when a value actually /// changed, so this is cheap. Runs on a worker thread (cookie reads marshal). +#[cfg(not(target_os = "windows"))] pub fn recapture_profiles(app: &AppHandle) { // Cookie reads marshal into each webview via the runtime dispatcher. If a // native menu modal loop is up (the strip's "⋯" popup), that marshal would