{"record":{"id":"1674ef2f8752da62","repo":"nautechsystems/nautilus_trader","slug":"failed-to-start-coinbase-session-generation-e","errorCode":null,"errorMessage":"Failed to start Coinbase session generation: {e}","messagePattern":"Failed to start Coinbase session generation: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/coinbase/src/execution.rs","lineNumber":388,"sourceCode":"        }\n\n        if !self.pending_tasks.is_open() {\n            self.await_pending_tasks().await?;\n            self.pending_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Coinbase task generation: {e}\"))?;\n        }\n\n        if !self.session_tasks.is_open() || !self.session_tasks.is_empty() {\n            self.abort_session_tasks();\n            self.ws_user\n                .disconnect()\n                .await\n                .context(\"failed to close stale Coinbase user WebSocket\")?;\n            self.await_session_tasks().await?;\n            self.session_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Coinbase session generation: {e}\"))?;\n        }\n        let ws_user = self.ws_user.clone();\n        let setup_guard =\n            TaskGroupGuard::new(&[&self.session_tasks, &self.pending_tasks], move || {\n                ws_user.begin_shutdown();\n            });\n\n        // If the underlying WS is still alive from a prior stop() that did not\n        // explicitly disconnect, tear it down before reconnecting. The\n        // in-handler signal path can race with the Disconnect command, leaving\n        // the inner connection_mode stale even after disconnect().await, so\n        // we rebuild the client outright to guarantee clean cmd_tx/out_rx\n        // pairs and a fresh signal.\n        if self.ws_user.is_active() || self.ws_user.is_reconnecting() {\n            log::debug!(\"Tearing down stale user WS before reconnect\");\n            self.ws_user\n                .disconnect()\n                .await","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/coinbase/src/execution.rs#L370-L406","documentation":"Raised in CoinbaseExecutionClient::connect when TaskGroup::start_generation() fails while re-establishing the user WebSocket session after closing a stale one. It wraps the underlying task-group error (e.g. a generation is already running or the group was shut down) into an anyhow error that aborts the connect flow.","triggerScenarios":"Calling connect() on a Coinbase execution client whose session_tasks TaskGroup is already in a running generation, or in a state (stopping/stopped) that refuses a new generation start.","commonSituations":"Double-connecting the same client (e.g. reconnect logic racing with an existing connection), restarting a live node without awaiting the prior disconnect, or framework lifecycle code calling connect twice.","solutions":["Ensure disconnect() is fully awaited before calling connect() again so the previous session generation ends.","Check the wrapped {e} source for 'already started' style TaskGroup errors and serialize your connect calls.","Await await_session_tasks()/join on prior session tasks before reconnecting.","If the client is permanently stopped, create a fresh client instance instead of reconnecting."],"exampleFix":"// before\nclient.connect().await?;\n// after (serialize reconnects)\nif client.is_connected() { client.disconnect().await?; }\nclient.connect().await?;","handlingStrategy":"try-catch","validationCode":"if client_is_connected() { tokio::time::timeout(Duration::from_secs(10), client.disconnect()).await.expect(\"disconnect timed out\"); }","typeGuard":"fn can_start_generation(joined: bool, stopped: bool) -> bool { !joined && !stopped }","tryCatchPattern":"match client.connect().await {\n    Ok(_) => {},\n    Err(e) if e.to_string().contains(\"session generation\") => { /* serialize reconnect: wait for prior tasks, then retry once */ }\n    Err(e) => return Err(e),\n}","preventionTips":["Always await disconnect() fully before reconnecting.","Serialize connect calls with a mutex/once flag per client.","Log and join session tasks during shutdown paths."],"tags":["websocket","task-group","connection-lifecycle","rust"],"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"}