{"record":{"id":"c0bc7a0c06cf7cec","repo":"nautechsystems/nautilus_trader","slug":"websocket-client-not-initialized","errorCode":null,"errorMessage":"WebSocket client not initialized","messagePattern":"WebSocket client not initialized","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/deribit/src/data.rs","lineNumber":200,"sourceCode":"            is_connected: AtomicBool::new(false),\n            cancellation_token: session_tasks.cancellation_token(),\n            session_tasks,\n            command_tasks,\n            data_sender,\n            instruments: Arc::new(AtomicMap::new()),\n            mark_price_subs: Arc::new(AtomicSet::new()),\n            index_price_subs: Arc::new(AtomicSet::new()),\n            option_greeks_subs: Arc::new(AtomicSet::new()),\n            combo_leg_trade_subs: Arc::new(AtomicMap::new()),\n            clock,\n        })\n    }\n\n    /// Returns a mutable reference to the WebSocket client.\n    fn ws_client_mut(&mut self) -> anyhow::Result<&mut DeribitWebSocketClient> {\n        self.ws_client\n            .as_mut()\n            .ok_or_else(|| anyhow::anyhow!(\"WebSocket client not initialized\"))\n    }\n\n    fn spawn_command<F>(&self, future: F)\n    where\n        F: std::future::Future<Output = ()> + Send + 'static,\n    {\n        if let Err(e) = self.command_tasks.spawn(future) {\n            log::warn!(\"Skipping Deribit data command after shutdown began: {e}\");\n        }\n    }\n\n    async fn finish_tasks(&self) -> anyhow::Result<()> {\n        let (session_result, command_result) = tokio::join!(\n            self.session_tasks\n                .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),\n            self.command_tasks\n                .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2)),\n        );","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/deribit/src/data.rs#L182-L218","documentation":"DeribitDataApp's WebSocket client is created during connect(); before that, ws_client is None. ws_client_mut unwraps it and throws this error when any code path (e.g. connect's handshake/subscribe steps) tries to mutate or use the client before initialization. It signals a lifecycle-ordering bug rather than a network failure.","triggerScenarios":"Calling methods that require the WS client (connect internals, command dispatch, subscription calls) before initialize/connection — e.g. double connect racing, calling a subscribe/quotes path without prior connect, or a callback firing before connection completes.","commonSituations":"Subscribing immediately after constructing the data app without awaiting connection; reconnect logic racing with initial connect; tests instantiating DeribitDataApp without a WS client and invoking ws_client_mut paths.","solutions":["Ensure connect() completes (await it) before any other data-app methods that use the WebSocket client.","Guard call order in your integration: only subscribe/request after the connection-ready callback/state.","If reconnecting, verify the reconnect path re-initializes ws_client before dispatching commands.","For tests, initialize the WS client (or the app) exactly as connect() does before exercising these paths."],"exampleFix":"// before\nlet app = DeribitDataApp::new(config);\napp.subscribe_quotes(instrument_id).await?; // ws not initialized\n\n// after\nlet app = DeribitDataApp::new(config);\napp.connect().await?; // initializes ws_client\napp.subscribe_quotes(instrument_id).await?;","handlingStrategy":"validation","validationCode":"// Rust\nanyhow::ensure!(\n    app.is_connected(),\n    \"call connect() before any WebSocket-dependent operation\"\n);","typeGuard":null,"tryCatchPattern":"match app.connect().await {\n    Ok(()) => app.subscribe_quotes(id).await?,\n    Err(e) if e.to_string().contains(\"not initialized\") => {\n        return Err(anyhow!(\"data app not connected; connect() must run first: {e}\"));\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Await connect() before any subscribe/request calls","Serialize connect/reconnect so it cannot race with subscriptions","Gate subscriptions on a connected-state flag or callback"],"tags":["deribit","rust","websocket","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-14T00:17:10.932Z"}