{"record":{"id":"b630962c0bb8d580","repo":"nautechsystems/nautilus_trader","slug":"failed-to-register-websocket-handler-task-e","errorCode":null,"errorMessage":"failed to register WebSocket handler task: {e}","messagePattern":"failed to register WebSocket handler task: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/deribit/src/websocket/client.rs","lineNumber":849,"sourceCode":"                                log::error!(\"Authentication failed: {reason}\");\n                            }\n                        }\n                        _ => {}\n                    },\n                    None => {\n                        log::debug!(\"Handler returned None, stopping task\");\n                        break;\n                    }\n                }\n            }\n        };\n\n        if let Err(e) = handler_spawner.spawn(handler_task) {\n            if let Some(control) = &self.socket_control {\n                control.deregister();\n            }\n            self.out_rx = None;\n            anyhow::bail!(\"failed to register WebSocket handler task: {e}\");\n        }\n        log::debug!(\"Connected to WebSocket\");\n\n        Ok(())\n    }\n\n    /// Closes the WebSocket connection.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the close operation fails.\n    pub async fn close(&self) -> DeribitWsResult<()> {\n        self.begin_shutdown();\n        let connect_lock = Arc::clone(&self.connect_lock);\n        let _connect_guard = connect_lock.lock().await;\n        self.close_locked().await\n    }\n","sourceCodeStart":831,"sourceCodeEnd":867,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/deribit/src/websocket/client.rs#L831-L867","documentation":"During WebSocket connect, the message-handler task must be spawned and registered with the connection's handler spawner; if spawning fails, connect deregisters socket control, tears down the output receiver, and bails with 'failed to register WebSocket handler task: {e}'. Without the handler, incoming frames would never be dispatched, so connect refuses to report success.","triggerScenarios":"Calling connect when the handler spawner rejects the task spawn — typically runtime shutdown in progress, spawner capacity exhausted, or a closed/crashed runtime handle.","commonSituations":"Application shutting down while reconnect logic still fires; too many spawned tasks saturating the executor; misconfigured spawner with a zero/small task limit.","solutions":["Check the inner {e} from the spawner for the root cause (runtime closed vs capacity)","Ensure the tokio runtime is alive and not shutting down when connect is called","Reduce concurrent spawned tasks or raise the spawner's capacity","Retry connect after confirming the runtime is healthy — connect cleans up state on this failure path"],"exampleFix":"// before\nclient.connect().await?; // may bail if runtime is shutting down\n// after\nif !runtime_handle.is_running() {\n    log::warn!(\"skipping ws connect: runtime shutting down\");\n    return Ok(());\n}\nclient.connect().await?;","handlingStrategy":"try-catch","validationCode":"// Confirm the runtime is alive before connecting\nif handle.shutdown_started() {\n    log::warn!(\"runtime shutting down; skip ws connect\");\n    return Ok(());\n}","typeGuard":null,"tryCatchPattern":"match client.connect().await {\n    Err(e) if e.to_string().contains(\"failed to register WebSocket handler task\") => {\n        log::error!(\"handler spawn failed: {e}\");\n        // check runtime health, then retry with backoff\n        retry_with_backoff(3, || client.connect()).await\n    }\n    other => other,\n}","preventionTips":["Ensure connect is not called during runtime shutdown; gate reconnect loops on runtime health","Bound total spawned tasks so the spawner never exhausts capacity","Verify spawner configuration (task limits) in tests","Clean up stale connections before reconnecting to reduce task churn"],"tags":["websocket","task-spawn","runtime","deribit"],"backgroundTag":"internal-invariant-violation","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"}