nautechsystems/nautilus_trader · error

Failed to create stream

Error message

Failed to create stream

What it means

In `request_contract_events_stream`, the code calls `client.clone().stream(query, StreamConfig::default()).await.expect("Failed to create stream")` to open a HyperSync event stream. The `stream` call is async and returns a `Result`; it fails when the query is rejected or the remote HyperSync endpoint cannot establish the stream (auth rejection, bad query shape, unreachable host). Since the panic occurs inside a public method that returns a stream, any stream-setup failure crashes the caller rather than yielding an error item.

Source

Thrown at crates/adapters/blockchain/src/hypersync/client.rs:250

        from_block: u64,
        to_block: Option<u64>,
        contract_address: &Address,
        topics: Vec<&str>,
    ) -> impl Stream<Item = PoolEventStreamItem> + use<> {
        let query = Self::construct_contract_events_query(
            from_block,
            to_block,
            &[*contract_address],
            &topics,
        );

        let chain = self.chain.name;
        let mut rx = self
            .client
            .clone()
            .stream(query, StreamConfig::default())
            .await
            .expect("Failed to create stream");

        async_stream::stream! {
              while let Some(response) = rx.recv().await {
                let response = response.unwrap();
                for item in pool_events_from_response(chain, response.data.blocks, response.data.logs) {
                    yield item;
                }
            }
        }
    }

    /// Disconnects from the HyperSync service and stops all background tasks.
    pub async fn disconnect(&mut self) {
        log::debug!("Disconnecting HyperSync client");
        self.cancellation_token.cancel();

        if let Some(outcome) = finish_task(
            &mut self.blocks_task,

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Verify network reachability and that the HyperSync endpoint URL is correct and up.
  2. Confirm `ENVIO_API_TOKEN` is a valid, active UUID — auth failures surface at stream creation.
  3. Validate the query (block range, topics, contract addresses) against the hypersync-client schema for your version.
  4. Restructure the call site to `match`/`?` the `stream(...)` result and surface a `Retryable`/error outcome instead of `expect`.
  5. Add retry with backoff around stream creation for transient endpoint failures.

Example fix

// before
let mut rx = self.client.clone().stream(query, StreamConfig::default()).await
    .expect("Failed to create stream");
// after
let mut rx = self.client.clone().stream(query, StreamConfig::default()).await
    .map_err(|e| anyhow::anyhow!("hypersync stream setup failed: {e}"))?;
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-flight: confirm the endpoint accepts a cheap request before opening a long stream
let probe = reqwest::get(format!("{}/health", chain.hypersync_url.trim_end_matches('/')))
    .await
    .map_err(|e| anyhow::anyhow!("hypersync endpoint unreachable: {e}"))?;
anyhow::ensure!(probe.status().is_success(), "hypersync health check failed");

Try / catch

// Replace expect with Result mapping and retry transient failures
match self.client.clone().stream(query, StreamConfig::default()).await {
    Ok(rx) => rx,
    Err(e) if e.is_retryable() => /* backoff and retry */,
    Err(e) => return Err(anyhow::anyhow!("hypersync stream setup failed: {e}")),
}

Prevention

When it happens

Trigger: Calling `request_contract_events_stream` when the HyperSync endpoint rejects the query/stream setup: invalid or revoked API token, malformed query (bad range/topic/filter combination), endpoint downtime or network failure, or a URL pointing at a non-responsive host.

Common situations: Network outages or firewall blocks while opening a live subscription; query asking for a block range or topic set the endpoint refuses; rate limits or account issues causing auth rejection at stream open; stale test infrastructure hitting an unreachable HyperSync host.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


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