nautechsystems/nautilus_trader · critical

invalid `MarketStatus` enum string value, was '{value}'

Error message

invalid `MarketStatus` enum string value, was '{value}'

What it means

market_status_from_cstr converts a C string into a MarketStatus enum via FromStr. If the string does not match any MarketStatus variant, the function panics with this message (wrapped by abort_on_panic, which aborts the process instead of unwinding across the FFI boundary). This library panics deliberately at the FFI edge because there is no Result-based error channel across the C ABI.

Source

Thrown at crates/model/src/ffi/enums.rs:577

pub extern "C" fn market_status_to_cstr(value: MarketStatus) -> *const c_char {
    str_to_cstr(value.as_ref())
}

/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `MarketStatus` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn market_status_from_cstr(ptr: *const c_char) -> MarketStatus {
    abort_on_panic(|| {
        let value = unsafe { cstr_as_str(ptr) };
        MarketStatus::from_str(value)
            .unwrap_or_else(|_| panic!("invalid `MarketStatus` enum string value, was '{value}'"))
    })
}

#[unsafe(no_mangle)]
pub extern "C" fn market_status_action_to_cstr(value: MarketStatusAction) -> *const c_char {
    str_to_cstr(value.as_ref())
}

/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `MarketStatusAction` variant.
#[unsafe(no_mangle)]

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Print or log the offending '{value}' from the panic message and compare it against the valid MarketStatus variant strings (as defined by MarketStatus::from_str / as_ref in the model enums).
  2. Fix the caller to pass the exact variant spelling (case-sensitive) expected by the Rust enum.
  3. Validate/whitelist the incoming string in the foreign-language code before calling the FFI function.
  4. If the string comes from an external feed, map it to a valid MarketStatus variant explicitly instead of forwarding raw text.
  5. Check for a version mismatch: rebuild the calling code against the same crate version so enum names match.

Example fix

// before
const char* s = "OPEN_MARKET";
MarketStatus st = market_status_from_cstr(s); // panics

// after
const char* s = "open"; // exact MarketStatus variant string
MarketStatus st = market_status_from_cstr(s);
Defensive patterns

Strategy: validation

Validate before calling

const MARKET_STATUS_VARIANTS = new Set(["pre_open", "open", "pause", "pre_close", "close", "post_close", "auction"]);
function isValidMarketStatus(s) { return typeof s === "string" && MARKET_STATUS_VARIANTS.has(s); }
if (!isValidMarketStatus(value)) throw new Error(`unsupported MarketStatus: ${value}`);

Type guard

function isMarketStatus(v) { return typeof v === 'string' && MARKET_STATUS_VARIANTS.has(v); }

Try / catch

Not applicable: the Rust side aborts the process on panic (abort_on_panic), so it cannot be caught in-process. Validate before calling market_status_from_cstr.

Prevention

When it happens

Trigger: Calling the exported C function market_status_from_cstr(ptr) with a pointer to a string that is not a valid MarketStatus variant string, e.g. 'OPEN_MARKET', 'open ', 'trading', an empty string, or a stale/garbage pointer buffer. Any value not produced by MarketStatus.as_ref()/ToString will panic.

Common situations: Passing a Python/other-language enum value whose str() rendering differs from the Rust variant name (e.g. 'MarketStatus.OPEN' or '1'); receiving a status string from a broker feed that uses different vocabulary; upgrading NautilusTrader where a variant was renamed or removed; building the C string with a wrong length so trailing garbage is included.

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/dd45066945d605f7. Report an issue: GitHub.