{"record":{"id":"37a9757fb46a3ba4","repo":"libnyanpasu/clash-nyanpasu","slug":"ipc-server-is-already-initialized","errorCode":null,"errorMessage":"IPC server is already initialized","messagePattern":"IPC server is already initialized","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/nyanpasu-egui/src/ipc.rs","lineNumber":33,"sourceCode":"pub enum Message {\n    Stop,\n    UpdateStatistic(StatisticMessage),\n    UpdateLogo(LogoPreset),\n}\n\npub struct IPCServer {\n    oneshot_server: Option<ipc::IpcOneShotServer<IpcSender<Message>>>,\n    tx: Option<IpcSender<Message>>,\n}\n\nimpl IPCServer {\n    pub fn is_connected(&self) -> bool {\n        self.tx.is_some()\n    }\n\n    pub fn connect(&mut self) -> anyhow::Result<()> {\n        if self.oneshot_server.is_none() {\n            anyhow::bail!(\"IPC server is already initialized\");\n        }\n\n        let (_, tx) = self.oneshot_server.take().unwrap().accept()?;\n        self.tx = Some(tx);\n        Ok(())\n    }\n\n    pub fn into_tx(self) -> Option<IpcSender<Message>> {\n        self.tx\n    }\n}\n\npub fn create_ipc_server() -> anyhow::Result<(IPCServer, String)> {\n    let (oneshot_server, oneshot_server_name) = ipc::IpcOneShotServer::new()?;\n    Ok((\n        IPCServer {\n            oneshot_server: Some(oneshot_server),\n            tx: None,","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/nyanpasu-egui/src/ipc.rs#L15-L51","documentation":"Ipc::connect() bails with 'IPC server is already initialized' when self.oneshot_server is None. Note the inverted-looking guard: once the server slot has been consumed (already connected/accepted), connect refuses to run again, enforcing a single connection per Ipc instance.","triggerScenarios":"Calling connect() a second time on the same Ipc instance after the oneshot server was already taken, or on an instance constructed without a server.","commonSituations":"Re-connecting an egui backend IPC client during hot-reload or re-init flows without recreating the Ipc object.","solutions":["Check is_connected() before calling connect() and skip if already connected","Create a fresh Ipc instance instead of reconnecting the old one","Reuse the existing tx channel rather than re-accepting"],"exampleFix":"// before\nipc.connect()?;\n// after\nif !ipc.is_connected() {\n    ipc.connect()?;\n}","handlingStrategy":"type-guard","validationCode":"if ipc.is_connected() {\n  return Ok(()); // already connected\n}","typeGuard":"pub fn is_connected(&self) -> bool {\n  self.tx.is_some()\n}","tryCatchPattern":"match ipc.connect() {\n  Ok(()) => {},\n  Err(e) if e.to_string().contains(\"already initialized\") => {}, // idempotent no-op\n  Err(e) => return Err(e),\n}","preventionTips":["Make connect idempotent by checking is_connected first","Recreate the Ipc instance instead of reconnecting","Treat one-shot servers as single-use by design"],"tags":["ipc","rust","state"],"backgroundTag":"invalid-state-transition","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"}