nautechsystems/nautilus_trader · error · anyhow::Error

Betfair execution startup teardown failed: {teardown_error}

Error message

Betfair execution startup teardown failed: {teardown_error}

What it means

Raised in the Betfair execution client's `connect` when constructing the stream client fails and the subsequent `teardown_partial_connect` also errors; the teardown error is attached as anyhow context to the original error. It signals both a failed startup and a failed partial-cleanup.

Source

Thrown at crates/adapters/betfair/src/execution.rs:1564

        };
        let state_sink = match self.socket_control.as_ref() {
            Some(control) => control.sink_with(halt_on_disconnect),
            None => SocketStateSink::new(halt_on_disconnect),
        };

        let stream_client_result = BetfairStreamClient::connect_with_state_sink(
            &self.credential,
            session_token,
            handler,
            self.stream_config.clone(),
            HeartbeatTimeoutSource::Server,
            Some(state_sink),
        )
        .await;
        let stream_client = match stream_client_result {
            Ok(stream_client) => stream_client,
            Err(e) => {
                let e = anyhow::Error::new(e);
                if let Err(teardown_error) = self.teardown_partial_connect().await {
                    return Err(e.context(format!(
                        "Betfair execution startup teardown failed: {teardown_error}"
                    )));
                }
                return Err(e);
            }
        };

        let stream_client = Arc::new(stream_client);

        if let Err(e) = stream_client.subscribe_orders(None, None).await {
            if let Err(close_error) = stream_client.close().await {
                log::warn!(
                    "Failed to close Betfair order stream after subscribe failure: {close_error}"
                );
            }
            let e = anyhow::Error::new(e);

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Inspect the root error source (chain) — fix the original stream connection failure first (credentials, app key, network).
  2. Check logs for the teardown_partial_connect error to identify the resource that failed cleanup.
  3. Refresh Betfair credentials/session token and retry connect.
  4. If teardown persistently fails, look for leaked stream/heartbeat tasks in the adapter.
Defensive patterns

Strategy: retry

Validate before calling

// Verify Betfair session token validity before connect
// e.g. call keep_alive / login API and check HTTP 200

Try / catch

match exec_client.connect().await {
    Err(e) => {
        log::error!("Betfair connect failed: {e:#}"); // prints full chain incl. teardown
        refresh_session_token().await?;
        retry_with_backoff().await;
    }
    Ok(_) => {},
}

Prevention

When it happens

Trigger: Stream client creation (auth/token handshake to Betfair stream API) errors, and teardown_partial_connect cannot cleanly unwind already-started resources (e.g. execution client already spawned, order stream partially established).

Common situations: Expired/invalid Betfair session token, network drop mid-handshake, node stopping while connect is in flight, or misconfigured Betfair app key.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


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