{"record":{"id":"ea92fbb10f4c4d38","repo":"nautechsystems/nautilus_trader","slug":"failed-to-terminate-polymarket-session-tasks-e","errorCode":null,"errorMessage":"Failed to terminate Polymarket session tasks: {e}","messagePattern":"Failed to terminate Polymarket session tasks: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/polymarket/src/execution/lifecycle.rs","lineNumber":226,"sourceCode":"                    && !self.fill_tracker.is_fully_filled(&venue_order_id)\n                {\n                    self.emitter\n                        .emit_order_canceled(&order, Some(venue_order_id), cancel_ts);\n                }\n            }\n        }\n\n        result\n            .map_err(|e| anyhow::anyhow!(\"Failed to terminate Polymarket execution tasks: {e}\"))?;\n        Ok(())\n    }\n\n    pub(super) async fn await_session_tasks(&self) -> anyhow::Result<()> {\n        self.session_tasks.begin_shutdown();\n        self.session_tasks\n            .finish_shutdown(TASK_SESSION_GRACEFUL_SHUTDOWN_TIMEOUT, TASK_ABORT_TIMEOUT)\n            .await\n            .map_err(|e| anyhow::anyhow!(\"Failed to terminate Polymarket session tasks: {e}\"))?;\n        Ok(())\n    }\n\n    pub(super) async fn refresh_account_state(&self) -> anyhow::Result<()> {\n        fetch_and_emit_account_state(\n            &self.http_client,\n            &self.emitter,\n            self.clock,\n            self.config.signature_type,\n        )\n        .await\n    }\n\n    pub(super) async fn await_account_registered(&self, timeout_secs: f64) -> anyhow::Result<()> {\n        let account_id = self.core.account_id;\n\n        if self.core.cache().account(&account_id).is_some() {\n            log::info!(\"Account {account_id} registered\");","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/polymarket/src/execution/lifecycle.rs#L208-L244","documentation":"This error wraps a failure from `await_session_tasks`, which gracefully shuts down the Polymarket session task group (WebSocket dispatch, account-state polling) via `begin_shutdown`/`finish_shutdown` with dedicated graceful and abort timeouts. It surfaces when session tasks neither finish nor abort within those windows, or when the task group itself reports an error during shutdown.","triggerScenarios":"Calling await_session_tasks (from teardown_partial_connect or connect_client) while the WebSocket dispatch task or session spawner tasks hang past TASK_SESSION_GRACEFUL_SHUTDOWN_TIMEOUT, or the task group returns an error from finish_shutdown.","commonSituations":"WS message stream stalled without disconnecting; a session task blocked reading from a closed channel; slow machine or blocked runtime worker preventing task polling during shutdown; reconnect loop invoking connect_client while old session tasks are stuck.","solutions":["Read the wrapped `{e}` error chain to identify which session task failed","Ensure the WebSocket client is disconnected before awaiting session shutdown so dispatch tasks observe closure","Increase TASK_SESSION_GRACEFUL_SHUTDOWN_TIMEOUT if tasks legitimately need longer to wind down","Verify no session task loops on blocking (std::thread::sleep / blocking I/O) that starves the async runtime","Retry connection after abort timeout has force-terminated stuck tasks"],"exampleFix":"// before: session tasks never see WS close\nself.await_session_tasks().await?;\n// after: disconnect WS first so dispatch loop exits\nself.ws_client.disconnect().await;\nself.await_session_tasks().await?;","handlingStrategy":"try-catch","validationCode":"// ensure WS is disconnected before awaiting session shutdown\nif adapter.is_ws_connected() {\n    adapter.disconnect_ws().await;\n}","typeGuard":null,"tryCatchPattern":"if let Err(e) = adapter.await_session_tasks().await {\n    tracing::error!(\"session shutdown failed: {e:#}\");\n}","preventionTips":["Disconnect the WebSocket before session shutdown so dispatch tasks exit","Avoid blocking calls inside session tasks","Size TASK_SESSION_GRACEFUL_SHUTDOWN_TIMEOUT for worst-case task latency"],"tags":["rust","async","shutdown","task-timeout"],"backgroundTag":"request-timeout","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"}