{"record":{"id":"3c1ee9307da26411","repo":"nautechsystems/nautilus_trader","slug":"failed-to-acquire-websocket-handler-task-spawner","errorCode":null,"errorMessage":"failed to acquire WebSocket handler task spawner: {e}","messagePattern":"failed to acquire WebSocket handler task spawner: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/deribit/src/websocket/client.rs","lineNumber":541,"sourceCode":"\n        log_debug!(\n            \"Connecting to WebSocket: {}\",\n            self.url,\n            color = LogColor::Blue\n        );\n\n        if !self.handler_tasks.is_open() || !self.handler_tasks.is_empty() {\n            self.handler_tasks.begin_shutdown();\n            self.signal.store(true, Ordering::Relaxed);\n            self.finish_handler()\n                .await\n                .map_err(|e| anyhow::anyhow!(\"failed to stop prior WebSocket handler: {e}\"))?;\n            self.handler_tasks.start_generation().map_err(|e| {\n                anyhow::anyhow!(\"failed to start WebSocket handler task generation: {e}\")\n            })?;\n        }\n        let handler_spawner = self.handler_tasks.spawner().map_err(|e| {\n            anyhow::anyhow!(\"failed to acquire WebSocket handler task spawner: {e}\")\n        })?;\n\n        // Reset stop signal and subscription state so callers can\n        // resubscribe cleanly after a manual disconnect/connect cycle.\n        self.signal.store(false, Ordering::Relaxed);\n        self.subscriptions_state.clear();\n\n        // Create message handler and channel\n        let (message_handler, raw_rx) = channel_message_handler();\n\n        // No-op ping handler: handler responds to pings directly\n        // Inbound Ping frames are answered by the transport, so no ping handler is needed;\n        // the reader routes them away from the message channel and the handler never sees them.\n\n        // Configure WebSocket client\n        let config = WebSocketConfig {\n            url: self.url.clone(),\n            headers: vec![(USER_AGENT.to_string(), NAUTILUS_USER_AGENT.to_string())],","sourceCodeStart":523,"sourceCodeEnd":559,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/deribit/src/websocket/client.rs#L523-L559","documentation":"`connect()` obtains a spawner from `handler_tasks` to launch the new WebSocket handler tasks. This error is thrown when the spawner cannot be acquired, meaning the task group is in a state (shut down, mid-generation) where spawning is not allowed.","triggerScenarios":"Any call to `connect()` where `handler_tasks.spawner()` returns Err — typically the group was shut down, closed, or is in an invalid generation state after prior start_generation calls.","commonSituations":"Reusing a client after an explicit shutdown/close; connect invoked while another connect is mid-setup; the client was dropped from a task set that has ended its lifecycle.","solutions":["Check the wrapped `{e}` to confirm the task-group state (closed vs wrong generation).","Create a fresh Deribit WebSocket client instead of reusing a shut-down one.","Avoid calling connect after shutdown/close of the client.","Serialize connect calls to prevent two setups from racing generation state.","If you need repeated reconnects, verify each prior disconnect completed cleanly first."],"exampleFix":"// before\nlet client = DeribitWsClient::new(...);\nclient.disconnect().await?;\nclient.connect().await?; // fails if internal tasks shut down\n// after\nlet client = DeribitWsClient::new(...); // fresh client per lifecycle\nclient.connect().await?;","handlingStrategy":"try-catch","validationCode":"// Only reuse a client within its valid lifecycle; do not connect after shutdown.\nif client_is_shutdown { client = DeribitWsClient::new(...)?; }","typeGuard":null,"tryCatchPattern":"match client.connect().await {\n    Err(e) if e.to_string().contains(\"handler task spawner\") => {\n        let mut client = DeribitWsClient::new(config).await?;\n        client.connect().await?;\n    }\n    other => other?,\n}","preventionTips":["Never call connect after shutdown/close.","Treat the client as single-lifecycle; rebuild for a fresh session.","Guard connect entry points against concurrent invocation.","Keep reconnect logic inside a dedicated supervisor task."],"tags":["websocket","task-spawn","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-14T05:17:10.506Z"}