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

failed to get window

Error message

failed to get window

What it means

save_state (the default implementation of a window-state trait) looks up the webview window by its label from the AppHandle. If no window with that label exists, it fails with this anyhow error. The library throws it because window state (size/position) can only be persisted for a live window.

Source

Thrown at backend/tauri/src/window.rs:816

        send_message_to_window(app_handle, message)
    }

    /// Broadcast a message to all instances of another window type
    fn broadcast_to_type(
        &self,
        app_handle: &AppHandle,
        target_type: &str,
        event: &str,
        payload: serde_json::Value,
    ) -> Result<()> {
        broadcast_to_window_type(app_handle, target_type, self.label(), event, payload)
    }

    /// Save window state with default implementation
    fn save_state(&self, app_handle: &AppHandle, save_to_file: bool) -> Result<()> {
        let win = app_handle
            .get_webview_window(self.label())
            .ok_or(anyhow::anyhow!("failed to get window"))?;
        if win.is_minimized()? {
            if save_to_file {
                Config::verge().data().save_file()?;
            }
            return Ok(());
        }

        let state = match win.current_monitor()? {
            Some(_) => {
                let maximized = win.is_maximized()?;
                let fullscreen = win.is_fullscreen()?;
                let size = win.inner_size()?;

                // During system shutdown, Windows sends resize events with 0x0 dimensions.
                // Skip saving in this case to preserve the last valid window state.
                if (size.width == 0 || size.height == 0) && !maximized && !fullscreen {
                    tracing::debug!(
                        "skipping window state save: invalid size {}x{} in normal state",

View on GitHub (pinned to f7dbce2997)

Solutions

  1. Guard the save: check app_handle.get_webview_window(self.label()).is_some() before calling save_state, or log-and-return Ok(()) when absent
  2. Ensure save_state is invoked while the window still exists (e.g. on window close-request events, not after CloseRequested completes)
  3. Verify the label() returned by the implementor matches the label used at window creation
  4. Call Config::verge().data().save_file() unconditionally at shutdown if persistence is the actual goal

Example fix

// before
let win = app_handle
    .get_webview_window(self.label())
    .ok_or(anyhow::anyhow!("failed to get window"))?;
// after
let Some(win) = app_handle.get_webview_window(self.label()) else {
    log::debug!("window {:?} gone; skipping state save", self.label());
    return Ok(());
};
Defensive patterns

Strategy: validation

Validate before calling

// before saving state
if app_handle.get_webview_window(self.label()).is_none() {
    return Ok(()); // window gone; nothing to persist
}

Type guard

fn window_alive(app: &AppHandle, label: &str) -> bool {
    app.get_webview_window(label).is_some()
}

Try / catch

if let Err(e) = win_state.save_state(&app_handle, true) {
    log::warn!("window state save skipped: {e}");
}

Prevention

When it happens

Trigger: Calling save_state (or triggering state save on app exit/blur) when the window for self.label() was already closed or never created — e.g. saving state after the window is destroyed, or a label mismatch between the trait implementor and actual window creation.

Common situations: Saving window state during app exit after windows were dropped; multiple windows where one was closed before the global save; renaming a window label without updating the trait implementor; calling save_state from a non-UI thread after window teardown.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


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