{"record":{"id":"68f0a854a4abadc1","repo":"libnyanpasu/clash-nyanpasu","slug":"failed-to-start-server","errorCode":null,"errorMessage":"failed to start server","messagePattern":"failed to start server","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"backend/tauri/src/lib.rs","lineNumber":333,"sourceCode":"                &[\"clash-nyanpasu\", \"clash\"],\n                move |request| {\n                    log::info!(target: \"app\", \"scheme request received: {:?}\", request);\n                    resolve::create_window(&handle.clone()); // create window if not exists\n                    while !is_window_opened() {\n                        log::info!(target: \"app\", \"waiting for window open\");\n                        std::thread::sleep(std::time::Duration::from_millis(100));\n                    }\n                    log_err!(\n                        crate::ipc::SchemeRequestReceivedEvent { url: request }.emit(&handle),\n                        \"failed to emit scheme-request-received event\"\n                    );\n                }\n            ));\n            std::thread::spawn(move || {\n                nyanpasu_utils::runtime::block_on(async move {\n                    server::run(*server::SERVER_PORT)\n                        .await\n                        .expect(\"failed to start server\");\n                });\n            });\n            Ok(())\n        });\n\n    let app = builder\n        .build(tauri::generate_context!())\n        .expect(\"error while running tauri application\");\n    app.run(|app_handle, e| match e {\n        tauri::RunEvent::ExitRequested { api, code, .. } if code.is_none() => {\n            api.prevent_exit();\n        }\n        tauri::RunEvent::ExitRequested { .. } => {\n            utils::help::cleanup_processes(app_handle);\n        }\n        tauri::RunEvent::WindowEvent { label, event, .. } if label == \"main\" => match event {\n            tauri::WindowEvent::ScaleFactorChanged { scale_factor, .. } => {\n                core::tray::on_scale_factor_changed(scale_factor);","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/lib.rs#L315-L351","documentation":"During Tauri app setup, the internal server (server::run) is started on a background thread; if it returns an error the .expect panics with 'failed to start server'. The server is essential (IPC/services depend on it), so the library treats a failed bind/start as fatal.","triggerScenarios":"server::run(*SERVER_PORT) fails — usually because SERVER_PORT is already bound by another process or a leftover instance of the app, or the async runtime inside the spawned thread fails to drive the server.","commonSituations":"Previous instance didn't shut down and still holds the port; two app instances racing for the same fixed port; firewall/AV blocking the bind; port conflict with another local service.","solutions":["Check what occupies the port (netstat) and kill the stale process, or free the port","Use port 0 / a fallback port if the fixed port is taken, and persist the chosen port","Detect an already-running instance first and forward activation to it instead of binding","Add error propagation/logging before the expect to identify the underlying bind error"],"exampleFix":"// before\nserver::run(*server::SERVER_PORT).await.expect(\"failed to start server\");\n// after\nif let Err(e) = server::run(*server::SERVER_PORT).await {\n    log::error!(\"server failed to start: {e}\");\n}","handlingStrategy":"retry","validationCode":"use std::net::TcpListener;\nfn port_free(port: u16) -> bool {\n    TcpListener::bind((\"127.0.0.1\", port)).is_ok()\n}\n// call before starting the app; if !port_free(SERVER_PORT), abort or pick fallback","typeGuard":null,"tryCatchPattern":"match server::run(*server::SERVER_PORT).await {\n    Ok(()) => {},\n    Err(e) => log::error!(\"server start failed: {e}\"), // surface bind error instead of panic\n}","preventionTips":["Ensure previous instances exit cleanly and release the port","Support a fallback port when the configured port is occupied","Check for a running single instance before binding"],"tags":["server","port-conflict","startup","panic"],"backgroundTag":"address-already-in-use","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"}