nautechsystems/nautilus_trader · error · anyhow::Error

Failed to terminate Bybit execution session tasks: {e}

Error message

Failed to terminate Bybit execution session tasks: {e}

What it means

Same shutdown guard as the pending tasks but for session tasks (WebSocket session workers). If finish_shutdown times out or errors while stopping session tasks within the 1s/2s windows, the client bails with this message.

Source

Thrown at crates/adapters/bybit/src/execution.rs:279

        self.session_tasks.begin_shutdown();
    }

    async fn await_pending_tasks(&self) -> anyhow::Result<()> {
        self.pending_tasks.begin_shutdown();
        self.pending_tasks
            .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2))
            .await
            .map_err(|e| anyhow::anyhow!("Failed to terminate Bybit execution tasks: {e}"))?;
        Ok(())
    }

    async fn await_session_tasks(&self) -> anyhow::Result<()> {
        self.session_tasks.begin_shutdown();
        self.session_tasks
            .finish_shutdown(Duration::from_secs(1), Duration::from_secs(2))
            .await
            .map_err(|e| {
                anyhow::anyhow!("Failed to terminate Bybit execution session tasks: {e}")
            })?;
        Ok(())
    }

    async fn teardown_partial_connect(&mut self) -> anyhow::Result<()> {
        self.abort_session_tasks();
        self.abort_pending_tasks();
        self.http_client.cancel_all_requests();
        self.ws_private.begin_shutdown();
        self.ws_trade.begin_shutdown();

        if let Err(e) = self.ws_private.close().await {
            self.shutdown_errors
                .push(format!("private WebSocket shutdown failed: {e}"));
        }

        if let Err(e) = self.ws_trade.close().await {
            self.shutdown_errors

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Retry teardown after a short delay
  2. Check for blocking or long-await loops in session task code
  3. Look at the wrapped inner error to distinguish timeout vs cancellation failure
  4. Ensure network connectivity or force client drop if tasks are stuck
Defensive patterns

Strategy: try-catch

Try / catch

if let Err(e) = client.disconnect().await {
    if e.to_string().contains("execution session tasks") {
        tracing::warn!("session tasks did not stop in time: {e}");
        // proceed with drop; tasks die with the runtime
    } else {
        return Err(e);
    }
}

Prevention

When it happens

Trigger: Tearing down BybitExecutionClient while private/trade WebSocket session tasks are still busy or hung.

Common situations: Unresponsive WebSocket connections, handlers awaiting network I/O during shutdown, abrupt network loss right before disconnect.

Understand the failure class

Background: Request timed out: what client-side request timeouts mean across libraries (Request timed out, TIMED_OUT, APITimeoutError) — this error's family across 39 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/44d5db0a04e978aa. Report an issue: GitHub.