edge-net: HTTP Server Write Timeout
Is it possible for a handler to be stuck during the write_all phase of a response? Take the code below, if conn.write_all was writing a large buffer or over a slow connection, and the client closed the connection.
Would the handler be stuck trying to write_all forever?
impl<'b, T, const N: usize> Handler<'b, T, N> for HttpHandler
where
T: Read + Write,
T::Error: Send + Sync + std::error::Error + 'static,
{
type Error = anyhow::Error;
async fn handle(&self, conn: &mut Connection<'b, T, N>) -> Result<(), Self::Error> {
let headers = conn.headers()?;
if !matches!(headers.method, Some(Method::Get)) {
conn.initiate_response(405, Some("Method Not Allowed"), &[])
.await?;
} else if !matches!(headers.path, Some("/")) {
conn.initiate_response(404, Some("Not Found"), &[]).await?;
} else {
conn.initiate_response(200, Some("OK"), &[("Content-Type", "text/plain")])
.await?;
conn.write_all(b"Hello world!").await?;
}
Ok(())
}
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 22 (18 by maintainers)
@DaneSlattery @AnthonyGrondin There’s this branch now and the corresponding PR
@DaneSlattery
@AnthonyGrondin
edge-nal-embassy, which should take care of the hanging CURL connection withConnection: Close.embassy-nettesting env will take me quite some time (still deep in STD land with Matter, etc.)Also, and based on the yesterday’s discussion with Dario, I hope the socket shutdown logic is as simple as it gets, and as correct as it gets (modulo trivial bugs), so once we confirm it works fine, you might want to replicate it in the
esp-mbedtls+esp-wificombo.This branch is now in
masteras it definitely fixed at least two issues related to how we handle “old fashioned” HTTP connections which haveConnection: Closeheader.TL;DR: Yes, they are. Future drop in Rust means cancellation. So futures routinely “close” stuff in their
dropmethod.Elaboration: However - if you have to do something - like “closing stuff” - that needs
asyncyou currently cannot do it in the futuredropfn, because thedropis always blocking (and ideally should not wait). This is known as the “rust async drop” problem, where having an “async drop” is just one out of many possibilities (having un-droppable types that you always need to manually destruct is another).Anyway, I digress.
I’m planning to put some “global” timeouts in the HTTP server handling code too, so whatever happens, at some point the HTTP handlers will always get un-stuck.