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

task canceled when client connection closed #2728

Closed
polariseye opened this issue Dec 29, 2021 · 4 comments
Closed

task canceled when client connection closed #2728

polariseye opened this issue Dec 29, 2021 · 4 comments
Labels
C-bug Category: bug. Something is wrong. This is bad!

Comments

@polariseye
Copy link

polariseye commented Dec 29, 2021

Version
hyper 0.14.16

Platform
windows

Description
the task was canceled when the http connection closed
One way to structure the description:

[short summary of the bug]
task canceled when client connection closed.
I tried this code:

async fn hello_world(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
    println!("start sleep!!!!!");
    tokio::time::sleep(tokio::time::Duration::from_secs(20)).await;
    println!("end sleep!!!!");
    Ok(Response::new("Hello, World".into()))
}

#[tokio::main]
async fn main() {
    // We'll bind to 127.0.0.1:3000
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));

    // A `Service` is needed for every connection, so this
    // creates one from our `hello_world` function.
    let make_svc = make_service_fn(|_conn| async {
        // service_fn converts our function into a `Service`
        Ok::<_, Infallible>(service_fn(hello_world))
    });

    let server = Server::bind(&addr).serve(make_svc);

    // Run this server for... forever!
    if let Err(e) = server.await {
        eprintln!("server error: {}", e);
    }
}

[code sample that causes the bug]
i tried curl --location --request POST 'http://127.0.0.1:3030'.and then, abort request. the server side just print "start sleep!!!!!".not print "end sleep!!!!"
I expected to see this happen: [explanation]
i want to see print "end sleep!!!!"
Instead, this happened: [explanation]

@polariseye polariseye added the C-bug Category: bug. Something is wrong. This is bad! label Dec 29, 2021
@seanmonstar
Copy link
Member

This is expected behavior. When the connection closes, the relevant task is canceled.

@polariseye
Copy link
Author

well~~could you please tell me how can i let the task will not cancel

@seanmonstar
Copy link
Member

You can't make prevent that specific task from being canceled. But, depending on what you're actually trying to do, you have a few options:

  • If you just want to make sure something definitely happens, like cleaning up a resource or logging, you could create a custom type and impl Drop for it, and then let _guard = MyGuard;.
  • You could move the handler code into a task::spawn, which doesn't cancel normally, and have that spawned task return a Response. Something like:
    async fn hello(req: Request) -> Result {
        tokio::spawn(async move {
            do_stuff(req).await;
            Ok(resp)
        }).await
    }

@polariseye
Copy link
Author

that's good.thank you very mutch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug. Something is wrong. This is bad!
Projects
None yet
Development

No branches or pull requests

2 participants