nautechsystems/nautilus_trader · error · anyhow::Error

Failed to serialize unsubscription: {e}

Error message

Failed to serialize unsubscription: {e}

What it means

`handle_unsubscribe` serializes the OKXSubscription (op=unsubscribe) to JSON before sending. If `serde_json::to_string` fails, the error is wrapped with this message. As with subscribe, this is near-impossible with well-formed typed args.

Source

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

        Ok(())
    }

    async fn handle_unsubscribe(&self, args: Vec<OKXSubscriptionArg>) -> anyhow::Result<()> {
        for arg in &args {
            log::debug!(
                "Unsubscribing from channel: channel={:?}, inst_id={:?}",
                arg.channel,
                arg.inst_id
            );
        }

        let message = OKXSubscription {
            op: OKXWsOperation::Unsubscribe,
            args,
        };

        let json_txt = serde_json::to_string(&message)
            .map_err(|e| anyhow::anyhow!("Failed to serialize unsubscription: {e}"))?;

        self.send_with_retry(json_txt, Some(OKX_RATE_LIMIT_KEY_SUBSCRIPTION.as_slice()))
            .await
            .map_err(|e| anyhow::anyhow!("Failed to send unsubscription after retries: {e}"))?;
        Ok(())
    }

    pub(crate) fn parse_raw_message(
        msg: tokio_tungstenite::tungstenite::Message,
    ) -> Option<OKXWsFrame> {
        match msg {
            tokio_tungstenite::tungstenite::Message::Text(text) => {
                if text == TEXT_PONG {
                    log::trace!("Received pong from OKX");
                    return None;
                }

                if text == TEXT_PING {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check the inner `{e}` and the args passed; ensure channel/inst_id values are plain strings.
  2. Use the adapter's public unsubscribe API rather than hand-building OKXSubscriptionArg values.
  3. Report as a bug if it reproduces with standard args.
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: `serde_json::to_string(&OKXSubscription{op: Unsubscribe, args})` returns Err during handle_unsubscribe — malformed or unserializable unsubscribe args.

Common situations: Programmatically constructed unsubscribe args containing invalid values; virtually never seen with the adapter's normal unsubscribe API.

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