nautechsystems/nautilus_trader · error

Failed to terminate Deribit session tasks: {e}

Error message

Failed to terminate Deribit session tasks: {e}

What it means

`await_session_tasks` finishes all session-scoped tasks (message-processing loops) within 1s/2s deadlines during shutdown. If they can't be terminated in time, the error at execution.rs:209 is raised, propagated out of `connect()` when it needs a clean slate for a new session. It means lingering session tasks blocked a fresh session start.

Source

Thrown at crates/adapters/deribit/src/execution.rs:209

        self.session_tasks.begin_shutdown();
        self.ws_client.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 Deribit 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 Deribit session tasks: {e}"))?;
        Ok(())
    }

    async fn teardown_partial_connect(&self) -> anyhow::Result<()> {
        self.abort_session_tasks();
        self.abort_pending_tasks();

        let mut errors = Vec::new();
        if let Err(e) = self.ws_client.close().await {
            errors.push(format!("WebSocket shutdown failed: {e}"));
        }
        let (session_result, pending_result) =
            tokio::join!(self.await_session_tasks(), self.await_pending_tasks());

        if let Err(e) = session_result {
            errors.push(e.to_string());
        }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Force-abort session tasks (`abort_session_tasks`) before reconnecting, then retry connect.
  2. Retry connect after a brief backoff to let wedged tasks unwind.
  3. Check for a stalled WebSocket or missing read-timeout causing session loops to hang.
  4. If the runtime is overloaded, reduce concurrency or use a fresh execution client per session.

Example fix

// before
client.connect().await?;

// after
if let Err(e) = client.connect().await {
    if e.to_string().contains("Failed to terminate Deribit session tasks") {
        client.abort_session_tasks();
        tokio::time::sleep(Duration::from_millis(200)).await;
    }
    client.connect().await?;
}
Defensive patterns

Strategy: retry

Validate before calling

// Pre-check: don't reconnect while a previous shutdown is still settling
assert!(last_shutdown_complete.load(Ordering::SeqCst), "previous shutdown still in progress");

Try / catch

if let Err(e) = client.connect().await {
    if e.to_string().contains("Failed to terminate Deribit session tasks") {
        client.abort_session_tasks();
        tokio::time::sleep(Duration::from_millis(200)).await;
        client.connect().await?;
    } else {
        return Err(e);
    }
}

Prevention

When it happens

Trigger: Reconnecting the execution client while prior session task loops are still alive and exceed the shutdown deadlines; session tasks blocked awaiting WebSocket messages on a stalled connection.

Common situations: Reconnect after network drop where the old read loop is wedged; many short-lived reconnects stacking session tasks; async runtime starvation preventing tasks from observing the shutdown signal within 2 seconds.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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