{"record":{"id":"fa8347d8c64dc4ad","repo":"nautechsystems/nautilus_trader","slug":"failed-to-terminate-betfair-execution-tasks-e","errorCode":null,"errorMessage":"Failed to terminate Betfair execution tasks: {e}","messagePattern":"Failed to terminate Betfair execution tasks: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/betfair/src/execution.rs","lineNumber":527,"sourceCode":"\n        self.reconciliation_gate.clear();\n    }\n\n    fn abort_pending_tasks(&self) {\n        self.pending_tasks.begin_shutdown();\n    }\n\n    fn abort_session_tasks(&mut self) {\n        self.session_tasks.begin_shutdown();\n        self.account_refresh_tx = None;\n    }\n\n    async fn await_pending_tasks(&self) -> anyhow::Result<()> {\n        self.pending_tasks.begin_shutdown();\n        self.pending_tasks\n            .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2))\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to terminate Betfair execution tasks: {e}\"))?;\n        Ok(())\n    }\n\n    async fn await_session_tasks(&self) -> anyhow::Result<()> {\n        self.session_tasks.begin_shutdown();\n        self.session_tasks\n            .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2))\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to terminate Betfair session tasks: {e}\"))?;\n        Ok(())\n    }\n\n    async fn teardown_partial_connect(&mut self) -> anyhow::Result<()> {\n        self.abort_session_tasks();\n        self.abort_pending_tasks();\n\n        if let Some(control) = &self.socket_control {\n            control.deregister();","sourceCodeStart":509,"sourceCodeEnd":545,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/betfair/src/execution.rs#L509-L545","documentation":"await_pending_tasks shuts down the Betfair execution client's pending-task group via begin_shutdown/finish_shutdown with 1s and 2s grace durations. If finish_shutdown cannot terminate the tasks within those windows or returns an error, it is wrapped in this anyhow error.","triggerScenarios":"In-flight HTTP order/account tasks not completing within the 1s grace + 2s timeout windows during teardown; tasks stuck awaiting unresponsive Betfair API responses.","commonSituations":"Disconnecting while Betfair API is slow or hung; network outage at shutdown; placing orders immediately before disconnect so tasks are still in flight.","solutions":["Increase the shutdown grace/timeout durations if legitimate in-flight work is common","Drain or cancel long-running requests before calling await_pending_tasks","Check network/API health; the root cause is usually tasks blocked on unresponsive Betfair endpoints"],"exampleFix":"// before\nself.pending_tasks.finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)).await?;\n// after\nself.pending_tasks.finish_shutdown(Duration::from_secs(5), Duration::from_secs(10)).await?;","handlingStrategy":"try-catch","validationCode":"// check no order tasks in flight before shutdown\nif !self.pending_tasks.is_idle() { log::warn!(\"tasks still in flight at shutdown\"); }","typeGuard":null,"tryCatchPattern":"if let Err(e) = exec_client.await_pending_tasks().await {\n    log::error!(\"pending task shutdown: {e}; forcing teardown\");\n    exec_client.abort_pending_tasks();\n}","preventionTips":["Avoid placing orders right before disconnect","Allow adequate grace time for slow networks","Monitor Betfair API latency at shutdown"],"tags":["rust","betfair","shutdown","timeout","task-cleanup"],"backgroundTag":"request-timeout","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"}