nautechsystems/nautilus_trader · error

Invalid `ConnectionMode` value: {value}

Error message

Invalid `ConnectionMode` value: {value}

What it means

ConnectionMode::from_u8 maps a raw u8 stored in shared state (e.g. an AtomicU8) back to the enum: 0=Active, 1=Reconnect, 2=Disconnect, 3=Closed. Any other value is outside the representable range and panics with this message. It guards against corrupted or uninitialized state bytes.

Source

Thrown at crates/network/src/mode.rs:78

    /// All associated tasks have been terminated and the connection is no longer available.
    Closed = 3,
}

impl ConnectionMode {
    /// Converts a `u8` loaded from an [`AtomicU8`] into a [`ConnectionMode`].
    ///
    /// # Panics
    ///
    /// Panics if `value` is not a valid `ConnectionMode` discriminant (must be between 0 and 3 inclusive).
    #[inline]
    #[must_use]
    pub fn from_u8(value: u8) -> Self {
        match value {
            0 => Self::Active,
            1 => Self::Reconnect,
            2 => Self::Disconnect,
            3 => Self::Closed,
            _ => panic!("Invalid `ConnectionMode` value: {value}"),
        }
    }

    /// Loads a [`ConnectionMode`] from an [`AtomicU8`] using sequential consistency.
    #[inline]
    #[must_use]
    pub fn from_atomic(value: &AtomicU8) -> Self {
        Self::from_u8(value.load(Ordering::SeqCst))
    }

    /// Atomically transitions to `Reconnect`, but only from `Active`.
    ///
    /// Returns `true` if this call performed the transition. A concurrent
    /// `Disconnect`/`Closed` (or an in-flight `Reconnect`) is left untouched,
    /// so a writer detecting a dead connection cannot resurrect a client that
    /// is being torn down.
    pub fn request_reconnect(value: &AtomicU8) -> bool {
        Self::request_reconnect_outcome(value) == ReconnectRequestOutcome::Accepted

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Only write values produced by ConnectionMode as u8 into the atomic/state
  2. Validate the raw value is <= 3 before calling from_u8
  3. Initialize the AtomicU8 with a valid ConnectionMode (e.g. Active.as_u8()) at construction
  4. If untrusted input, use a checked mapping (match on the u8) instead of the panicking from_u8

Example fix

// before
let mode = ConnectionMode::from_u8(raw);
// after
let mode = if raw <= 3 { ConnectionMode::from_u8(raw) } else { eprintln!("bad connection mode {raw}"); ConnectionMode::Reconnect };
Defensive patterns

Strategy: validation

Validate before calling

fn is_valid_connection_mode_u8(v: u8) -> bool { v <= 3 }

Try / catch

let mode = u8::try_from(raw).ok().filter(|v| *v <= 3).map(ConnectionMode::from_u8).unwrap_or(ConnectionMode::Active);

Prevention

When it happens

Trigger: Calling ConnectionMode::from_u8(v) with v > 3; loading an AtomicU8 that was never initialized with a valid ConnectionMode value; memory/state corruption or writing raw integers into the atomic from another component.

Common situations: Restoring connection state from a file/DB where an invalid byte was persisted; a bug writing an out-of-range mode into the shared AtomicU8; manual construction of mode values in tests or bindings passing e.g. 255.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/35bea074e806f726. Report an issue: GitHub.