nautechsystems/nautilus_trader · info

writing to String should not fail

Error message

writing to String should not fail

What it means

This panic comes from `.expect("writing to String should not fail")` on the result of `write!(buf, ...)` in `write_fixed_prefix` in crates/common/src/generators/order_list_id.rs. Formatting into a `String` via `std::fmt::Write` is infallible in practice — a `String` never fails to grow except on allocation failure — so the code asserts this invariant. The panic indicates an impossible formatter state, essentially an internal invariant violation.

Source

Thrown at crates/common/src/generators/order_list_id.rs:163

        Timestamp::from_second(
            i64::try_from(epoch_second).expect("seconds timestamp should fit i64"),
        )
        .expect("seconds timestamp should be within valid range"),
    );

    buf.clear();

    write!(
        buf,
        "OL-{:04}{:02}{:02}-{:02}{:02}{:02}-{trader_tag}-{strategy_tag}-",
        now_utc.year(),
        now_utc.month(),
        now_utc.day(),
        now_utc.hour(),
        now_utc.minute(),
        now_utc.second(),
    )
    .expect("writing to String should not fail");
}

#[cfg(test)]
mod tests {
    use std::{cell::RefCell, rc::Rc};

    use nautilus_core::UnixNanos;
    use nautilus_model::{
        identifiers::{OrderListId, StrategyId, TraderId},
        stubs::TestDefault,
    };
    use rstest::rstest;

    use crate::{clock::TestClock, generators::order_list_id::OrderListIdGenerator};

    fn get_order_list_id_generator(initial_count: Option<usize>) -> OrderListIdGenerator {
        let clock = Rc::new(RefCell::new(TestClock::new()));
        OrderListIdGenerator::new(

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check system memory conditions if this genuinely fires; it signals OOM, not bad input
  2. Do not attempt to handle it — it is a deliberate internal invariant assertion
  3. If you need graceful degradation, use `write!` result handling upstream instead of expect (requires library change)

Example fix

null
Defensive patterns

Strategy: fallback

Try / catch

// String fmt::Write is infallible except on OOM; no meaningful catch pattern
let _ = write!(buf, "..."); // or keep expect as internal invariant

Prevention

When it happens

Trigger: Calling `refresh_fixed_prefix` triggers the write!; the expect only fires if the fmt::Write impl for String errors, which in practice only happens on memory allocation failure (OOM).

Common situations: Effectively never hit by users; could surface under extreme memory exhaustion, or indicate corrupted inputs such as a trader/strategy tag with invalid formatting width specifiers (not possible with plain Display).

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 nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/fbfdf5d2bc0a7c62. Report an issue: GitHub.