{"record":{"id":"0027052151aad689","repo":"nautechsystems/nautilus_trader","slug":"failed-to-start-deribit-task-generation-e","errorCode":null,"errorMessage":"Failed to start Deribit task generation: {e}","messagePattern":"Failed to start Deribit task generation: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/deribit/src/execution.rs","lineNumber":478,"sourceCode":"        self.core.set_stopped();\n        self.core.set_disconnected();\n        self.abort_session_tasks();\n        self.abort_pending_tasks();\n        log::info!(\"Stopped: client_id={}\", self.core.client_id);\n        Ok(())\n    }\n\n    async fn connect(&mut self) -> anyhow::Result<()> {\n        if self.core.is_connected() && self.pending_tasks.is_open() && self.session_tasks.is_open()\n        {\n            return Ok(());\n        }\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 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()","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/deribit/src/execution.rs#L460-L496","documentation":"`connect` must open a new pending-task generation before running order/trade tasks. If `start_generation` on the task manager fails (typically called on a manager not in a startable state, e.g. still shutting down or already open), the error at execution.rs:478 is returned. The connect attempt aborts before any WebSocket work for trading tasks begins.","triggerScenarios":"Calling `connect()` while the pending-task manager is mid-shutdown from a previous session; concurrent `connect()` calls racing on the same task manager; a previous `finish_shutdown` timeout left the manager in a bad state.","commonSituations":"Reconnect storms on unstable networks calling connect repeatedly; framework code invoking connect from multiple tasks without synchronization; recovery after the 'Failed to terminate Deribit execution tasks' error without aborting tasks first.","solutions":["Ensure only one `connect()` runs at a time; serialize connects behind a mutex/single owner.","After shutdown errors, call `abort_pending_tasks()` before retrying `connect()`.","Back off and retry the connect after a short delay so the task manager exits its shutdown state.","Check application code for reconnect loops without backoff that race the shutdown window."],"exampleFix":"// before\nlet (a, b) = tokio::join!(client.connect(), client.connect());\n\n// after\nstatic CONNECT: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());\nlet _guard = CONNECT.lock().await;\nclient.connect().await?;","handlingStrategy":"retry","validationCode":"// Guard against concurrent connects before calling the API\nlet _guard = connect_mutex.lock().await;\n// proceed with client.connect().await","typeGuard":null,"tryCatchPattern":"match client.connect().await {\n    Err(e) if e.to_string().contains(\"Failed to start Deribit task generation\") => {\n        client.abort_pending_tasks();\n        tokio::time::sleep(Duration::from_millis(500)).await;\n        client.connect().await?;\n    }\n    other => other.map(|_| ()),\n}","preventionTips":["Enforce single-flight connect via a mutex or a dedicated supervisor task.","After any shutdown-related error, abort tasks and reset state before retrying.","Use backoff on reconnect loops so the task manager can settle.","If reusing a client across sessions, verify is_open/is_empty states before connect."],"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"}