nautechsystems/nautilus_trader · error

Failed to start Binance Spot session generation: {e}

Error message

Failed to start Binance Spot session generation: {e}

What it means

Wraps a failure from `session_tasks.start_generation()` while connecting the Binance Spot execution client. The client awaits any in-flight session tasks, then tries to begin a new WebSocket session generation; if the task-group manager refuses (e.g. group is in a stopping/failed state), the underlying error is rethrown with this Binance Spot prefix.

Source

Thrown at crates/adapters/binance/src/spot/execution.rs:870

        {
            return Ok(());
        }

        if !self.pending_tasks.is_open() || !self.session_tasks.is_open() {
            self.teardown_partial_connect().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 Spot task generation: {e}")
            })?;
        }

        if !self.session_tasks.is_open() {
            self.await_session_tasks().await?;
            self.session_tasks.start_generation().map_err(|e| {
                anyhow::anyhow!("Failed to start Binance Spot session generation: {e}")
            })?;
        }
        let ws_trading_client = self.ws_trading_client.clone();
        let ws_user_data_client = Arc::clone(&self.ws_user_data_client);
        let setup_guard =
            TaskGroupGuard::new(&[&self.session_tasks, &self.pending_tasks], move || {
                if let Some(client) = ws_trading_client {
                    client.begin_shutdown();
                }

                if let Some(client) = ws_user_data_client.lock().as_ref() {
                    client.begin_shutdown();
                }
            });

        let ws_setup_timeout = Duration::from_millis(self.config.ws_trading_setup_timeout_ms);

        // Load instruments if not already done

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Ensure `disconnect()`/`await_session_tasks()` fully completes before reconnecting.
  2. Avoid calling `connect()` twice on the same client instance; recreate the client instead.
  3. Inspect the inner error (`{e}` in the message) for the actual TaskGroup failure cause.
  4. Retry connect with backoff if the previous generation is still shutting down.

Example fix

// before
self.session_tasks.start_generation().map_err(|e| {
    anyhow::anyhow!("Failed to start Binance Spot session generation: {e}")
})?;
// after
if self.session_tasks.is_open() {
    self.session_tasks.stop_generation().await?;
}
self.await_session_tasks().await?;
self.session_tasks.start_generation().map_err(|e| {
    anyhow::anyhow!("Failed to start Binance Spot session generation: {e}")
})?;
Defensive patterns

Strategy: retry

Validate before calling

if !client.is_connected() && client.session_tasks_state() == TaskState::Stopped { /* safe to connect */ }

Try / catch

match client.connect().await {
    Err(e) if e.to_string().contains("session generation") => tokio::time::sleep(Duration::from_secs(2)).await, // retry after teardown
    other => other?,
}

Prevention

When it happens

Trigger: Calling `connect()` when the session task group has been stopped or previously errored, or when a previous generation's shutdown has not fully completed so `start_generation` rejects.

Common situations: Reconnecting after a network drop while old tasks are still tearing down; calling connect twice; a prior session error left the TaskGroup in a terminal state.

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