nautechsystems/nautilus_trader · warning

Invalid OKX websocket channel

Error message

Invalid OKX websocket channel

What it means

The OKX websocket handler deserializes a parsed channel enum back to a JSON string with serde_json::to_string when logging connection-status events. The expect panics if serializing the channel value fails, which should be impossible for an enum that was just parsed from JSON; it is an internal invariant assertion labeled 'Invalid OKX websocket channel'.

Source

Thrown at crates/adapters/okx/src/websocket/handler.rs:636

                            arg,
                            conn_id,
                            ..
                        } => {
                            let channel_str = serde_json::to_string(&arg.channel)
                                .expect("Invalid OKX websocket channel")
                                .trim_matches('"')
                                .to_string();
                            log::debug!("{event}d: channel={channel_str}, conn_id={conn_id}");
                            Some(ws_event)
                        }
                        OKXWsFrame::ChannelConnCount {
                            channel,
                            conn_count,
                            conn_id,
                            ..
                        } => {
                            let channel_str = serde_json::to_string(channel)
                                .expect("Invalid OKX websocket channel")
                                .trim_matches('"')
                                .to_string();
                            log::debug!(
                                "Channel connection status: \
                                 channel={channel_str}, connections={conn_count}, conn_id={conn_id}",
                            );
                            None
                        }
                        OKXWsFrame::Ping => {
                            log::trace!("Ignoring ping event parsed from text payload");
                            None
                        }
                        OKXWsFrame::Data { .. }
                        | OKXWsFrame::BookData { .. }
                        | OKXWsFrame::RpiBookData { .. } => Some(ws_event),
                        OKXWsFrame::OrderResponse {
                            id, op, code, data, ..
                        } => {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Update the nautilus OKX adapter to match the current OKX websocket API event schema.
  2. Log the channel with Debug (`{:?}`) instead of re-serializing to JSON if you patch locally.
  3. Report the raw payload if it persists, since this path is an internal invariant and should not panic.

Example fix

// before
let channel_str = serde_json::to_string(channel)
    .expect("Invalid OKX websocket channel")
    .trim_matches('"')
    .to_string();

// after
let channel_str = format!("{:?}", channel); // cannot panic
Defensive patterns

Strategy: try-catch

Try / catch

// treat as an internal invariant: catch at the ws message loop boundary
match std::panic::catch_unwind(|| handler.parse_raw_message(raw.clone())) {
    Ok(v) => v,
    Err(_) => { log::error!("unparseable OKX channel event: {raw}"); continue; }
}

Prevention

When it happens

Trigger: Receiving a websocket 'channel-conn-count' style event whose channel variant fails JSON serialization in parse_raw_message; practically only reachable if the channel enum's Serialize impl is broken or a non-serializable representation is produced upstream.

Common situations: OKX changing event payload shapes after an adapter update; locally patched enum variants that don't round-trip; corrupted or malformed frames from proxies.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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