nautechsystems/nautilus_trader · critical

Failed to connect to Databento LSG: {e}

Error message

Failed to connect to Databento LSG: {e}

What it means

This error is thrown by the Databento live streaming adapter when the initial or reconnecting call to establish the LSG (Live Streaming Gateway) client connection fails. The underlying Databento client error (authentication, network, DNS, subscription limits) is wrapped via anyhow::bail. It aborts run_session before any data is received.

Source

Thrown at crates/adapters/databento/src/live.rs:598

            base.user_agent_extension(NAUTILUS_USER_AGENT.into())
                .key(api_key.expose_secret().to_owned())?
                .dataset(dataset)
                .build()
                .await
        })
        .await?;

        let mut client = match result {
            Ok(client) => {
                if attempt > 1 {
                    log::info!("Reconnected successfully");
                } else {
                    log::info!("Connected");
                }
                client
            }
            Err(e) => {
                anyhow::bail!("Failed to connect to Databento LSG: {e}");
            }
        };

        // Process any commands buffered during reconnection backoff
        let mut start_buffered = false;

        if !self.buffered_commands.is_empty() {
            log::debug!(
                "Processing {} buffered commands",
                self.buffered_commands.len()
            );

            for cmd in self.buffered_commands.drain(..) {
                match cmd {
                    HandlerCommand::Subscribe(sub) => {
                        if !self.replay && sub.start.is_some() {
                            self.replay = true;
                        }

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Verify the Databento API key is valid and has access to the configured dataset
  2. Check network connectivity and that the LSG host/port is reachable (no firewall/proxy block)
  3. Retry after checking Databento status page for outages
  4. Review the wrapped inner error {e} for the root cause (auth vs network)

Example fix

// before
client connect fails silently with generic key
api_key: Option<String>
// after
let api_key = std::env::var("DATABENTO_API_KEY")
    .expect("DATABENTO_API_KEY must be set");
Defensive patterns

Strategy: retry

Validate before calling

let api_key = std::env::var("DATABENTO_API_KEY")?;
assert!(!api_key.is_empty());

Try / catch

match adapter.run().await {
    Ok(_) => {},
    Err(e) if e.to_string().contains("Failed to connect") => {
        tokio::time::sleep(BACKOFF).await; // then retry
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: run_session calls client connect; the handshake returns Err — bad API key, unreachable gateway host, network outage, or Databento rejecting the dataset/subscription.

Common situations: Expired or invalid DATABENTO_API_KEY, no internet / DNS failure, wrong dataset id, firewall blocking the LSG port, Databento service maintenance windows.

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