{"record":{"id":"a07c6535966a203b","repo":"nautechsystems/nautilus_trader","slug":"failed-to-send-websocket-client-to-handler-e","errorCode":null,"errorMessage":"Failed to send WebSocket client to handler: {e}","messagePattern":"Failed to send WebSocket client to handler: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/okx/src/websocket/client.rs","lineNumber":866,"sourceCode":"\n        if let Err(e) = handler_spawner.spawn(handler_task) {\n            self.out_rx = None;\n            anyhow::bail!(\"Failed to register WebSocket handler task: {e}\");\n        }\n\n        let set_client_result = {\n            let cmd_tx = self.cmd_tx.read().await;\n            cmd_tx.send(HandlerCommand::SetClient(client))\n        };\n\n        if let Err(e) = set_client_result {\n            self.handler_tasks.begin_shutdown();\n            self.signal.store(true, Ordering::Release);\n            let handler_abort = self.handler_abort.lock().clone();\n            handler_abort.cancel();\n            let shutdown_result = self.close_stream_task(Duration::from_secs(2)).await;\n            self.out_rx = None;\n            anyhow::bail!(match shutdown_result {\n                Ok(()) => format!(\"Failed to send WebSocket client to handler: {e}\"),\n                Err(shutdown_error) => format!(\n                    \"Failed to send WebSocket client to handler: {e}; handler shutdown failed: \\\n                     {shutdown_error}\"\n                ),\n            });\n        }\n\n        if let Some(control) = &self.socket_control {\n            control.register(move || reconnect_handle.request_reconnect());\n        }\n        log::debug!(\"Sent WebSocket client to handler\");\n\n        if self.credential.is_some()\n            && let Err(e) = self.authenticate().await\n        {\n            self.handler_tasks.begin_shutdown();\n            self.request_close().await;","sourceCodeStart":848,"sourceCodeEnd":884,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/okx/src/websocket/client.rs#L848-L884","documentation":"After spawning the handler task, `connect` must register the newly created WebSocket client with it by sending `HandlerCommand::SetClient` over the command channel. If that send fails, the client initiates an emergency handler shutdown; this error reports the send failure, and if the shutdown itself also failed, the message is extended with the shutdown error.","triggerScenarios":"`cmd_tx.send(HandlerCommand::SetClient(client))` fails during `connect()` — typically because the handler task's command receiver was dropped (handler died immediately after spawn).","commonSituations":"Handler task panicked or aborted right after spawn; runtime shutdown racing the connection; preceding spawn errors leaving the channel broken.","solutions":["Check for an earlier handler-task panic (look for panic logs just before this error).","Retry `connect()` after ensuring `disconnect()` completed cleanly.","Examine the appended `handler shutdown failed` text if present — resolve that shutdown error as well before reconnecting."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"// this error means the handler died mid-connect; recreate and retry with backoff\nmatch client.connect().await {\n    Err(e) if e.to_string().contains(\"Failed to send WebSocket client to handler\") => {\n        tokio::time::sleep(Duration::from_secs(1)).await;\n        client = build_new_client()?; // internal channel state was rolled back\n        client.connect().await?;\n    }\n    other => other?,\n}","preventionTips":["Investigate any handler-task panics logged just before this error.","Serialize connect/disconnect calls so teardown never races connect.","Recreate the client after this error; the command channel was dropped."],"tags":["okx","websocket","channel","connect"],"backgroundTag":"broken-pipe","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"}