{"record":{"id":"bab7b84a57310883","repo":"nautechsystems/nautilus_trader","slug":"failed-to-start-binance-futures-session-generation","errorCode":null,"errorMessage":"Failed to start Binance Futures session generation: {e}","messagePattern":"Failed to start Binance Futures session generation: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/binance/src/futures/execution.rs","lineNumber":1700,"sourceCode":"            return Ok(());\n        }\n\n        if !self.pending_tasks.is_open() || !self.session_tasks.is_open() {\n            self.disconnect().await?;\n        }\n\n        if !self.pending_tasks.is_open() {\n            self.await_pending_tasks().await?;\n            self.pending_tasks.start_generation().map_err(|e| {\n                anyhow::anyhow!(\"Failed to start Binance Futures task generation: {e}\")\n            })?;\n        }\n\n        if !self.session_tasks.is_open() {\n            self.await_session_tasks().await?;\n            self.await_dispatch_task().await?;\n            self.session_tasks.start_generation().map_err(|e| {\n                anyhow::anyhow!(\"Failed to start Binance Futures session generation: {e}\")\n            })?;\n        }\n\n        self.cancellation_token = CancellationToken::new();\n        let cancellation_token = self.cancellation_token.clone();\n        let ws_client = Arc::clone(&self.ws_client);\n        let ws_trading_client = self.ws_trading_client.clone();\n        let setup_guard =\n            TaskGroupGuard::new(&[&self.session_tasks, &self.pending_tasks], move || {\n                cancellation_token.cancel();\n\n                if let Some(client) = ws_client.lock().as_ref() {\n                    client.begin_shutdown();\n                }\n\n                if let Some(client) = ws_trading_client {\n                    client.begin_shutdown();\n                }","sourceCodeStart":1682,"sourceCodeEnd":1718,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/binance/src/futures/execution.rs#L1682-L1718","documentation":"Raised in BinanceFuturesExecutionClient::connect when the session-tasks tokio TaskGroup fails to start a new generation, after awaiting session tasks and the user-stream dispatch task. Like the pending-task generation error, it means the client's internal background-task infrastructure could not be (re)armed, so connection setup aborts. It usually signals the task group is in a bad lifecycle state or a task failed during startup.","triggerScenarios":"connect() called while session_tasks group is closed and await_session_tasks()/await_dispatch_task() complete but session_tasks.start_generation() fails — e.g. after an unclean prior disconnect, a task panic, or runtime shutdown concurrent with connect.","commonSituations":"Restarting a live node after an error without full teardown, reconnect loops that do not await proper disconnect, or stopping the trader while the WebSocket session tasks are still winding down.","solutions":["Perform a full disconnect() before each reconnect attempt so both task groups restart cleanly.","Inspect the wrapped inner error `{e}` and the preceding logs to find the failing/parked task.","Avoid calling connect() concurrently from multiple places; serialize connection attempts.","Recreate the execution client if the task groups remain permanently closed."],"exampleFix":"// before\n// reconnect loop\nclient.connect().await?;\n\n// after\nclient.disconnect().await?;\ntokio::time::sleep(Duration::from_secs(1)).await;\nclient.connect().await?;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match client.connect().await {\n    Err(e) if e.to_string().contains(\"session generation\") => {\n        client.disconnect().await?;\n        tokio::time::sleep(Duration::from_secs(1)).await;\n        client.connect().await?;\n    }\n    r => r?,\n}","preventionTips":["Await full disconnect before starting a new session generation","Avoid shutting down the runtime while connect() is in flight","Recreate the client if task groups stay closed after repeated failures"],"tags":["binance","tokio","task-group","session","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"}