{"record":{"id":"445d7020fddc3fc9","repo":"nautechsystems/nautilus_trader","slug":"failed-to-start-lighter-websocket-handler-task-e","errorCode":null,"errorMessage":"Failed to start Lighter WebSocket handler task: {e}","messagePattern":"Failed to start Lighter WebSocket handler task: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/lighter/src/websocket/client.rs","lineNumber":627,"sourceCode":"                                log::error!(\"Failed to send Lighter message (receiver dropped)\");\n                            }\n                            break;\n                        }\n                    }\n                    None => {\n                        if handler.is_stopped() {\n                            log::debug!(\"Lighter handler stop signal observed, exiting loop\");\n                            break;\n                        }\n                        log::warn!(\"Lighter WebSocket stream ended unexpectedly\");\n                        break;\n                    }\n                }\n            }\n            log::debug!(\"Lighter handler task completed\");\n        }) {\n            self.out_rx = None;\n            anyhow::bail!(\"Failed to start Lighter WebSocket handler task: {e}\");\n        }\n        Ok(())\n    }\n\n    /// Disconnects gracefully: signals shutdown, drains the handler, then\n    /// awaits the task handle with a timeout.\n    ///\n    /// # Errors\n    ///\n    /// This function currently completes best-effort shutdown and returns `Ok(())`.\n    pub async fn disconnect(&mut self) -> Result<(), LighterWsError> {\n        self.connection_generation.fetch_add(1, Ordering::AcqRel);\n        self.initial_connect_cancellation.load().cancel();\n\n        let _guard = self.connection_lock.lock().await;\n        self.initial_connect_cancellation.load().cancel();\n\n        log::debug!(\"Disconnecting Lighter WebSocket\");","sourceCodeStart":609,"sourceCodeEnd":645,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/lighter/src/websocket/client.rs#L609-L645","documentation":"Raised when spawning the Lighter WebSocket handler tokio task fails. The code resets self.out_rx to None and bails with the JoinHandle error from tokio::spawn. On modern tokio this almost always means the runtime is shutting down or spawn was called outside a runtime context.","triggerScenarios":"Calling connect() outside a tokio runtime, or during runtime shutdown, so tokio::spawn for the handler task returns an error which is surfaced with this message.","commonSituations":"Calling client.connect() from a blocking thread or plain async fn without #[tokio::main]/runtime handle; stopping the actor/runtime while a data client is still initializing; embedding the adapter in a non-tokio executor.","solutions":["Ensure connect() is awaited within a live tokio runtime (use #[tokio::main] or an explicit Runtime/Handle).","If using a custom runtime, pass its Handle so tokio::spawn can find an executor.","Delay client startup until after the runtime is initialized; don't spawn clients during shutdown.","Check that node start ordering doesn't stop the runtime before WebSocket clients connect.","Wrap startup in a runtime::Handle::current().spawn if bridging from another executor."],"exampleFix":"// before: calling connect outside a runtime\nstd::thread::spawn(|| client.connect().await?); // spawn fails\n\n// after: run inside the tokio runtime\nlet handle = tokio::runtime::Handle::current();\nhandle.spawn(async move { client.connect().await })?;","handlingStrategy":"try-catch","validationCode":"// ensure a runtime exists before connecting\nlet _guard = tokio::runtime::Handle::try_current()?;","typeGuard":null,"tryCatchPattern":"match client.connect().await {\n    Err(e) if e.to_string().contains(\"Failed to start\") => {\n        log::error!(\"runtime unavailable for handler spawn: {e:#}\");\n        // re-run startup inside an active runtime handle\n    }\n    r => r?,\n}","preventionTips":["Always call connect() within a live tokio runtime","Don't initialize clients during runtime shutdown","Pass an explicit Handle when bridging from other executors","Verify node start ordering: runtime first, then clients"],"tags":["async","tokio","runtime","rust"],"backgroundTag":"missing-env-var","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}