{"record":{"id":"b7016c9fd2fa0ea3","repo":"nautechsystems/nautilus_trader","slug":"failed-to-terminate-binance-futures-session-tasks","errorCode":null,"errorMessage":"Failed to terminate Binance Futures session tasks: {e}","messagePattern":"Failed to terminate Binance Futures session tasks: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/binance/src/futures/execution.rs","lineNumber":916,"sourceCode":"    {\n        crate::common::execution::spawn_task(&self.pending_tasks, description, fut);\n    }\n\n    fn abort_pending_tasks(&self) {\n        crate::common::execution::abort_pending_tasks(&self.pending_tasks);\n    }\n\n    fn abort_session_tasks(&self) {\n        self.session_tasks.begin_shutdown();\n    }\n\n    async fn await_pending_tasks(&self) -> anyhow::Result<()> {\n        crate::common::execution::await_pending_tasks(&self.pending_tasks).await\n    }\n\n    async fn await_session_tasks(&self) -> anyhow::Result<()> {\n        self.finish_session_tasks().await.map_err(|e| {\n            anyhow::anyhow!(\"Failed to terminate Binance Futures session tasks: {e}\")\n        })?;\n        Ok(())\n    }\n\n    async fn finish_session_tasks(&self) -> Result<(), TaskShutdownError> {\n        self.session_tasks.begin_shutdown();\n        self.session_tasks\n            .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2))\n            .await?;\n        Ok(())\n    }\n\n    async fn await_dispatch_task(&self) -> anyhow::Result<()> {\n        let _recovery_guard = self.recovery_lock.lock().await;\n        let mut task_slot = self.ws_task.lock().await;\n        let Some(outcome) = finish_task(\n            &mut task_slot,\n            Duration::from_secs(1),","sourceCodeStart":898,"sourceCodeEnd":934,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/binance/src/futures/execution.rs#L898-L934","documentation":"During disconnect/shutdown, await_session_tasks calls finish_session_tasks to shut down the WebSocket session tasks; if shutdown completes with an error (TaskShutdownError, e.g. tasks not stopping within the timeout), it is wrapped in this anyhow error. It signals the Binance Futures data/execution client could not cleanly terminate its session tasks.","triggerScenarios":"Calling disconnect (or connect's failure path) while session tasks are stuck — e.g. a WebSocket stream task blocked on a hung network read or a handler not honoring the shutdown signal — causing finish_session_tasks to time out or fail.","commonSituations":"Network partitions leaving WS reads blocked, slow/stalled event loop under load, or a bug in a task that ignores abort signals during node shutdown.","solutions":["Retry the disconnect; transient stalls often resolve once the network recovers","Check network/proxy stability that may keep WebSocket reads blocked past the shutdown timeout","Upgrade to the latest nautilus_trader version for shutdown-timeout fixes","Capture and inspect the inner TaskShutdownError via the error chain to see which task failed to stop","Force-drop the node/kernel as a last resort if tasks are wedged"],"exampleFix":"// before\nclient.disconnect().await?; // may surface 'Failed to terminate Binance Futures session tasks'\n// after\nif let Err(e) = client.disconnect().await {\n    tracing::warn!(\"session task shutdown failed: {e:#}; retrying\");\n    client.disconnect().await?;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match client.disconnect().await {\n    Err(e) if e.to_string().contains(\"Failed to terminate Binance Futures session tasks\") => {\n        tracing::warn!(\"graceful shutdown failed: {e:#}; retrying\");\n        client.disconnect().await?;\n    }\n    other => other?,\n}","preventionTips":["Keep network paths healthy so WS reads don't hang past shutdown timeouts","Upgrade nautilus_trader regularly for shutdown fixes","Log the full error chain ({e:#}) to identify the stuck task"],"tags":["binance","shutdown","websocket","task-timeout"],"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-14T00:17:10.932Z"}