nautechsystems/nautilus_trader · error

Failed to register Betfair reconnect task: {e}

Error message

Failed to register Betfair reconnect task: {e}

What it means

At the end of connect, the Betfair data client spawns a reconnect task that re-establishes the streaming connection on drop. If registering this reconnect task fails, connect wraps the inner error with this message and returns Err, aborting the connection setup.

Source

Thrown at crates/adapters/betfair/src/data.rs:1079

                                continue;
                            }
                        };

                        let _ = reconnect_http
                            .with_session_token(|token| {
                                refresh_stream_sessions(
                                    reconnect_stream.as_ref(),
                                    reconnect_race_stream.as_deref(),
                                    reconnect_cricket_stream.as_deref(),
                                    &reconnect_app_key,
                                    token,
                                    session_replaced,
                                );
                            })
                            .await;
                    }
                })
                .map_err(|e| anyhow::anyhow!("Failed to register Betfair reconnect task: {e}"))?;

            Ok::<(), anyhow::Error>(())
        }
        .await;

        if let Err(e) = session_result {
            if let Err(teardown_error) = self.teardown_partial_connect().await {
                return Err(e.context(format!(
                    "Betfair data startup teardown failed: {teardown_error}"
                )));
            }
            return Err(e);
        }

        self.is_connected.store(true, Ordering::Release);
        setup_guard.disarm();

        log::info!("Betfair data client connected: {}", self.client_id);

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Retry connect after teardown completes, not while it is in progress
  2. Verify the tokio runtime is active for the lifetime of the adapter
  3. Check the inner error `e` to identify the exact registration failure

Example fix

// before: connect racing teardown
let _ = tokio::spawn(async { client.disconnect().await });
client.connect().await?;
// after: sequential lifecycle
tokio::spawn(async { client.disconnect().await }).await.ok();
client.connect().await?;
Defensive patterns

Strategy: retry

Validate before calling

// ensure prior disconnect finished
let _ = client.disconnect().await;

Try / catch

if let Err(e) = market_client.connect().await {
    if e.to_string().contains("reconnect task") {
        tokio::time::sleep(Duration::from_secs(2)).await;
        market_client.connect().await?;
    } else { return Err(e); }
}

Prevention

When it happens

Trigger: The reconnect-task registration future returns Err — typically runtime shutdown in progress, a closed registration channel, or connecting after teardown_partial_connect has begun.

Common situations: Connecting while the actor system is stopping; calling connect from a dying task; a previous failed connect left the client partially torn down.

Related errors


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