libnyanpasu/clash-nyanpasu · error · anyhow::Error

failed to get tray menu window after creation

Error message

failed to get tray menu window after creation

What it means

show_tray_menu_window reuses an existing tray menu window or creates one via TrayMenuWindow.create_with_params. When creation was requested but the window still cannot be found by label immediately afterwards, this error is raised. It means the create call did not result in a registered webview window.

Source

Thrown at backend/tauri/src/utils/resolve.rs:628

/// Show the webview tray menu window near the given cursor position.
pub fn show_tray_menu_window(
    app_handle: &AppHandle,
    cursor: tauri::PhysicalPosition<f64>,
) -> Result<()> {
    use tauri::{Manager, PhysicalPosition};

    TRAY_MENU_PERSISTENT.store(false, Ordering::Release);
    TRAY_MENU_READY.store(false, Ordering::Release);
    ignore_tray_menu_blur_for(TRAY_MENU_SHOW_BLUR_GRACE_MS);

    let win = match app_handle.get_webview_window(crate::consts::TRAY_MENU_WINDOW_LABEL) {
        Some(existing) => existing,
        None => {
            let result = TrayMenuWindow.create_with_params(app_handle, None)?;
            let win = app_handle
                .get_webview_window(crate::consts::TRAY_MENU_WINDOW_LABEL)
                .ok_or_else(|| anyhow::anyhow!("failed to get tray menu window after creation"))?;
            if result.is_new {
                setup_tray_menu_focus_handler(&win);
            }
            win
        }
    };

    let (menu_w, menu_h) = win
        .outer_size()
        .map(|size| {
            let width = if size.width == 0 {
                240.0
            } else {
                size.width as f64
            };
            let height = if size.height == 0 {
                448.0
            } else {

View on GitHub (pinned to f7dbce2997)

Solutions

  1. Confirm the label passed to TrayMenuWindow creation equals crate::consts::TRAY_MENU_WINDOW_LABEL
  2. Inspect logs from the webview window creation for build failures that unregister the window
  3. Retry the lookup once after a yield/sleep if registration is asynchronous, or return the created window handle directly from create_with_params
  4. Serialize tray window show/create to avoid concurrent close/recreate races

Example fix

// before
let result = TrayMenuWindow.create_with_params(app_handle, None)?;
let win = app_handle
    .get_webview_window(crate::consts::TRAY_MENU_WINDOW_LABEL)
    .ok_or_else(|| anyhow::anyhow!("failed to get tray menu window after creation"))?;
// after: have create_with_params return the window itself
let result = TrayMenuWindow.create_with_params(app_handle, None)?;
let win = result.window.ok_or_else(|| anyhow::anyhow!(
    "tray menu window missing after creation (is_new={})", result.is_new
))?;
Defensive patterns

Strategy: retry

Validate before calling

if app_handle.get_webview_window(crate::consts::TRAY_MENU_WINDOW_LABEL).is_some() {
    // existing window; skip creation path entirely
}

Try / catch

let win = loop {
    if let Some(w) = app_handle.get_webview_window(crate::consts::TRAY_MENU_WINDOW_LABEL) {
        break w;
    }
    TrayMenuWindow.create_with_params(app_handle, None)?;
    tokio::time::sleep(Duration::from_millis(50)).await; // allow registration
    // bound retries in real code
};

Prevention

When it happens

Trigger: The None-branch runs (window absent), create_with_params returns Ok, but the follow-up get_webview_window(TRAY_MENU_WINDOW_LABEL) still returns None — label mismatch, deferred/async registration, or the new window failed during webview initialization and was dropped.

Common situations: Label constant changed in creation code but not consts; window creation succeeded at the manager level but the webview build failed and cleaned up; concurrent close racing the lookup.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of libnyanpasu/clash-nyanpasu@f7dbce2997 (2026-09-08). Data as JSON: /api/errors/4f6cf8f5e2c37de1. Report an issue: GitHub.