{"record":{"id":"31eaec150cc0dea7","repo":"nautechsystems/nautilus_trader","slug":"failed-to-send-addbartype-command-e","errorCode":null,"errorMessage":"Failed to send AddBarType command: {e}","messagePattern":"Failed to send AddBarType command: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/hyperliquid/src/websocket/client.rs","lineNumber":1602,"sourceCode":"        let interval = bar_type_to_interval(&bar_type)?;\n        let subscription = SubscriptionRequest::Candle { coin, interval };\n\n        // Cache the bar type for parsing using canonical key\n        let key = format!(\"candle:{coin}:{interval}\");\n        self.bar_types.insert(key.clone(), bar_type);\n\n        let cmd_tx = self.cmd_tx.read().await;\n\n        cmd_tx\n            .send(HandlerCommand::UpdateInstrument(instrument.clone()))\n            .map_err(|e| anyhow::anyhow!(\"Failed to send UpdateInstrument command: {e}\"))?;\n\n        cmd_tx\n            .send(HandlerCommand::AddBarType {\n                key: key.clone(),\n                bar_type,\n            })\n            .map_err(|e| anyhow::anyhow!(\"Failed to send AddBarType command: {e}\"))?;\n\n        if let Err(e) = self.send_subscription(&cmd_tx, subscription) {\n            self.bar_types.remove(&key);\n            let _ = cmd_tx.send(HandlerCommand::RemoveBarType { key });\n            return Err(e);\n        }\n        Ok(())\n    }\n\n    /// Subscribe to funding rate updates for an instrument.\n    pub async fn subscribe_funding_rates(&self, instrument_id: InstrumentId) -> anyhow::Result<()> {\n        self.subscribe_asset_context_data(instrument_id, AssetContextDataType::FundingRate)\n            .await\n    }\n\n    /// Subscribe to open interest updates for an instrument.\n    pub async fn subscribe_open_interest(&self, instrument_id: InstrumentId) -> anyhow::Result<()> {\n        self.subscribe_asset_context_data(instrument_id, AssetContextDataType::OpenInterest)","sourceCodeStart":1584,"sourceCodeEnd":1620,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/hyperliquid/src/websocket/client.rs#L1584-L1620","documentation":"This error wraps a failed send of AddBarType on the internal mpsc command channel during bar subscription. After registering the instrument, the client sends the BarType key to the handler so incoming bar data is routed; failure means the handler task's receiver is closed. The client rolls back its local bar_types registry before returning this error.","triggerScenarios":"Calling subscribe_bars when the handler task has exited between the UpdateInstrument send and the AddBarType send, or already before — after disconnect(), handler panic, or task cancellation.","commonSituations":"Shutdown racing a bar subscribe; handler task aborted via its JoinHandle; stale client instance reused post-teardown; handler panic on prior traffic.","solutions":["Verify the handler task is alive before subscribing; respawn/reconnect the client if not.","Do not call subscribe_bars concurrently with disconnect/shutdown.","If the handler loop exited, create a fresh client; the channel cannot be reopened.","Check handler-task logs for the root cause of the exit."],"exampleFix":"// before\nclient.disconnect().await?;\nclient.subscribe_bars(bar_type).await?; // channel closed\n\n// after\nclient.subscribe_bars(bar_type).await?;\nclient.disconnect().await?;","handlingStrategy":"try-catch","validationCode":"// rust\nif !client.is_connected() {\n    return Err(anyhow::anyhow!(\"cannot subscribe bars: handler channel closed\"));\n}","typeGuard":null,"tryCatchPattern":"// rust\nif let Err(e) = client.subscribe_bars(bar_type).await {\n    if e.to_string().contains(\"AddBarType\") {\n        tracing::warn!(\"bar subscribe lost handler ({e}); reconnecting\");\n        client = reconnect_client().await?;\n        client.subscribe_bars(bar_type).await?;\n    } else { return Err(e); }\n}","preventionTips":["Sequence bar subscribe before any disconnect call.","Keep the handler task alive for the whole session, not per-request.","Watch for handler-task panics via panic hooks/logging.","After a closed-channel error, discard the client and build a fresh one."],"tags":["rust","tokio","websocket","channel-closed","hyperliquid"],"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"}