How to handle request cancelation?
- Dominant language
- Rust
- Stars
- 166
- Forks
- 49
- PR merge metrics
- No merged PRs in 30d
Description
How can I detect from a handler that the underlying request is canceled? Other server implementations such as `hyper` drop the handle future on request cancelation, but it seems `async-h1` doesn't.
How to reproduce:
```rust
struct DropGuard;
impl Drop for DropGuard {
fn drop(&mut self) {
println!("dropped!")
}
}
// Take a TCP stream, and convert it into sequential HTTP request / response pairs.
async fn accept(stream: TcpStream) -> http_types::Result<()> {
println!("starting new connection from {}", stream.peer_addr()?);
async_h1::accept(stream.clone(), |_req| async move {
let g = DropGuard;
println!("got request!");
async_std::task::sleep(std::time::Duration::from_secs(3)).await;
println!("sending response!");
let mut res = Response::new(StatusCode::Ok);
res.insert_header("Content-Type", "text/plain");
res.set_body("Hello world");
Ok(res)
})
.await?;
Ok(())
}
```
In a terminal, `curl localhost:8080` and ctrl+C before the 3 second timeout.
What you see is:
```
got request!
...(3 seconds later)
sending response!
dropped!!!!
```
what you would see if the future was dropped is:
```
got request!
...(on request cancel, less than 3 seconds later)
dropped!!!!
```
Contributor guide
Research direction
Start at the async_h1::accept call shown in the issue and reproduce the behavior with the provided handler and curl cancellation steps. Compare the handler lifecycle with the described hyper behavior; done means request cancellation is detectable from the handler, or the supported limitation is clearly documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100