{"record":{"id":"c7474e038bb1aa96","repo":"nautechsystems/nautilus_trader","slug":"failed-to-terminate-ax-execution-session-tasks-e","errorCode":null,"errorMessage":"Failed to terminate AX execution session tasks: {e}","messagePattern":"Failed to terminate AX execution session tasks: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/architect_ax/src/execution.rs","lineNumber":425,"sourceCode":"        self.session_tasks.begin_shutdown();\n        self.ws_orders.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 AX 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| anyhow::anyhow!(\"Failed to terminate AX execution session tasks: {e}\"))?;\n        Ok(())\n    }\n\n    async fn teardown_partial_connect(&mut self) -> anyhow::Result<()> {\n        self.abort_session_tasks();\n        self.abort_pending_tasks();\n        self.http_client.cancel_all_requests();\n\n        if let Err(e) = self.ws_orders.close().await {\n            self.shutdown_errors\n                .push(format!(\"AX orders WebSocket shutdown failed: {e}\"));\n        }\n\n        let (session_result, pending_result) =\n            tokio::join!(self.await_session_tasks(), self.await_pending_tasks());\n        self.core.set_disconnected();\n\n        if let Err(e) = session_result {","sourceCodeStart":407,"sourceCodeEnd":443,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/architect_ax/src/execution.rs#L407-L443","documentation":"Raised in `await_session_tasks` when the execution client's session-task supervisor (`session_tasks`) fails to finish shutdown within its 1s grace and 2s hard deadline. Session tasks (e.g., session keep-alive/streams for the AX execution client) did not terminate cleanly during teardown.","triggerScenarios":"Calling `disconnect()`/`await_session_tasks` when session tasks remain blocked — typically a WebSocket session loop or stream reader stuck on a hung connection that ignores the shutdown signal until the hard deadline passes.","commonSituations":"WS session loop not observing the cancellation token; TCP connection half-open during teardown; slow AX server responses keeping session handlers alive; shutdown racing with an active reconnect loop.","solutions":["Inspect the wrapped inner error for which session tasks did not exit.","Ensure session task loops select on the shutdown/cancellation signal and exit promptly.","Add timeouts to session network reads so tasks cannot block indefinitely.","Increase the grace/timeout durations if sessions need more time to wind down.","Verify a fallback abort path (e.g., `abort_session_tasks`) is invoked after the failure to avoid leaked tasks."],"exampleFix":"// before\nloop {\n    let msg = ws.next().await;  // blocks shutdown\n    handle(msg);\n}\n// after\nloop {\n    tokio::select! {\n        msg = ws.next() => handle(msg),\n        _ = shutdown.cancelled() => break,\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match client.await_session_tasks().await {\n    Err(e) => {\n        log::warn!(\"session tasks shutdown timed out: {e:#}\");\n        client.abort_session_tasks(); // forced fallback\n    }\n    Ok(()) => {},\n}","preventionTips":["Structure session loops with tokio::select! over the shutdown signal.","Set read/write timeouts on session sockets so loops cannot block indefinitely.","Stop reconnect loops before initiating shutdown.","Always run a forced abort path after graceful session shutdown fails."],"tags":["shutdown","tasks","timeout","session"],"backgroundTag":"task-shutdown-timeout","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"}