{"record":{"id":"5a06acb3e893ac52","repo":"libnyanpasu/clash-nyanpasu","slug":"can-not-select-fixed-current-port-is-not-availabl","errorCode":null,"errorMessage":"can not select fixed: current port is not available.","messagePattern":"can not select fixed: current port is not available\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/tauri/src/feat.rs","lineNumber":307,"sourceCode":"            {\n                Config::clash().discard();\n                bail!(\"port already in use\");\n            }\n        };\n\n        // 检测 external-controller port 是否修改\n        if let Some(external_controller) = patch.get(\"external-controller\") {\n            let external_controller = external_controller.as_str().unwrap();\n            let changed = external_controller != Config::clash().data().get_client_info().server;\n            if changed {\n                let (_, port) = external_controller.split_once(':').unwrap();\n                let port = port.parse::<u16>()?;\n                let strategy = Config::verge()\n                    .latest()\n                    .get_external_controller_port_strategy();\n                if core_running && get_clash_external_port(&strategy, port).is_err() {\n                    Config::clash().discard();\n                    bail!(\"can not select fixed: current port is not available.\");\n                }\n            }\n        }\n\n        // Every desired clash patch enters the rebuild input and regenerates the\n        // derived config. Only port/controller/secret changes restart the core.\n        let restart = requires_core_restart(&patch);\n        let rebuilt = rebuild(restart).await?;\n        if restart {\n            handle::Handle::refresh_clash();\n        }\n\n        // 更新系统代理\n        if mixed_port.is_some() {\n            log_err!(sysopt::Sysopt::global().init_sysproxy());\n        }\n\n        if patch.get(\"mode\").is_some() {","sourceCodeStart":289,"sourceCodeEnd":325,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/feat.rs#L289-L325","documentation":"When the patch sets `external-controller` to a fixed port, and the core is currently running, the code verifies a free external-controller port can be selected under the configured port strategy (get_clash_external_port). If selection fails, the config draft is discarded and it bails with \"can not select fixed: current port is not available.\"","triggerScenarios":"patch_clash with `external-controller: \"127.0.0.1:<fixed-port>\"` where core is running, the port strategy is \"fixed\", and the requested fixed port is occupied or unavailable (get_clash_external_port returns Err).","commonSituations":"User pins external-controller to a port already used by another app or a previous core instance; switching the port strategy to fixed while the desired port conflicts; running a second instance that already grabbed the port.","solutions":["Choose a different external-controller port that is currently free, and retry the patch.","Switch the external-controller port strategy (verge config) from \"fixed\" to a random/range strategy so a free port is auto-selected.","Stop the conflicting process holding the port (or restart the app to reap the stale core), then re-apply the fixed port."],"exampleFix":"// before\npatch_clash(serde_json::json!({ \"external-controller\": \"127.0.0.1:9090\" })).await?;\n// after\nif port_scanner::local_port_available(9090) {\n    patch_clash(serde_json::json!({ \"external-controller\": \"127.0.0.1:9090\" })).await?;\n} else {\n    // leave strategy as random so get_clash_external_port picks a free port\n    eprintln!(\"9090 busy; using random external-controller port\");\n}","handlingStrategy":"validation","validationCode":"// before patching to a fixed external-controller port:\nlet port: u16 = 9090;\nif !port_scanner::local_port_available(port) {\n    eprintln!(\"fixed external-controller port {} unavailable; use random strategy\", port);\n    return;\n}","typeGuard":null,"tryCatchPattern":"match patch_clash(patch).await {\n    Err(e) if e.to_string().contains(\"can not select fixed\") => {\n        // fall back to a random/range port strategy and retry\n    },\n    other => other?,\n}","preventionTips":["Prefer the random/range port strategy unless a fixed port is strictly required.","Before pinning a fixed port, verify it is free and not used by other local services.","Restart the app to reap stale core processes that may hold the previously selected port."],"tags":["port","external-controller","configuration"],"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"}