{"record":{"id":"e31b82043c974eda","repo":"libnyanpasu/clash-nyanpasu","slug":"port-is-not-available","errorCode":null,"errorMessage":"Port {} is not available","messagePattern":"Port (.+?) is not available","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/tauri/src/utils/help.rs","lineNumber":164,"sourceCode":"    Ok(())\n}\n\n/// Resolve the UI language from the OS locale, as the canonical i18n key.\n///\n/// The key names a `rust_i18n` bundle in `backend/tauri/locales` and a paraglide\n/// locale in the frontend; both use the same lowercase spelling.\npub fn detect_system_i18n_key() -> &'static str {\n    nyanpasu_config::application::default_i18n_language().as_str()\n}\n\npub fn get_clash_external_port(\n    strategy: &ExternalControllerPortStrategy,\n    port: u16,\n) -> anyhow::Result<u16> {\n    match strategy {\n        ExternalControllerPortStrategy::Fixed => {\n            if !port_scanner::local_port_available(port) {\n                bail!(\"Port {} is not available\", port);\n            }\n        }\n        ExternalControllerPortStrategy::Random | ExternalControllerPortStrategy::AllowFallback => {\n            if ExternalControllerPortStrategy::AllowFallback == *strategy\n                && port_scanner::local_port_available(port)\n            {\n                return Ok(port);\n            }\n            let new_port = port_scanner::request_open_port()\n                .ok_or_else(|| anyhow!(\"Can't find an open port\"))?;\n            return Ok(new_port);\n        }\n    }\n    Ok(port)\n}\n\npub fn resize_tray_image(img: &[u8], scale_factor: f64) -> Result<Vec<u8>> {\n    let img = ImageReader::new(Cursor::new(img))","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/utils/help.rs#L146-L182","documentation":"With ExternalControllerPortStrategy::Fixed, get_clash_external_port requires the configured port to be free; if local_port_available reports it occupied, it bails with this message. The function refuses to silently pick another port under a fixed strategy.","triggerScenarios":"Calling get_clash_external_port (via prepare_external_controller_port or patch_clash_with_rebuild) with strategy Fixed while another process — often a leftover clash/mihomo core or the app itself — is already bound to the port.","commonSituations":"Previous core process not fully terminated after a crash, two app instances running simultaneously, or the fixed port colliding with another service (e.g. another proxy panel).","solutions":["Find and stop the process holding the port (netstat/Get-NetTCPConnection) — usually a stale clash core.","Switch the strategy to AllowFallback or Random so a free port is chosen automatically.","Change the configured external-controller port to an unused one.","Ensure previous core instances are killed before restart (check the app's core lifecycle handling)."],"exampleFix":"// before\nstrategy: ExternalControllerPortStrategy::Fixed,\n// after\nstrategy: ExternalControllerPortStrategy::AllowFallback, // fall back to a free port","handlingStrategy":"validation","validationCode":"let port = parse_port(config.external_controller_port)?;\nif strategy == ExternalControllerPortStrategy::Fixed\n    && !port_scanner::local_port_available(port) {\n    eprintln!(\"port {port} busy; free it or switch to AllowFallback before starting\");\n}","typeGuard":null,"tryCatchPattern":"match get_clash_external_port(strategy, port) {\n    Ok(p) => p,\n    Err(e) if e.to_string().contains(\"is not available\") => {\n        get_clash_external_port(ExternalControllerPortStrategy::Random, port)?\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Prefer AllowFallback over Fixed unless the port must be stable","Kill stale clash/mihomo core processes before restart","Avoid running two app instances against the same data dir"],"tags":["port","network","config","clash"],"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"}