{"record":{"id":"d20a2a8863cb587a","repo":"nautechsystems/nautilus_trader","slug":"failed-to-start-deribit-session-generation-e","errorCode":null,"errorMessage":"Failed to start Deribit session generation: {e}","messagePattern":"Failed to start Deribit session generation: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/deribit/src/execution.rs","lineNumber":493,"sourceCode":"            self.await_pending_tasks().await?;\n            self.pending_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Deribit task generation: {e}\"))?;\n        }\n\n        if !self.session_tasks.is_open() || !self.session_tasks.is_empty() {\n            self.abort_session_tasks();\n\n            if self.ws_client.is_active() {\n                self.ws_client\n                    .close()\n                    .await\n                    .context(\"failed to close stale Deribit WebSocket\")?;\n            }\n            self.await_session_tasks().await?;\n            self.session_tasks\n                .start_generation()\n                .map_err(|e| anyhow::anyhow!(\"Failed to start Deribit session generation: {e}\"))?;\n        } else if self.ws_client.is_active() {\n            self.ws_client\n                .close()\n                .await\n                .context(\"failed to close stale Deribit WebSocket\")?;\n        }\n        let ws_client = self.ws_client.clone();\n        let setup_guard =\n            TaskGroupGuard::new(&[&self.session_tasks, &self.pending_tasks], move || {\n                ws_client.begin_shutdown();\n            });\n\n        // Check if credentials are available before requesting account state\n        if !self.config.has_api_credentials() {\n            anyhow::bail!(\"Missing API credentials; set Deribit environment variables\");\n        }\n\n        // Set account ID for order/fill reports","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/deribit/src/execution.rs#L475-L511","documentation":"When connecting, session tasks must be started via `start_generation` after any stale session is awaited/aborted. If that call fails (manager not in a startable state — e.g. still shutting down or already open), execution.rs:493 returns this error and the connect aborts before authentication.","triggerScenarios":"Calling `connect()` while session-task generation state is inconsistent: concurrent connects, or a prior shutdown that never fully completed leaving the manager non-startable.","commonSituations":"Simultaneous connect attempts from different parts of an application; reconnect immediately after a shutdown-timeout error without aborting session tasks; state machine misuse after partial connect teardown (`teardown_partial_connect`) followed by an immediate reconnect race.","solutions":["Serialize `connect()` calls (mutex or single owner task) to avoid racing session-task state.","On failure, call `abort_session_tasks()` then retry connect after a short backoff.","Resolve any prior 'Failed to terminate Deribit session tasks' error before reconnecting.","Verify teardown logic always leaves the session-task manager in a startable (closed/empty) state."],"exampleFix":"// before\nif let Err(e) = client.connect().await {\n    panic!(\"connect failed: {e}\");\n}\n\n// after\nif let Err(e) = client.connect().await {\n    if e.to_string().contains(\"Failed to start Deribit session generation\") {\n        client.abort_session_tasks();\n        tokio::time::sleep(Duration::from_millis(200)).await;\n    }\n    client.connect().await?;\n}","handlingStrategy":"retry","validationCode":"// Serialize connects and ensure prior teardown finished\nlet _guard = connect_mutex.lock().await;\nif connect_in_flight.swap(true, Ordering::SeqCst) {\n    return Err(anyhow!(\"connect already in progress\"));\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = client.connect().await {\n    if e.to_string().contains(\"Failed to start Deribit session generation\") {\n        client.abort_session_tasks();\n        tokio::time::sleep(Duration::from_millis(500)).await;\n        client.connect().await?;\n    } else {\n        return Err(e);\n    }\n}","preventionTips":["Never race multiple connect() calls on the same execution client.","Handle partial-connect teardown fully before the next connect attempt.","Abort session tasks after any 'terminate session tasks' timeout before reconnecting.","Document and enforce the connect/disconnect state machine in your application."],"tags":["connect","task-generation","lifecycle","deribit"],"backgroundTag":"module-init-failed","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"}