nautechsystems/nautilus_trader · error

Stream client not connected

Error message

Stream client not connected

What it means

subscribe_book_deltas requires the Betfair stream client to be present. If self.stream_client is None (set only after a successful connect), the call fails immediately with 'Stream client not connected' instead of attempting to subscribe.

Solutions

  1. Ensure connect() completed successfully before subscribing
  2. Guard with an Option check / await readiness instead of assuming the client exists
  3. Reconnect after any disconnect before retrying the subscription

Example fix

// before
if !market_client.is_connected().await { market_client.connect().await?; }
market_client.subscribe_book_deltas(...).await?;
// after — explicit readiness check
if market_client.stream_ready() {
    market_client.subscribe_book_deltas(...).await?;
} else {
    log::warn!("stream not connected; skipping subscribe");
}
Defensive patterns

Strategy: validation

Validate before calling

if market_client.stream_client().is_none() {
    market_client.connect().await?;
}

Type guard

fn stream_ready(client: &BetfairMarketDataClient) -> bool {
    client.stream_client().is_some()
}

Try / catch

match market_client.subscribe_book_deltas(...).await {
    Err(e) if e.to_string().contains("Stream client not connected") => {
        market_client.connect().await?;
        market_client.subscribe_book_deltas(...).await?;
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling subscribe_book_deltas before connect() succeeded, after disconnect() cleared stream_client, or after a failed reconnect cleared the client.

Common situations: Subscribing during startup before the connection handshake finished; resubscribing after a stream drop without reconnecting; ordering issues where subscription requests arrive ahead of the connect future.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

        self.subscribed_market_ids.clear();

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

    fn subscribe_book_deltas(&mut self, cmd: SubscribeBookDeltas) -> anyhow::Result<()> {
        let instrument_id = cmd.instrument_id;
        let market_id = extract_market_id(&instrument_id)?;

        if !self.subscribed_market_ids.insert(market_id.clone()) {
            log::debug!("Book deltas already subscribed for market {market_id}");
            return Ok(());
        }

        let stream_client = Arc::clone(
            self.stream_client
                .as_ref()
                .ok_or_else(|| anyhow::anyhow!("Stream client not connected"))?,
        );

        let all_ids: Vec<String> = self.subscribed_market_ids.iter().cloned().collect();

        let market_filter = StreamMarketFilter {
            market_ids: Some(all_ids),
            ..Default::default()
        };

        let data_filter = MarketDataFilter {
            fields: Some(vec![
                MarketDataFilterField::ExAllOffers,
                MarketDataFilterField::ExTraded,
                MarketDataFilterField::ExTradedVol,
                MarketDataFilterField::ExLtp,
                MarketDataFilterField::ExMarketDef,
                MarketDataFilterField::SpTraded,
                MarketDataFilterField::SpProjected,

View on GitHub (pinned to 18893faf8b)