{"record":{"id":"3c95cf584b1870f0","repo":"loco-rs/loco","slug":"failed-to-bind-to-address","errorCode":null,"errorMessage":"Failed to bind to address","messagePattern":"Failed to bind to address","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/testing/request.rs","lineNumber":157,"sourceCode":"/// The hostname to which the test server binds.\npub const TEST_BINDING_SERVER: &str = \"localhost\";\n\n/// Constructs and returns the base URL used for the test server.\n#[must_use]\npub fn get_base_url_port(port: i32) -> String {\n    format!(\"http://{TEST_BINDING_SERVER}:{port}/\")\n}\n\n/// Returns a unique port number. Usually increments by 1 starting from 59126\n///\n/// # Panics\n///\n/// Will panic if binding to test server address fails or if getting the local address fails\npub async fn get_available_port() -> i32 {\n    let addr = format!(\"{TEST_BINDING_SERVER}:0\");\n    let listener = TcpListener::bind(addr)\n        .await\n        .expect(\"Failed to bind to address\");\n\n    i32::from(\n        listener\n            .local_addr()\n            .expect(\"Failed to get local address\")\n            .port(),\n    )\n}\n\n/// Bootstraps test application with test environment hard coded.\n///\n/// # Example\n///\n/// The provided example demonstrates how to boot the test case with the\n/// application context.\n///\n/// ```rust,ignore\n/// use myapp::app::App;","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/loco-rs/loco/blob/23639d1e360dbc618073642b507d6f8664adbaff/src/testing/request.rs#L139-L175","documentation":"get_available_port binds a TcpListener to the TEST_BINDING_SERVER address with port 0 to let the OS pick a free port. If the bind fails — address family/hostname unresolvable, networking unavailable, or (on exotic setups) the resolver rejects it — the .expect panics with 'Failed to bind to address'.","triggerScenarios":"Calling get_available_port() (directly or via the request test helpers) when TcpListener::bind(\"{TEST_BINDING_SERVER}:0\") fails: the hostname does not resolve, IPv6/IPv4 mismatch in restricted containers, or no loopback networking (some sandboxes/CI without network namespaces configured).","commonSituations":"CI runners with networking disabled or restricted; Docker containers run with --network none; hostname resolution differences between macOS/Linux where 'localhost' resolution is blocked; corporate firewalls or seccomp profiles blocking socket bind.","solutions":["Verify loopback networking works in the environment (ping/curl localhost, check /etc/hosts has 127.0.0.1 localhost).","If TEST_BINDING_SERVER resolves oddly, ensure it maps to 127.0.0.1 (fix /etc/hosts or use an explicit IP).","Enable networking in the CI container (remove --network none / add loopback support).","Check seccomp/AppArmor or sandbox profiles that block socket() or bind() syscalls and allow them.","As a fallback, parse the port from an explicitly bound 127.0.0.1:0 listener before calling the helper."],"exampleFix":"// before\nlet addr = format!(\"{TEST_BINDING_SERVER}:0\");\nlet listener = TcpListener::bind(addr).await.expect(\"Failed to bind to address\");\n// after\nlet addr = format!(\"{TEST_BINDING_SERVER}:0\");\nlet listener = TcpListener::bind(&addr).await\n    .or_else(|_| tokio::net::TcpListener::bind(\"127.0.0.1:0\"))\n    .await\n    .expect(\"Failed to bind to any test address\");","handlingStrategy":"try-catch","validationCode":"// precheck before running tests\nmatch tokio::net::TcpListener::bind(\"127.0.0.1:0\").await {\n    Ok(l) => drop(l),\n    Err(e) => panic!(\"loopback bind unavailable in this environment: {e}\"),\n}","typeGuard":null,"tryCatchPattern":"match TcpListener::bind(format!(\"{TEST_BINDING_SERVER}:0\")).await {\n    Ok(listener) => { /* proceed */ }\n    Err(e) => eprintln!(\"skip test: cannot bind test listener: {e}\"),\n}","preventionTips":["Ensure /etc/hosts maps localhost to 127.0.0.1 in CI containers","Enable loopback networking in sandboxed runners (--network != none)","Allow socket bind syscalls in seccomp/AppArmor profiles","Pin the binding host to an explicit IP when hostname resolution is flaky"],"tags":["rust","testing","network","bind"],"backgroundTag":"connection-refused","analyzedSha":"23639d1e360dbc618073642b507d6f8664adbaff","analyzedAt":"2026-09-12T01:47:20.769Z","contentChangedAt":"2026-09-12T01:47:20.769Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}