quickwit-oss/quickwit · info

Split should never fail.

Error message

Split should never fail.

What it means

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.

Source

Thrown at quickwit/quickwit-common/src/net.rs:338

}

// Inner function for testing purposes.
fn _get_hostname(hostname: OsString) -> io::Result<String> {
    let hostname_lossy = hostname.to_string_lossy();
    if is_valid_hostname(&hostname_lossy) {
        Ok(hostname_lossy.to_string())
    } else {
        Err(io::Error::other(format!(
            "invalid hostname: `{hostname_lossy}`"
        )))
    }
}

pub fn get_short_hostname() -> io::Result<String> {
    Ok(get_hostname()?
        .split('.')
        .next()
        .expect("Split should never fail.")
        .to_string())
}

/// Returns whether a hostname is valid according to [RFC 1123](https://www.rfc-editor.org/rfc/rfc1123).
///
/// A hostname is valid if the following conditions are met:
///
/// - It does not start or end with `-` or `.`.
/// - It does not contain any characters outside of the alphanumeric range, except for `-` and `.`.
/// - It is not empty.
/// - It is 253 or fewer characters.
/// - Its labels (characters separated by `.`) are not empty.
/// - Its labels are 63 or fewer characters.
/// - Its labels do not start or end with '-' or '.'.
pub fn is_valid_hostname(hostname: &str) -> bool {
    if hostname.is_empty() || hostname.len() > 253 {
        return false;
    }

View on GitHub (pinned to a39730c5cd)

Solutions

  1. If the related hostname error occurs, set an explicit hostname in your container/runtime (`docker run --hostname ...`, `--name` in K8s pods).
  2. Or configure node_id explicitly in the quickwit config to bypass hostname discovery.
  3. For the expect itself, no fix needed — it is a sound invariant; replace with unwrap_or_default only if silencing lints.

Example fix

// before
Ok(get_hostname()?.split('.').next().expect("Split should never fail.").to_string())
// after
Ok(get_hostname()?.split('.').next().unwrap_or_default().to_string())
Defensive patterns

Strategy: type-guard

Validate before calling

let hostname = std::env::var("HOSTNAME").or_else(|_| hostname_cmd()).unwrap_or_default();
assert!(!hostname.is_empty(), "hostname must be set for default node_id");

Type guard

fn has_nonempty_hostname(h: &str) -> bool { !h.is_empty() }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of quickwit-oss/quickwit@a39730c5cd (2026-09-08). Data as JSON: /api/errors/3949bd4ec8ce2b34. Report an issue: GitHub.