nautechsystems/nautilus_trader · error

Session ended by gateway

Error message

Session ended by gateway

What it means

The Databento gateway closed the stream (record stream returned None) before the session reached the configured success_threshold duration. run_session treats a short-lived session as a failure and bails so the reconnection loop can restart it.

Source

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

                        log::debug!("Closed inner client");
                        return Ok(false);
                    }
                    None => {
                        log::debug!("Command channel disconnected");
                        return Ok(false);
                    }
                },
                result = client.next_record() => result,
            };

            let record = match record_opt {
                Ok(Some(record)) => record,
                Ok(None) => {
                    if session_start.elapsed() >= self.success_threshold {
                        log::debug!("Session ended after successful run");
                        return Ok(true);
                    }
                    anyhow::bail!("Session ended by gateway");
                }
                Err(e) => {
                    if session_start.elapsed() >= self.success_threshold {
                        log::debug!("Connection error after successful run: {e}");
                        return Ok(true);
                    }
                    anyhow::bail!("Connection error: {e}");
                }
            };

            let ts_init = clock.get_time_ns();

            // Decode record
            if let Some(msg) = record.get::<dbn::ErrorMsg>() {
                handle_error_msg(msg);
            } else if let Some(msg) = record.get::<dbn::SystemMsg>() {
                if let Some(ack) = handle_system_msg(msg, ts_init) {
                    self.send_msg(DatabentoMessage::SubscriptionAck(ack));

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Rely on the adapter's reconnect loop — the error signals a retry is warranted
  2. Lower success_threshold so healthy short sessions are not treated as failures
  3. Check Databento gateway status and network stability
  4. Inspect logs for a pattern indicating server-side disconnects
Defensive patterns

Strategy: retry

Validate before calling

// ensure success_threshold is realistic relative to gateway idle timeouts
assert!(config.success_threshold < GATEWAY_IDLE_DISCONNECT_SECS);

Try / catch

loop {
    match session.run_session().await {
        Ok(true) => break, // healthy session
        Err(e) if e.to_string().contains("Session ended by gateway") => {
            warn!("gateway ended session, reconnecting");
            continue;
        }
        Err(e) => return Err(e),
    }
}

Prevention

When it happens

Trigger: The dbn record channel yields Ok(None) while session_start.elapsed() < self.success_threshold — gateway disconnect, idle timeout, or server-side session termination.

Common situations: Long-lived live sessions dropped by Databento, network instability causing gateway to end sessions, success_threshold set longer than the gateway's idle disconnect interval.

Related errors


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