Handler future gets cancelled early when body isn't read #2724
-
Version Platform Description I have two channels. Something like this: let (request_tx, mut request_rx) = mpsc::channel(1); // (Sender<Request<Body>>, Reciever<Request<Body>>)
let (response_tx, response_rx) = mpsc::channel(1); // (Sender<Response<Body>>, Reciever<Response<Body>>) The hyper request gets recv'd successfully (by the current task) but the response, although sent successfully, never gets recv'd by the Here's what the Http::new()
.with_executor(LocalExecutor)
.serve_connection(
tcp_stream,
service_fn(move |request| {
let request_tx = request_tx.clone();
let response_rx = Rc::clone(&response_rx);
async move {
let mut response_rx = response_rx.borrow_mut();
let mut response = Response::default();
// Send request.
if let Err(err) = request_tx.send(request).await {
println!("\n\n>>>> HTTP_DRIVER: Error sending response = {:?}", err);
return Ok(response);
}
println!("\n\n>>>> HTTP_DRIVER: Waiting to recieve response!");
// Wait for response.
if let Some(resp) = response_rx.recv().await {
response = resp;
}
println!("\n\n>>>> HTTP_DRIVER: Response is never recieved! This is not printed!");
Ok::<_, Infallible>(response)
}
}),
)
.await
.unwrap(); The local executor. #[derive(Clone)]
struct LocalExecutor;
impl<F> Executor<F> for LocalExecutor
where
F: std::future::Future + 'static,
{
fn execute(&self, fut: F) {
tokio::task::spawn_local(fut);
}
} It is as if the service_fn future gets cancelled before it can use it so the server returns an empty response The curious thing here is that reading request body makes it work as expected. The full example is here https://gist.github.com/appcypher/c7aeb8fc9d46f053601b366f83b98c1a#file-server-rs. Do you know what I'm doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
I've tried your code locally, and I don't see the behavior you're describing. It might be something to do with the request you're sending? I test this with just |
Beta Was this translation helpful? Give feedback.
I've tried your code locally, and I don't see the behavior you're describing. It might be something to do with the request you're sending? I test this with just
curl http://127.0.0.1:6060/wat
.