{"record":{"id":"fd66b93394f45b58","repo":"nautechsystems/nautilus_trader","slug":"failed-to-start-derive-data-session-generation-e","errorCode":null,"errorMessage":"Failed to start Derive data session generation: {e}","messagePattern":"Failed to start Derive data session generation: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/derive/src/data.rs","lineNumber":745,"sourceCode":"\n    async fn connect(&mut self) -> anyhow::Result<()> {\n        if self.is_connected()\n            && !self.cancellation_token.is_cancelled()\n            && self.session_tasks.is_open()\n            && self.pending_tasks.is_open()\n        {\n            return Ok(());\n        }\n\n        // Completes the async teardown deferred by sync reset()/stop().\n        if self.cancellation_token.is_cancelled()\n            || !self.session_tasks.is_open()\n            || !self.pending_tasks.is_open()\n        {\n            self.teardown_partial_connect().await?;\n            self.cancellation_token = CancellationToken::new();\n            self.session_tasks.start_generation().map_err(|e| {\n                anyhow::anyhow!(\"Failed to start Derive data session generation: {e}\")\n            })?;\n            self.pending_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Derive data task generation: {e}\"))?;\n        }\n        let cancellation_token = self.cancellation_token.clone();\n        let ws_shutdown = self.ws_client.shutdown_handle();\n        let setup_guard =\n            TaskGroupGuard::new(&[&self.session_tasks, &self.pending_tasks], move || {\n                cancellation_token.cancel();\n                ws_shutdown.begin_shutdown();\n            });\n\n        if !self.config.currencies.is_empty() {\n            self.provider\n                .load_all(None)\n                .await\n                .context(\"failed to load Derive instruments\")?;","sourceCodeStart":727,"sourceCodeEnd":763,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/derive/src/data.rs#L727-L763","documentation":"During the Derive data client's `connect`, if the session/pending task groups are not in a clean state, it tears down partial state and starts a new generation for `session_tasks`. This error wraps a failure of that generation start, blocking the data client from establishing its WebSocket session task set.","triggerScenarios":"Calling `connect()` on the Derive data client when `session_tasks` is not open/empty and `start_generation()` returns Err (group shut down, generation state invalid, prior teardown incomplete).","commonSituations":"Reconnecting after an aborted connect whose teardown didn't fully complete; concurrent connect attempts on the same data client; runtime shutdown racing connection setup.","solutions":["Read the wrapped `{e}` from start_generation for the root cause.","Await full teardown (`teardown_partial_connect`) / prior disconnect before reconnecting.","Avoid concurrent connect() calls on the same data client; serialize lifecycle calls.","Recreate the data client if its task groups were permanently shut down.","Check that the tokio runtime isn't being dropped mid-connect."],"exampleFix":"// before\ndata_client.connect().await?; // after a failed earlier connect\n// after\ndata_client.disconnect().await?; // ensure clean state\ndata_client.connect().await?;","handlingStrategy":"retry","validationCode":"// Only connect when previous lifecycle ended:\nif !data_client.is_connected() && prior_connect_finished { data_client.connect().await?; }","typeGuard":null,"tryCatchPattern":"match data_client.connect().await {\n    Err(e) if e.to_string().contains(\"session generation\") => {\n        tokio::time::sleep(BACKOFF).await;\n        data_client.connect().await.context(\"reconnect failed\")?;\n    }\n    other => other?,\n}","preventionTips":["Ensure disconnect/teardown completes before reconnecting.","Use a single supervisor task to own connect/disconnect.","Add retry with exponential backoff.","Watch for runtime shutdown racing connection setup."],"tags":["websocket","task-generation","derive","connection-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-14T00:17:10.932Z"}