{"record":{"id":"c493d2eb27924091","repo":"libnyanpasu/clash-nyanpasu","slug":"failed-to-get-window","errorCode":null,"errorMessage":"failed to get window","messagePattern":"failed to get window","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"warning","filePath":"backend/tauri/src/window.rs","lineNumber":816,"sourceCode":"        send_message_to_window(app_handle, message)\n    }\n\n    /// Broadcast a message to all instances of another window type\n    fn broadcast_to_type(\n        &self,\n        app_handle: &AppHandle,\n        target_type: &str,\n        event: &str,\n        payload: serde_json::Value,\n    ) -> Result<()> {\n        broadcast_to_window_type(app_handle, target_type, self.label(), event, payload)\n    }\n\n    /// Save window state with default implementation\n    fn save_state(&self, app_handle: &AppHandle, save_to_file: bool) -> Result<()> {\n        let win = app_handle\n            .get_webview_window(self.label())\n            .ok_or(anyhow::anyhow!(\"failed to get window\"))?;\n        if win.is_minimized()? {\n            if save_to_file {\n                Config::verge().data().save_file()?;\n            }\n            return Ok(());\n        }\n\n        let state = match win.current_monitor()? {\n            Some(_) => {\n                let maximized = win.is_maximized()?;\n                let fullscreen = win.is_fullscreen()?;\n                let size = win.inner_size()?;\n\n                // During system shutdown, Windows sends resize events with 0x0 dimensions.\n                // Skip saving in this case to preserve the last valid window state.\n                if (size.width == 0 || size.height == 0) && !maximized && !fullscreen {\n                    tracing::debug!(\n                        \"skipping window state save: invalid size {}x{} in normal state\",","sourceCodeStart":798,"sourceCodeEnd":834,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/window.rs#L798-L834","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Guard the save: check app_handle.get_webview_window(self.label()).is_some() before calling save_state, or log-and-return Ok(()) when absent","Ensure save_state is invoked while the window still exists (e.g. on window close-request events, not after CloseRequested completes)","Verify the label() returned by the implementor matches the label used at window creation","Call Config::verge().data().save_file() unconditionally at shutdown if persistence is the actual goal"],"exampleFix":"// before\nlet win = app_handle\n    .get_webview_window(self.label())\n    .ok_or(anyhow::anyhow!(\"failed to get window\"))?;\n// after\nlet Some(win) = app_handle.get_webview_window(self.label()) else {\n    log::debug!(\"window {:?} gone; skipping state save\", self.label());\n    return Ok(());\n};","handlingStrategy":"validation","validationCode":"// before saving state\nif app_handle.get_webview_window(self.label()).is_none() {\n    return Ok(()); // window gone; nothing to persist\n}","typeGuard":"fn window_alive(app: &AppHandle, label: &str) -> bool {\n    app.get_webview_window(label).is_some()\n}","tryCatchPattern":"if let Err(e) = win_state.save_state(&app_handle, true) {\n    log::warn!(\"window state save skipped: {e}\");\n}","preventionTips":["Trigger state saves from window close-request events, not after teardown","Keep the trait's label() in sync with the actual window creation label","Persist config files independently of window lifetime at shutdown"],"tags":["window","tauri","state"],"backgroundTag":"entity-not-found","analyzedSha":"f7dbce2997c633e484f54788035e770b3ee99773","analyzedAt":"2026-09-08T01:24:59.197Z","contentChangedAt":"2026-09-08T01:24:59.197Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}