{"record":{"id":"b665208caf4ee4d3","repo":"nautechsystems/nautilus_trader","slug":"failed-to-send-subscribe-command-e-b66520","errorCode":null,"errorMessage":"Failed to send Subscribe command: {e}","messagePattern":"Failed to send Subscribe command: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/coinbase/src/websocket/client.rs","lineNumber":424,"sourceCode":"        let channel_str = channel.as_ref();\n\n        if product_ids.is_empty() {\n            self.subscriptions.mark_subscribe(channel_str);\n        } else {\n            for product_id in product_ids {\n                let topic = format!(\"{channel_str}|{product_id}\");\n                self.subscriptions.mark_subscribe(&topic);\n            }\n        }\n\n        let cmd_tx = self.cmd_tx.read().await;\n        cmd_tx\n            .send(HandlerCommand::Subscribe {\n                channel: sub.channel,\n                product_ids: sub.product_ids,\n                payload: sub.payload,\n            })\n            .map_err(|e| anyhow::anyhow!(\"Failed to send Subscribe command: {e}\"))\n    }\n\n    /// Unsubscribes from a channel for the given product IDs.\n    pub async fn unsubscribe(\n        &self,\n        channel: CoinbaseWsChannel,\n        product_ids: &[Ustr],\n    ) -> anyhow::Result<()> {\n        let jwt = self.credential.as_ref().and_then(|c| c.build_ws_jwt().ok());\n\n        let unsub = protect_subscription(CoinbaseWsSubscription {\n            msg_type: CoinbaseWsAction::Unsubscribe,\n            product_ids: product_ids.to_vec(),\n            channel,\n            jwt,\n        })?;\n\n        let channel_str = channel.as_ref();","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/coinbase/src/websocket/client.rs#L406-L442","documentation":"`subscribe` dispatches the subscription to the internal handler task over an mpsc command channel. If the send fails (the receiver/handler task has already terminated), the error is wrapped as 'Failed to send Subscribe command'. This indicates the client's background handler is no longer running.","triggerScenarios":"Calling `subscribe` after the websocket handler task exited (connection dropped permanently, client shut down, or the task panicked), so `cmd_tx.send(HandlerCommand::Subscribe)` returns a SendError.","commonSituations":"Reusing a client after `close`/drop of the handler, subscribe racing with shutdown during app teardown, or handler task crash on malformed state.","solutions":["Check that the client/handler task is still running before subscribing; recreate the client if it was shut down","Retry connect() to restart the task generation, then resubscribe","Inspect logs for handler task panics or disconnects preceding the error","Guard the shutdown path so subscribe is not called concurrently with close"],"exampleFix":"// before\nclient.subscribe(channel, &products).await?;\n// after\nif client.is_closed() {\n    client.reconnect().await?;\n}\nclient.subscribe(channel, &products).await?;","handlingStrategy":"try-catch","validationCode":"async fn ensure_active(client: &CoinbaseWsClient) -> Result<(), anyhow::Error> {\n    if client.is_closed() {\n        anyhow::bail!(\"websocket client already closed; reconnect before subscribing\");\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"match client.subscribe(channel, &products).await {\n    Err(e) if e.to_string().contains(\"Failed to send Subscribe command\") => {\n        client.reconnect().await?;\n        client.subscribe(channel, &products).await?;\n    }\n    other => other?,\n}","preventionTips":["Keep a single owner of client lifecycle; never subscribe after close()","Handle reconnects centrally so handler-task death always triggers recreation","Watch for handler task panics in logs; they surface later as subscribe send failures"],"tags":["coinbase","websocket","channel","lifecycle"],"backgroundTag":"broken-pipe","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}