{"record":{"id":"44d5db0a04e978aa","repo":"nautechsystems/nautilus_trader","slug":"failed-to-terminate-bybit-execution-session-tasks","errorCode":null,"errorMessage":"Failed to terminate Bybit execution session tasks: {e}","messagePattern":"Failed to terminate Bybit execution session tasks: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/bybit/src/execution.rs","lineNumber":279,"sourceCode":"        self.session_tasks.begin_shutdown();\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 Bybit 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| {\n                anyhow::anyhow!(\"Failed to terminate Bybit execution session tasks: {e}\")\n            })?;\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        self.http_client.cancel_all_requests();\n        self.ws_private.begin_shutdown();\n        self.ws_trade.begin_shutdown();\n\n        if let Err(e) = self.ws_private.close().await {\n            self.shutdown_errors\n                .push(format!(\"private WebSocket shutdown failed: {e}\"));\n        }\n\n        if let Err(e) = self.ws_trade.close().await {\n            self.shutdown_errors","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/bybit/src/execution.rs#L261-L297","documentation":"Same shutdown guard as the pending tasks but for session tasks (WebSocket session workers). If finish_shutdown times out or errors while stopping session tasks within the 1s/2s windows, the client bails with this message.","triggerScenarios":"Tearing down BybitExecutionClient while private/trade WebSocket session tasks are still busy or hung.","commonSituations":"Unresponsive WebSocket connections, handlers awaiting network I/O during shutdown, abrupt network loss right before disconnect.","solutions":["Retry teardown after a short delay","Check for blocking or long-await loops in session task code","Look at the wrapped inner error to distinguish timeout vs cancellation failure","Ensure network connectivity or force client drop if tasks are stuck"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if let Err(e) = client.disconnect().await {\n    if e.to_string().contains(\"execution session tasks\") {\n        tracing::warn!(\"session tasks did not stop in time: {e}\");\n        // proceed with drop; tasks die with the runtime\n    } else {\n        return Err(e);\n    }\n}","preventionTips":["Keep WebSocket session loops cancellation-friendly (select! on shutdown signals)","Avoid unbounded awaits on network reads inside session tasks","Test disconnect under simulated network stall"],"tags":["bybit","shutdown","websocket","timeout"],"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"}