nautechsystems/nautilus_trader · error · anyhow::Error

Failed to start Binance Futures task generation: {e}

Error message

Failed to start Binance Futures task generation: {e}

What it means

Raised in BinanceFuturesExecutionClient::connect when the pending-tasks tokio TaskGroup fails to start a new generation. The client uses task groups to track background tasks; if awaiting or starting the pending-task generation fails (e.g. the group was already shut down or a task panicked during spawn/generation start), connect aborts with this wrapped error. It indicates the client could not set up its internal task infrastructure.

Source

Thrown at crates/adapters/binance/src/futures/execution.rs:1692

    fn get_account(&self) -> Option<AccountAny> {
        self.core.cache().account_owned(&self.core.account_id)
    }

    async fn connect(&mut self) -> anyhow::Result<()> {
        if self.core.is_connected() && self.session_tasks.is_open() && self.pending_tasks.is_open()
        {
            return Ok(());
        }

        if !self.pending_tasks.is_open() || !self.session_tasks.is_open() {
            self.disconnect().await?;
        }

        if !self.pending_tasks.is_open() {
            self.await_pending_tasks().await?;
            self.pending_tasks.start_generation().map_err(|e| {
                anyhow::anyhow!("Failed to start Binance Futures task generation: {e}")
            })?;
        }

        if !self.session_tasks.is_open() {
            self.await_session_tasks().await?;
            self.await_dispatch_task().await?;
            self.session_tasks.start_generation().map_err(|e| {
                anyhow::anyhow!("Failed to start Binance Futures session generation: {e}")
            })?;
        }

        self.cancellation_token = CancellationToken::new();
        let cancellation_token = self.cancellation_token.clone();
        let ws_client = Arc::clone(&self.ws_client);
        let ws_trading_client = self.ws_trading_client.clone();
        let setup_guard =
            TaskGroupGuard::new(&[&self.session_tasks, &self.pending_tasks], move || {
                cancellation_token.cancel();

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Call disconnect() (or recreate the client) before reconnecting so task groups are fully torn down and can start a fresh generation.
  2. Check logs above the error for a panicking background task and fix the underlying panic.
  3. Ensure the tokio runtime is alive and not being shut down while connect() is awaited.
  4. If reproducible, capture the inner error `{e}` for the concrete cause and report/fix that root task failure.

Example fix

// before
client.connect().await?; // reconnect after fault

// after
client.disconnect().await?;
client.connect().await?;
Defensive patterns

Strategy: try-catch

Try / catch

match client.connect().await {
    Err(e) if e.to_string().contains("task generation") => {
        client.disconnect().await?;
        client.connect().await?;
    }
    r => r?,
}

Prevention

When it happens

Trigger: Calling connect() when pending_tasks group is not open and start_generation() returns an error — typically after a previous disconnect shut the group down improperly, a task in the group panicked, or the runtime is shutting down while connect is in flight.

Common situations: Reconnecting a live-node client after a fault without a clean disconnect, stopping the node while connect() is still running, or a panic inside a previously spawned pending task poisoning the group generation.

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