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)

Most upvoted comments

@DaneSlattery @AnthonyGrondin There’s this branch now and the corresponding PR

@DaneSlattery

  • If you can look at the PR description, this PR now implements proper timeouts for the HTTP server, so with it - whatever happens - the server would never get stuck. You can use the default ones (30min for a request-response and 50sec for each IO operation) or you can tailor these to your liking
  • There is a proper socket shutdown now for STD, even though STD was working even without it

@AnthonyGrondin

  • Besides the above, I’ve also implemented proper socket shutdown for edge-nal-embassy, which should take care of the hanging CURL connection with Connection: Close.
  • However I would be very grateful if you could test that and return feedback, as setting up an embassy-net testing 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-wifi combo.

but perhaps I’ll point my code to this branch and see if it also helps things with the server write.

This branch is now in master as it definitely fixed at least two issues related to how we handle “old fashioned” HTTP connections which have Connection: Close header.

I suspect that futures in rust are cancelled when they are dropped, do you have any literature on that?

TL;DR: Yes, they are. Future drop in Rust means cancellation. So futures routinely “close” stuff in their drop method.

Elaboration: However - if you have to do something - like “closing stuff” - that needs async you currently cannot do it in the future drop fn, because the drop is 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.