Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read response as a continuous stream? #53

Open
blaind opened this issue Aug 22, 2019 · 3 comments
Open

How to read response as a continuous stream? #53

blaind opened this issue Aug 22, 2019 · 3 comments
Labels
bug Something isn't working

Comments

@blaind
Copy link

blaind commented Aug 22, 2019

I'm trying to make a GET request where the response is a (slow) continuous stream. However, the response reading results to a core dump.

async fn connect_to_server(&mut self) -> Result<()> {
    let mut reader = surf::get("http://...[server-address]").await?;

    println!("{:?}", reader.status());

    let mut buf = [0_u8; 1024];

    loop {
        let n = reader.read(&mut buf).await?;
        println!("Read data {:?} {}", &buf[..16], n);
    }

    Ok(())
}

stdout says:

200
Read data [13, 13, 23, ...] 12
Read data [15, 99, 121, ...] 887
Read data [15, 99, 121, ...] 0
thread 'async-task-driver' panicked at 'Receiver::next_message called after `None`', src/libcore/option.rs:1166:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
Aborted (core dumped)

Any ideas how to solve this? Here's the relevant part of the backtrace:

  11: core::option::Option<T>::expect
             at /rustc/4cf7673076e6975532213e494dd3f7f9d8c2328e/src/libcore/option.rs:345
  12: futures_channel::mpsc::Receiver<T>::next_message
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-channel-preview-0.3.0-alpha.17/src/mpsc/mod.rs:842
  13: <futures_channel::mpsc::Receiver<T> as futures_core::stream::Stream>::poll_next
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-channel-preview-0.3.0-alpha.17/src/mpsc/mod.rs:912
  14: <sluice::pipe::chunked::Reader as futures_io::if_std::AsyncRead>::poll_read
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/sluice-0.4.1/src/pipe/chunked.rs:83
  15: <sluice::pipe::PipeReader as futures_io::if_std::AsyncRead>::poll_read
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/sluice-0.4.1/src/pipe/mod.rs:43
  16: <chttp::body::Body as futures_io::if_std::AsyncRead>::poll_read
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/chttp-0.5.5/src/body.rs:157
  17: <alloc::boxed::Box<T> as futures_io::if_std::AsyncRead>::poll_read
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-io-preview-0.3.0-alpha.17/src/lib.rs:352
  18: <surf::http_client::Body as futures_io::if_std::AsyncRead>::poll_read
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/surf-1.0.1/src/http_client/mod.rs:83
  19: <&mut T as futures_io::if_std::AsyncRead>::poll_read
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-io-preview-0.3.0-alpha.17/src/lib.rs:352
  20: <surf::response::Response as futures_io::if_std::AsyncRead>::poll_read
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/surf-1.0.1/src/response.rs:246
  21: <&mut T as futures_io::if_std::AsyncRead>::poll_read
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-io-preview-0.3.0-alpha.17/src/lib.rs:352
@CryZe
Copy link

CryZe commented Aug 22, 2019

This is like a duplicate of #36

@blaind
Copy link
Author

blaind commented Aug 23, 2019

Now that #36 might be fixed, using commit 13485cb the reading works for longer (a few seconds), but still crashes. Using reader.read_exact(&mut buf) is a bit better - crash after ~30 seconds.

I tried using isahc directly, and no problem with it (version 0.7.0).

async fn connect_to_server(&mut self) -> Result<()> {
        let mut response = Request::get("http://URL")
            .body(())?
            .send_async()
            .await?;

        println!("Status: {}", response.status());
        let body = response.body_mut();

        let mut buf = [0_u8; 1024];

        loop {
            body.read_exact(&mut buf).await?;
            println!("Read data {:?}", &buf[..16]);
        }
        Ok(())
}

@yoshuawuyts yoshuawuyts added the bug Something isn't working label Aug 24, 2019
@yoshuawuyts
Copy link
Member

This seems to be a bug, and we should investigate further. Thanks heaps for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants