{"record":{"id":"6d5ecae37210d052","repo":"nautechsystems/nautilus_trader","slug":"failed-to-start-derive-session-generation-e","errorCode":null,"errorMessage":"Failed to start Derive session generation: {e}","messagePattern":"Failed to start Derive session generation: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/derive/src/execution.rs","lineNumber":605,"sourceCode":"    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        log::info!(\"Connecting Derive execution client\");\n\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.session_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Derive session generation: {e}\"))?;\n            self.pending_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Derive task generation: {e}\"))?;\n            self.cancellation_token = CancellationToken::new();\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        self.ensure_instruments_initialized()\n            .await\n            .context(\"failed to initialize Derive instruments\")?;\n\n        self.ws_client","sourceCodeStart":587,"sourceCodeEnd":623,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/derive/src/execution.rs#L587-L623","documentation":"connect starts a new generation on both the session and pending TaskGroups before spawning work. If session_tasks.start_generation() fails (group already started or in an invalid state), connect tears down the partial connect and raises this message. It means the Derive execution client could not initialize its session task group for a new connection generation.","triggerScenarios":"Calling connect when session_tasks is already open or mid-shutdown; double-connect on the same client instance; connect invoked concurrently from two places.","commonSituations":"Manual reconnect logic calling connect twice; racing connect calls from different actors/tasks; calling connect on a client that was never fully torn down after a previous failure.","solutions":["Ensure connect is called at most once per client lifecycle; recreate the client instead of reconnecting manually.","Synchronize connect calls (single owner/task) to avoid concurrent start_generation on the same group.","The error path already runs teardown_partial_connect — check its output for the underlying state; retry after a clean teardown.","Verify the wrapped TaskGroup error to confirm whether the group was already started."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Only connect a fresh or cleanly torn-down client:\nif client.is_connected() { return Ok(()); }","typeGuard":null,"tryCatchPattern":"if let Err(e) = exec_client.connect().await {\n    if e.to_string().contains(\"start Derive session generation\") {\n        // client half-initialized; teardown already ran — recreate client\n    }\n    return Err(e);\n}","preventionTips":["Avoid double-connect; guard with an is_connected check or connection state machine","Do not attempt manual reconnect on the same instance; instantiate a new client","Keep lifecycle transitions single-threaded/serialized"],"tags":["derive","connect","task-group","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-14T00:17:10.932Z"}