nautechsystems/nautilus_trader · error · anyhow::Error

Failed to start Polymarket task generation: {e}

Error message

Failed to start Polymarket task generation: {e}

What it means

connect_client calls tasks.start_generation() when the task group is not open. If that call fails (task group cannot begin a new admission generation, e.g. it is in a bad shutdown state or the underlying runtime facility is unavailable), this error propagates out of connect().

Source

Thrown at crates/adapters/polymarket/src/data/lifecycle.rs:510

    }

    pub(super) async fn connect_client(&mut self) -> anyhow::Result<()> {
        if self.is_connected() && self.tasks.is_open() && !self.reset_pending {
            return Ok(());
        }

        if self.reset_pending
            || !self.tasks.is_empty()
            || !self.tasks.is_open()
            || self.ws_client.connection_count() > 0
        {
            self.disconnect_client().await?;
        }

        if !self.tasks.is_open() {
            self.tasks
                .start_generation()
                .map_err(|e| anyhow::anyhow!("Failed to start Polymarket task generation: {e}"))?;
            self.cancellation_token = self.tasks.cancellation_token();
        }

        let ws_client = self.ws_client.handle();
        let rtds_feed = self.rtds_feed.clone();
        let setup_guard = TaskGroupGuard::new(&[&self.tasks], move || {
            ws_client.begin_shutdown();
            rtds_feed.begin_shutdown();
        });
        self.ensure_position_event_subscription();
        register_polymarket_custom_data();

        log::info!("Connecting Polymarket data client");

        log::debug!("Bootstrapping instruments from Gamma API...");
        self.bootstrap_instruments().await?;
        log::debug!(
            "Bootstrap complete, {} instruments loaded",

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Wait for the previous disconnect to fully complete (including task termination) before reconnecting
  2. Construct a fresh client instance if the previous generation failed to close cleanly
  3. Increase disconnect timeout to avoid leaving the group half-shut-down
  4. Ensure the tokio runtime is healthy and not being torn down
Defensive patterns

Strategy: retry

Try / catch

let client = match client.connect().await {
    Err(e) if e.to_string().contains("task generation") => {
        warn!("generation start failed; rebuilding client");
        let mut c = build_client(cfg)?;
        c.connect().await?;
        c
    }
    _ => client,
};

Prevention

When it happens

Trigger: Calling connect()/connect on a client whose task group fails start_generation() — group wedged mid-shutdown, or spawner infrastructure reporting closure.

Common situations: Reconnecting a client immediately after a failed/aborted disconnect; disconnect that timed out (see 'Failed to terminate Polymarket data tasks') leaving the group in a partial shutdown state; process teardown in progress.

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