fix(tauri): retry main-window lookup on Windows after SW_SHOW (#3A)
Summary
- Wraps the
app.get_webview_window(\"main\")lookup inshow_main_windowwith a 5×10ms bounded retry on Windows so the tray callback doesn't observeNoneduring the brief window where the Tauri runtime hasn't re-tracked the mainWebviewWindowrecord afterSW_SHOW. - Closes OPENHUMAN-TAURI-3A (~15 events) — the Sentry warn
[tray] failed to show main window from menu: main window not foundfires while the OS window has already come back visible, so the user sees the window restore but Sentry sees noise and the post-show polish chain (unminimize, Tauri-levelset_focus, CEFset_focus) is skipped. - Pure Windows-cfg-gated helper. macOS / Linux paths unchanged — the race is specific to the
SW_HIDEclose-to-tray flow on Windows.
Problem
On Windows, the app's close button doesn't destroy the main window — it routes through set_main_window_hidden(true) which uses Win32 EnumWindows + SW_HIDE on the Chrome_WidgetWin_1 HWND so the user can restore from the tray (PR #1583). CEF treats the hidden host as gone, so the Tauri runtime drops its WebviewWindow record for \"main\" while the window is in the SW_HIDE state.
When the user clicks the tray menu's Show window item (or left-clicks the tray icon), show_main_window runs the Windows branch:
set_main_window_hidden(false)issuesSW_SHOWon the raw HWND — the OS window becomes visible immediately.app.get_webview(\"main\")+webview.show()nudges the runtime to re-track.app.get_webview_window(\"main\")synchronously expects aSome(_)back.
Step 3 races step 2 because the runtime re-tracks the window on the next event-loop tick, not synchronously with SW_SHOW. The lookup returns None, the ? propagates \"main window not found\", and the tray callback emits:
[tray] failed to show main window from menu: main window not found
User effect today: the SW_SHOW already worked, so the window IS visible — but the post-show polish chain (Tauri-level window.show(), unminimize(), set_focus(), CEF set_focus() for keyboard routing) is skipped, and Sentry pages on what looks like a fatal tray failure even though the user got what they wanted.
15 occurrences observed on Windows 10/11 since 2026-05-09 (release 0.53.22 → 0.54.x), spread across at least one Foshan (CN), one Hyderabad (IN), and several US users. The CEF runtime + SW_HIDE close-to-tray code has not been touched on main since the issue was first seen, so the race is still live in 0.54.x.
Solution
Introduce get_main_webview_window_with_retry(app) next to show_main_window:
fn get_main_webview_window_with_retry(
app: &AppHandle<AppRuntime>,
) -> Option<tauri::WebviewWindow<AppRuntime>> {
#[cfg(target_os = \"windows\")]
{
const ATTEMPTS: usize = 5;
const BACKOFF: std::time::Duration = std::time::Duration::from_millis(10);
for attempt in 0..ATTEMPTS {
if let Some(window) = app.get_webview_window(\"main\") {
if attempt > 0 {
log::debug!(
\"[show_main_window] runtime re-tracked main window after {} retries\",
attempt
);
}
return Some(window);
}
if attempt + 1 < ATTEMPTS {
std::thread::sleep(BACKOFF);
}
}
None
}
#[cfg(not(target_os = \"windows\"))]
{
app.get_webview_window(\"main\")
}
}
show_main_window swaps the single-call lookup for get_main_webview_window_with_retry(app). Worst case: 50 ms of std::thread::sleep on the tray callback thread. The tray menu is already closed by the time the user clicks an item, so the small delay is invisible — the user sees SW_SHOW (window appears) immediately, and the focus/unminimize polish lands one tick late.
Hard cap at 5 attempts preserves the original error path if the runtime never re-tracks — that would be a real lifecycle bug worth a Sentry event, not a race worth papering over.
Non-Windows platforms route through the single-call path because:
- macOS close button routes through
app.hide()(PR #2049) — theWebviewWindowrecord stays intact. - Linux/X11 keeps the
WebviewWindowrecord acrossWM_DELETE_WINDOW— the close handler just hides without dropping the host.
Submission Checklist
If a section does not apply to this change, mark the item as
N/Awith a one-line reason. Do not delete items.
- [x] N/A: pure platform-cfg-gated retry helper around an existing Tauri API call. Tests added or updated (happy path + at least one failure / edge case) per Testing Strategy — the race is a runtime timing bug between SW_SHOW (raw HWND) and Tauri's event-loop re-track step, not a unit-testable code path. Validation plan in the Impact section below.
- [x] N/A: only added rustdoc + a Windows-cfg-gated branch + a one-line call-site swap. Diff coverage ≥ 80% — changed lines (Vitest + cargo-llvm-cov merged via
diff-cover) meet the gate enforced by.github/workflows/coverage.yml. - [x] N/A: behaviour-only change, no feature row impact. Coverage matrix updated — added/removed/renamed feature rows in
docs/TEST-COVERAGE-MATRIX.mdreflect this change. - [x] All affected feature IDs from the matrix are listed in the PR description under
## Related. - [x] No new external network dependencies introduced (mock backend used per Testing Strategy).
- [x] N/A: classifier-adjacent fix; no release-cut surface (tray show-from-menu was already in the release smoke). Manual smoke checklist updated if this touches release-cut surfaces (
docs/RELEASE-MANUAL-SMOKE.md). - [x] Linked issue closed via
Closes #NNNin the## Relatedsection.
Impact
- Runtime: on Windows, the tray Show window / tray icon left-click callback can now block its caller for up to 50 ms in the race window. The tray menu is closed by the time we reach this code path; no user-visible latency. macOS and Linux unchanged.
- Performance: negligible — 5×10ms is the worst case and only fires when the race triggers (which is exactly the case the current code paths emit a warn for). The fast path (window already re-tracked) is a single
get_webview_windowcall as before. - Security: no new attack surface; helper is private, single-purpose, and doesn't reach beyond
app.get_webview_window. - Migration: none.
- Compatibility: forward-compatible. If/when Tauri synchronously re-tracks the window after a host-visibility change (or our SW_SHOW path stops dropping the record at all), the retry loop short-circuits on the first attempt and the helper degrades to a no-op overhead.
- Validation plan (no unit test added — see Submission Checklist above):
- CI runs
cargo check --manifest-path app/src-tauri/Cargo.tomlandcargo clippy --manifest-path app/src-tauri/Cargo.toml -- -D warningson Windows. Both pass locally on macOS; CI Windows runner exercises the cfg-gated body. - Manual smoke on Windows: launch dev:app, close to tray, right-click tray → Show window, verify focus + unminimize land and no
[tray] failed to show main window from menu: main window not foundlands in stderr. Repeat with left-click tray icon. (I can't smoke this locally on macOS — flagging honestly.)
- CI runs
Related
- Closes OPENHUMAN-TAURI-3A
- Related context: PR #1583 (
feat(tauri): hide main window to tray on Windows close) introduced the SW_HIDE flow; PR #1607 (fix(tauri): retry main-window lookup) hardened against the same record-drop on a different code path; PR #2049 (fix(shell): use app-level hide on macOS close button) is why this PR is Windows-only. - Follow-up PR(s)/TODOs: if/when Tauri's re-track step lands synchronously with the host-visibility transition, this helper becomes vestigial and can be deleted. Tracked informally.
AI Authored PR Metadata (required for Codex/Linear PRs)
Keep this section for AI-authored PRs. For human-only PRs, mark each field
N/A.
Linear Issue
- Key: N/A (Sentry-driven triage; no Linear ticket)
- URL: N/A
Commit & Branch
- Branch: fix/sentry-3a-tray-window-race
- Commit SHA: ca240f93 (tip)
Agent
- Claude Code (Opus 4.7, 1M context) running locally
Summary by CodeRabbit
- Bug Fixes
- Improved main window display reliability by fixing a race condition on Windows that could prevent the window from appearing correctly.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 509f8726-e89a-4686-b143-c5c18f9d4f04
📥 Commits
Reviewing files that changed from the base of the PR and between 41e7631f05ec90f0426dd20a1211e8977149acd5 and ca240f931a6371cc4bc73f7dc223bf4af701dcf8.
📒 Files selected for processing (1)
app/src-tauri/src/lib.rs
📝 Walkthrough
Walkthrough
The PR adds retry logic to handle a Windows-specific race condition where setting window visibility (SW_SHOW) causes Tauri to temporarily drop the main webview window record. A new helper function attempts repeated lookups with bounded delay before returning None, while non-Windows platforms perform a single lookup. The show_main_window function is updated to use this helper.
Changes
Windows Main Window Retry Handler
| Layer / File(s) | Summary |
|---|---|
Retry helper and integration app/src-tauri/src/lib.rs |
get_main_webview_window_with_retry performs bounded retries on Windows (up to 5 attempts with short sleeps) when looking up the "main" webview window, while non-Windows platforms do a single lookup. show_main_window is updated to call this helper instead of direct app.get_webview_window("main"), and inline comments are updated to explain that window re-tracking is not synchronous with SW_SHOW and that the retry bounds prevent the tray callback from missing the re-tracked window. |
Estimated code review effort
🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
A rabbit hops through Windows gates, Where windows hide and briefly wait— Five swift retries, then off we go, The main one found, the tray will know. 🐰✨
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title accurately describes the main change: a Windows-specific fix for retrying main-window lookup after SW_SHOW to resolve a race condition. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
| Linked Issues check | ✅ Passed | Check skipped because no linked issues were found for this pull request. |
| Out of Scope Changes check | ✅ Passed | Check skipped because no linked issues were found for this pull request. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
Comment @coderabbitai help to get the list of available commands and usage tips.