{"record":{"id":"f2bb46d0f4de5435","repo":"nautechsystems/nautilus_trader","slug":"failed-to-start-betfair-data-command-tasks-e","errorCode":null,"errorMessage":"Failed to start Betfair data command tasks: {e}","messagePattern":"Failed to start Betfair data command tasks: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/betfair/src/data.rs","lineNumber":242,"sourceCode":"            self.command_tasks\n                .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),\n        );\n        session_result\n            .map_err(|e| anyhow::anyhow!(\"Failed to finish Betfair data session tasks: {e}\"))?;\n        command_result\n            .map_err(|e| anyhow::anyhow!(\"Failed to finish Betfair data command tasks: {e}\"))?;\n        Ok(())\n    }\n\n    async fn prepare_task_groups(&mut self) -> anyhow::Result<()> {\n        if !self.session_tasks.is_open() || !self.command_tasks.is_open() {\n            self.teardown_partial_connect().await?;\n            self.session_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Betfair data session tasks: {e}\"))?;\n            self.command_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Betfair data command tasks: {e}\"))?;\n        }\n        Ok(())\n    }\n\n    fn begin_stream_shutdown(&self) {\n        for stream in self.stream_shutdowns.lock().iter() {\n            stream.begin_shutdown();\n        }\n    }\n\n    async fn teardown_partial_connect(&mut self) -> anyhow::Result<()> {\n        self.session_tasks.begin_shutdown();\n        self.command_tasks.begin_shutdown();\n        self.begin_stream_shutdown();\n        self.is_connected.store(false, Ordering::Relaxed);\n\n        if let Some(client) = self.cricket_stream_client.as_ref() {\n            client.close().await;","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/betfair/src/data.rs#L224-L260","documentation":"Identical to error 1751 but for the command TaskGroup: command_tasks.start_generation() failed in prepare_task_groups. The command group hosts control-plane tasks (subscribes, commands); without a new generation, connect cannot spawn them.","triggerScenarios":"connect() -> prepare_task_groups detects command_tasks.is_open() == false after teardown_partial_connect, then start_generation() returns Err because the group is not in a restartable state.","commonSituations":"Reconnect after a disconnect where the command group shutdown did not finalize; overlapping connect attempts; leftover shutdown errors from a prior failed teardown.","solutions":["Ensure the prior disconnect fully drained command tasks before reconnecting (finish_tasks completed without error)","Read the inner `{e}` for the exact TaskGroup state reason","Serialize connect/disconnect calls; do not run them concurrently on the same client","If the group is permanently wedged, rebuild the BetfairDataClient"],"exampleFix":"// before\nself.command_tasks.start_generation().map_err(|e| anyhow::anyhow!(\"Failed to start Betfair data command tasks: {e}\"))?;\n// after\nif !self.command_tasks.is_open() {\n    self.command_tasks.start_generation().map_err(|e| anyhow::anyhow!(\"Failed to start Betfair data command tasks: {e}\"))?;\n}","handlingStrategy":"retry","validationCode":"// ensure no connect already in flight\nassert!(!client.is_connected(), \"previous connect still active\");","typeGuard":null,"tryCatchPattern":"match client.connect().await {\n    Err(e) if e.to_string().contains(\"Failed to start Betfair data command tasks\") => {\n        tokio::time::sleep(Duration::from_secs(2)).await;\n        client.disconnect().await.ok();\n        // then retry connect\n    }\n    Err(e) => return Err(e),\n    Ok(()) => {},\n}","preventionTips":["Serialize connect/disconnect with a mutex or actor-style ownership","Complete prior shutdowns before reconnecting","Back off between attempts to let TaskGroup shutdown drain","Rebuild the client if the command group stays wedged"],"tags":["rust","async","task-group","reconnect","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}