nautechsystems/nautilus_trader · error

Failed to terminate Deribit execution tasks: {e}

Error message

Failed to terminate Deribit execution tasks: {e}

What it means

During execution-client shutdown, `await_pending_tasks` asks the task manager to finish all pending trading tasks within tight deadlines (1s collect / 2s terminate). If tasks don't stop in time, `finish_shutdown` errors and is wrapped with this message at execution.rs:200. It is surfaced through `connect()` (which awaits pending tasks when the task generation isn't open), indicating stale tasks from a prior session blocked a clean connect.

Source

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

        }
    }

    /// Aborts all pending async tasks.
    fn abort_pending_tasks(&self) {
        self.pending_tasks.begin_shutdown();
    }

    fn abort_session_tasks(&self) {
        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 {

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Retry the connect after a short delay so stalled tasks get a chance to be aborted/reaped.
  2. Investigate why a pending task hung (typically an unanswered WebSocket request) and add upstream timeouts.
  3. Call the abort path (e.g. `abort_pending_tasks`) before reconnecting to force-cancel stragglers.
  4. If deadlines are too tight for your workload, run reconnect logic in a fresh execution-client instance instead of reusing one.

Example fix

// before
match client.connect().await {
    Err(e) => return Err(e),
    Ok(()) => {}
}

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

Strategy: retry

Validate before calling

// No direct pre-check exists; ensure no connect() is already in flight:
assert!(!connect_in_progress.load(Ordering::SeqCst), "connect already running");

Try / catch

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

Prevention

When it happens

Trigger: Reconnecting while previous pending tasks (order/trade handlers, request futures) are still running and refuse to finish within the 1s+2s shutdown windows; shutdown called while a pending task is blocked on a hung WebSocket request.

Common situations: Rapid disconnect/reconnect cycles on flaky networks; a Deribit API call stuck without a response; process reusing the execution client after a previous session's tasks stalled; overloaded runtime delaying task completion past the deadlines.

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/9600396c17ff41f2. Report an issue: GitHub.