{"record":{"id":"028a4013b4739fa1","repo":"libnyanpasu/clash-nyanpasu","slug":"failed-to-flush-socket","errorCode":null,"errorMessage":"Failed to flush socket","messagePattern":"Failed to flush socket","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/tauri-plugin-deep-link/src/windows.rs","lineNumber":175,"sourceCode":"                        // let primary_instance_pid = conn.peer_pid().unwrap_or(ASFW_ANY);\n                        // unsafe {\n                        //     let success = AllowSetForegroundWindow(primary_instance_pid) != 0;\n                        //     if !success {\n                        //         log::warn!(\"AllowSetForegroundWindow failed.\");\n                        //     }\n                        // }\n                        let (socket_rx, mut socket_tx) = conn.split();\n                        let mut socket_rx = socket_rx.as_tokio_async_read();\n                        let url = std::env::args().nth(1).expect(\"URL not provided\");\n                        socket_tx\n                            .write_all(url.as_bytes())\n                            .await\n                            .expect(\"Failed to write to socket\");\n                        socket_tx\n                            .write_all(b\"\\n\")\n                            .await\n                            .expect(\"Failed to write to socket\");\n                        socket_tx.flush().await.expect(\"Failed to flush socket\");\n\n                        let mut reader = BufReader::new(&mut socket_rx);\n                        let mut buf = String::new();\n                        if let Err(e) = reader.read_line(&mut buf).await {\n                            eprintln!(\"Error reading from connection: {e}\");\n                        }\n                        buf.pop();\n                        dummy_keypress();\n                        let pid = buf.parse::<u32>().unwrap_or(ASFW_ANY);\n                        unsafe {\n                            let success = AllowSetForegroundWindow(pid) != 0;\n                            if !success {\n                                eprintln!(\"AllowSetForegroundWindow failed.\");\n                            }\n                        }\n                        std::process::exit(0);\n                    }\n                    Err(e) => {","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri-plugin-deep-link/src/windows.rs#L157-L193","documentation":"The deep-link plugin's Windows single-instance handler panics when flushing the data written to the inter-process socket fails. flush() forces buffered bytes out to the OS socket; an error means the connection to the already-running instance broke mid-handshake (closed, reset, or unreachable). The library uses .expect(), so the failure aborts the new process instead of being recoverable.","triggerScenarios":"A second instance launches, connects to the first instance's named-pipe/TCP socket, writes the deep-link URL, and socket_tx.flush().await fails because the primary instance's listener dropped the connection, exited, or the socket was reset between write and flush.","commonSituations":"Primary app quitting just as a second instance starts; antivirus or firewall interfering with the local socket; socket path/permission problems on Windows; a stale or crashed primary instance whose socket is half-open.","solutions":["Ensure the primary instance's server/listener stays alive and accepts connections before the secondary writes to the socket","Replace the .expect on flush with graceful error handling so a transient connection failure exits quietly instead of panicking","Retry the connection once after a short delay before giving up","Check that no firewall/AV is blocking local inter-process socket connections"],"exampleFix":"// before\nsocket_tx.flush().await.expect(\"Failed to flush socket\");\n// after\nif let Err(e) = socket_tx.flush().await {\n    eprintln!(\"Failed to flush single-instance socket: {e}\");\n    std::process::exit(0);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if let Err(e) = socket_tx.flush().await {\n    eprintln!(\"single-instance flush failed: {e}\");\n    // app already running took over — exit quietly\n    std::process::exit(0);\n}","preventionTips":["Handle flush/write errors instead of expect so secondary instances exit gracefully","Keep the primary instance's socket listener alive until full shutdown","Log the underlying io error to distinguish reset vs refused connections"],"tags":["windows","ipc","socket","panic"],"backgroundTag":"broken-pipe","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"}