{"record":{"id":"093aa9723d161490","repo":"nautechsystems/nautilus_trader","slug":"failed-to-terminate-derive-execution-session-tasks","errorCode":null,"errorMessage":"Failed to terminate Derive execution session tasks: {e}","messagePattern":"Failed to terminate Derive execution session tasks: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/derive/src/execution.rs","lineNumber":342,"sourceCode":"        self.ws_client.begin_shutdown();\n    }\n\n    async fn await_pending_tasks(&self) -> anyhow::Result<()> {\n        self.pending_tasks.begin_shutdown();\n        self.pending_tasks\n            .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2))\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to terminate Derive execution tasks: {e}\"))?;\n        Ok(())\n    }\n\n    async fn await_session_tasks(&self) -> anyhow::Result<()> {\n        self.session_tasks.begin_shutdown();\n        self.session_tasks\n            .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2))\n            .await\n            .map_err(|e| {\n                anyhow::anyhow!(\"Failed to terminate Derive execution session tasks: {e}\")\n            })?;\n        Ok(())\n    }\n\n    async fn ensure_instruments_initialized(&self) -> anyhow::Result<()> {\n        if self.core.instruments_initialized() {\n            return Ok(());\n        }\n        // Lazy bootstrap: exec-side fetches per-instrument on first reference.\n        // Marking the flag prevents duplicate work across reconnect cycles.\n        self.core.set_instruments_initialized();\n        Ok(())\n    }\n\n    fn reconciliation_context(&self) -> DeriveReconciliationContext {\n        DeriveReconciliationContext {\n            http_client: self.http_client.clone(),\n            emitter: self.emitter.clone(),","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/derive/src/execution.rs#L324-L360","documentation":"Analogous to await_pending_tasks, await_session_tasks tries to drain the session-task group (WebSocket dispatch/session lifecycle tasks) with 1s/2s grace timeouts. If those session tasks cannot finish in time or the group reports an error, it is wrapped as this message. It means the Derive execution client's session tasks failed to terminate cleanly at shutdown.","triggerScenarios":"Disconnecting the Derive execution client while the WebSocket dispatch loop/session tasks are still active and non-responsive past the grace timeouts; finish_shutdown returning an error for the session_tasks group.","commonSituations":"WS connection in a bad state (stuck read) during teardown; process shutdown while a reconnect loop is mid-flight; blocking code inside the session task loop preventing timely cancellation.","solutions":["Ensure the WS connection is closed/healthy before shutdown so the dispatch loop exits promptly.","Retry shutdown; if persistent, inspect why the session task doesn't honor the cancellation token.","Fix network stalls or make the WS read use tokio::select! against the cancellation token.","Read the wrapped inner error for the TaskGroup's specific failure reason."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Ensure the WS connection is closed before shutdown:\nif client.is_ws_connected() { client.disconnect_ws().await?; }","typeGuard":null,"tryCatchPattern":"if let Err(e) = client.await_session_tasks().await {\n    log::error!(\"Derive session tasks failed to terminate: {e}\");\n    // proceed with process exit or force-abort the task group\n}","preventionTips":["Close the WebSocket before draining session tasks","Avoid blocking (non-async) calls inside the session dispatch loop","Always await shutdown helpers during a controlled teardown rather than dropping the client"],"tags":["derive","shutdown","task-group","websocket","timeout"],"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"}