{"record":{"id":"c9430a0e97778900","repo":"libnyanpasu/clash-nyanpasu","slug":"failed-to-get-delegate","errorCode":null,"errorMessage":"failed to get delegate","messagePattern":"failed to get delegate","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/tauri/src/window.rs","lineNumber":1101,"sourceCode":"                self.ivars().app_box.apply_traffic_lights_pos();\n                tracing::trace!(\"passthrough `windowDidExitFullScreen` to TAO layer\");\n                unsafe { self.ivars().super_class.windowDidExitFullScreen(notification) }\n            }\n\n            #[unsafe(method(windowDidFailToEnterFullScreen:))]\n            unsafe fn windowDidFailToEnterFullScreen(&self,window: &NSWindow) {\n                tracing::trace!(\"passthrough `windowDidFailToEnterFullScreen` to TAO layer\");\n                unsafe { self.ivars().super_class.windowDidFailToEnterFullScreen(window) }\n            }\n\n        }\n    }\n\n    impl WindowDelegate {\n        pub fn new(window_state: WindowState, mtm: MainThreadMarker) -> Retained<Self> {\n            let this = Self::alloc(mtm);\n            let super_class = window_state\n                .with_ns_window(|win| unsafe { win.delegate().expect(\"failed to get delegate\") });\n            let ivars = TrafficLightsWindowDelegateIvars {\n                app_box: window_state,\n                super_class,\n            };\n            let this = this.set_ivars(ivars);\n            unsafe { msg_send![super(this), init] }\n        }\n    }\n\n    pub struct TrafficLightsWindowDelegateGuard {\n        _delegate: Retained<WindowDelegate>,\n    }\n\n    thread_local! {\n        /// This is used to keep the delegate alive until the window is destroyed\n        static TRAFFIC_LIGHTS_WINDOW_DELEGATE_GUARD: RefCell<Option<TrafficLightsWindowDelegateGuard>> = const { RefCell::new(None) };\n    }\n","sourceCodeStart":1083,"sourceCodeEnd":1119,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/window.rs#L1083-L1119","documentation":"`WindowDelegate::new` reads the NSWindow's existing delegate via `win.delegate()` and expects it to be present, storing it as `super_class` so the custom `TrafficLightsWindowDelegate` can forward messages. The panic means Tauri's window had no delegate installed yet (or the objc message returned nil). Without the original delegate, message forwarding would break, so the code treats it as fatal.","triggerScenarios":"Constructing `WindowDelegate` (macOS traffic-light feature init) before Tauri/WRY installs its own NSWindow delegate, or after it has been replaced/cleared; calling this during early window setup on a not-yet-fully-initialized window.","commonSituations":"Applying the delegate at app startup ahead of WRY's window configuration; Tauri/WRY version changes that moved when the delegate is installed; creating the window with configurations that skip the default delegate.","solutions":["Defer `WindowDelegate::new` until after the window is fully created and shown (e.g. hook it on a setup/ready event, not during construction).","Check WRY/Tauri versions: ensure the delegate installation timing matches this code's assumption; upgrade or add a readiness wait.","Fall back to a nil super_class with degraded behavior (log + skip forwarding) instead of panicking if a delegate is genuinely absent.","If it persists, intercept earlier in window creation so the custom delegate wraps the delegate before it is consumed."],"exampleFix":"// before\nlet super_class = window_state\n    .with_ns_window(|win| unsafe { win.delegate().expect(\"failed to get delegate\") });\n// after\nlet super_class = window_state\n    .with_ns_window(|win| unsafe { win.delegate() })\n    .unwrap_or_else(|| {\n        tracing::warn!(\"window has no delegate yet; deferring traffic lights\");\n        // defer or skip delegate wrapping\n        todo!(\"handle missing delegate gracefully\")\n    });","handlingStrategy":"fallback","validationCode":"// call only after window setup completes\nlet has_delegate = window_state.with_ns_window(|win| unsafe { win.delegate().is_some() });\nif !has_delegate {\n    tracing::warn!(\"no delegate installed yet; defer WindowDelegate::new\");\n}","typeGuard":"fn window_has_delegate(window_state: &WindowState) -> bool {\n    window_state.with_ns_window(|win| unsafe { win.delegate().is_some() })\n}","tryCatchPattern":"match window_state.with_ns_window(|win| unsafe { win.delegate() }) {\n    Some(delegate) => { /* proceed with wrapping */ }\n    None => {\n        tracing::warn!(\"delegate missing; deferring traffic-light delegate setup\");\n        return None;\n    }\n}","preventionTips":["Install the custom delegate in the Tauri setup hook after window creation completes","Pin WRY/Tauri versions and re-verify delegate installation timing on upgrades","Never call delegate-dependent setup from background threads"],"tags":["macos","objc2","delegate","panic"],"backgroundTag":"resource-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"}