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

Reqwest hangs when manually polled in impl Future block #949

Closed
gakonst opened this issue Jun 15, 2020 · 2 comments
Closed

Reqwest hangs when manually polled in impl Future block #949

gakonst opened this issue Jun 15, 2020 · 2 comments

Comments

@gakonst
Copy link

gakonst commented Jun 15, 2020

For some reason the following code hangs. I sense that I might be doing something fundamentally wrong and that I should probably be storing the future inside the struct?

Repro:

use std::future::Future;
use std::task::{Context, Poll};
use std::pin::Pin;

#[derive(Clone, Debug)]
pub struct Thing;

impl Future for Thing {
    type Output = ();

    fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
        let mut fut = Box::pin(reqwest::get("https://httpstat.us/200"));
        let _res = match fut.as_mut().poll(ctx) {
            Poll::Ready(ret) => ret,
            Poll::Pending => return Poll::Pending,
        };

        // do something with the result
        Poll::Ready(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_thing() {
        let _logger = env_logger::init();
        let thing = Thing;
        // hangs here
        let ret = thing.await;
        dbg!(ret);
    }
}

Running it with RUST_LOG = hyper results in the following logs:

[2020-06-15T10:50:47Z TRACE hyper::client::pool] checkout waiting for idle connection: ("https", httpstat.us)
[2020-06-15T10:50:47Z TRACE hyper::client::connect::http] Http::connect; scheme=Some("https"), host=Some("httpstat.us"), port=None
[2020-06-15T10:50:47Z TRACE hyper::client::pool] checkout dropped for ("https", httpstat.us)
[2020-06-15T10:50:47Z DEBUG hyper::client::connect::dns] resolving host="httpstat.us"
[dependencies]
reqwest = "0.10.6"
env_logger = "0.7.1"

[dev-dependencies]
tokio = { version = "0.2.21", features = ["macros"] }
@seanmonstar
Copy link
Owner

If you drop the future after polling, it will cancel and never wake up the task again. You need to store it in your custom future. You might find it easier to just use an async fn.

@gakonst
Copy link
Author

gakonst commented Jun 15, 2020

Yeah thought so, thank you for confirming.

@gakonst gakonst closed this as completed Jun 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants