nautechsystems/nautilus_trader · error · anyhow::Error

AX execution startup teardown failed: {teardown_error}

Error message

AX execution startup teardown failed: {teardown_error}

What it means

Raised in the AX (Architect) execution client's `connect` when the startup sequence (instruments/stream/venue clients) fails, and the subsequent `teardown_partial_connect` also errors. The teardown failure is attached as `context` to the original error, so the root cause is preserved and the cleanup failure is reported alongside it.

Source

Thrown at crates/adapters/architect_ax/src/execution.rs:598

        let account_id = self.core.account_id;
        let instruments_cache = self.ws_orders.instruments_cache();
        let clock = self.clock;

        if let Err(e) = self.session_tasks.spawn(async move {
            pin_mut!(stream);
            while let Some(message) = stream.next().await {
                dispatch_ws_message(
                    message,
                    &emitter,
                    &caches,
                    account_id,
                    &instruments_cache,
                    clock,
                );
            }
        }) {
            if let Err(teardown_error) = self.teardown_partial_connect().await {
                return Err(anyhow::Error::new(e).context(format!(
                    "AX execution startup teardown failed: {teardown_error}"
                )));
            }
            return Err(e.into());
        }

        let session_result = async {
            let account_state = self
                .http_client
                .request_account_state(self.core.account_id)
                .await
                .context("failed to request AX account state")?;

            if !account_state.balances.is_empty() {
                log::debug!(
                    "Received account state with {} balance(s)",
                    account_state.balances.len()
                );

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Read the full anyhow error chain: the outermost message is teardown; the root source is the original connect failure — fix that first.
  2. Verify AX gateway URL, credentials, and network reachability.
  3. Check logs for the teardown_partial_connect error to find which resource failed to unwind.
  4. Retry connect after transient network recovery; investigate leaked tasks if teardown consistently fails.
Defensive patterns

Strategy: retry

Validate before calling

// Pre-flight: verify gateway reachability before connect
// e.g. TCP/DNS check of the AX gateway endpoint from config

Try / catch

match exec_client.connect().await {
    Err(e) => {
        // root cause is the innermost source; teardown context is ancillary
        log::error!("AX connect failed: {e:#}");
        tokio::time::sleep(backoff).await;
        retry_connect().await;
    }
    Ok(_) => {},
}

Prevention

When it happens

Trigger: Any failure during AX execution connect (e.g. the `?` inside the block returning Err) combined with teardown_partial_connect failing (network already broken, task cancellation, partial state that cannot be unwound).

Common situations: Credentials/network down at startup while cached instrument loaders or background tasks hang or refuse to shut down; node startup racing shutdown; misconfigured AX gateway endpoint.

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