{"record":{"id":"3949bd4ec8ce2b34","repo":"quickwit-oss/quickwit","slug":"split-should-never-fail","errorCode":null,"errorMessage":"Split should never fail.","messagePattern":"Split should never fail\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"quickwit/quickwit-common/src/net.rs","lineNumber":338,"sourceCode":"}\n\n// Inner function for testing purposes.\nfn _get_hostname(hostname: OsString) -> io::Result<String> {\n    let hostname_lossy = hostname.to_string_lossy();\n    if is_valid_hostname(&hostname_lossy) {\n        Ok(hostname_lossy.to_string())\n    } else {\n        Err(io::Error::other(format!(\n            \"invalid hostname: `{hostname_lossy}`\"\n        )))\n    }\n}\n\npub fn get_short_hostname() -> io::Result<String> {\n    Ok(get_hostname()?\n        .split('.')\n        .next()\n        .expect(\"Split should never fail.\")\n        .to_string())\n}\n\n/// Returns whether a hostname is valid according to [RFC 1123](https://www.rfc-editor.org/rfc/rfc1123).\n///\n/// A hostname is valid if the following conditions are met:\n///\n/// - It does not start or end with `-` or `.`.\n/// - It does not contain any characters outside of the alphanumeric range, except for `-` and `.`.\n/// - It is not empty.\n/// - It is 253 or fewer characters.\n/// - Its labels (characters separated by `.`) are not empty.\n/// - Its labels are 63 or fewer characters.\n/// - Its labels do not start or end with '-' or '.'.\npub fn is_valid_hostname(hostname: &str) -> bool {\n    if hostname.is_empty() || hostname.len() > 253 {\n        return false;\n    }","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/quickwit-oss/quickwit/blob/a39730c5cdcd1a4fe798403737ae293999ea21f8/quickwit/quickwit-common/src/net.rs#L320-L356","documentation":"get_short_hostname takes the machine hostname, splits on '.', and returns the first label. `str::split` always yields at least one item (possibly empty), so the expect \"Split should never fail.\" asserts a property of std, not of the environment. The only way to observe an anomaly is an empty-string hostname, which passes through as \"\" rather than panicking.","triggerScenarios":"Virtually never panics; split(' ').next()/split('.').next() always returns Some. The surrounding get_hostname() io::Error (propagated via `?`) is what users actually see when the hostname is unset.","commonSituations":"Misconfigured containers without a hostname (affects default_node_id); the io::Error from get_hostname is the realistic failure, typically in bare Docker runs lacking --hostname.","solutions":["If the related hostname error occurs, set an explicit hostname in your container/runtime (`docker run --hostname ...`, `--name` in K8s pods).","Or configure node_id explicitly in the quickwit config to bypass hostname discovery.","For the expect itself, no fix needed — it is a sound invariant; replace with unwrap_or_default only if silencing lints."],"exampleFix":"// before\nOk(get_hostname()?.split('.').next().expect(\"Split should never fail.\").to_string())\n// after\nOk(get_hostname()?.split('.').next().unwrap_or_default().to_string())","handlingStrategy":"type-guard","validationCode":"let hostname = std::env::var(\"HOSTNAME\").or_else(|_| hostname_cmd()).unwrap_or_default();\nassert!(!hostname.is_empty(), \"hostname must be set for default node_id\");","typeGuard":"fn has_nonempty_hostname(h: &str) -> bool { !h.is_empty() }","tryCatchPattern":null,"preventionTips":["Set node_id explicitly in quickwit config to avoid hostname dependence.","Give containers explicit hostnames (--hostname / pod spec).","Remember split('.').next() is always Some — the real risk is an empty or failed hostname lookup."],"tags":["hostname","invariant","cluster"],"backgroundTag":"internal-invariant-violation","analyzedSha":"a39730c5cdcd1a4fe798403737ae293999ea21f8","analyzedAt":"2026-09-08T13:19:37.784Z","contentChangedAt":"2026-09-08T13:19:37.784Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}