Skip to content

Response::chunk(...) will return Ok(Some([])) before returning Ok(None) at the end of the response under certain circumstances #2841

@richardstephens

Description

@richardstephens

When processing a response body one response.chunk() at a time, sometimes a single Ok(Some([])) (0-byte chunk) will be returned before the response starts returning Ok(None).

I am not 100% certain exactly what causes this but it seems to require both rustls and http2 to be enabled, and also be dependant on the remote server and the response being large enough.

here is a simple reproducer case

[package]
name = "httpbugtest"
version = "0.1.0"
edition = "2024"

[dependencies]
reqwest = { version = "0.12.24", default-features = false, features = ["rustls-tls", "rustls-tls-webpki-roots", "http2"] }
tokio = { version = "1.48.0", features = ["macros", "rt-multi-thread"] }
#[tokio::main]
async fn main() {
    let client = reqwest::Client::new();
    let mut response = client
        .get("https://bunny.net/")
        .send().await.unwrap();

    let mut total_bytes = 0;
    while let Some(chunk) = response.chunk().await.unwrap() {
        total_bytes += chunk.len();
        if chunk.len() == 0 {
            eprintln!("Zero-length chunk received at offset={total_bytes}");
        }
    }
    eprintln!("Total bytes received: {}", total_bytes);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions