Detect connection closed on endpoint with spawned task? #3037
-
SummaryHi, First of all, thank you for axum. It's mostly been a breeze working with it, and so far it's exceeded our expectations. However, I have a case I can't solve in an elegant way. We have an API endpoint that runs for a long time and eventually returns a response. However, if the connection disconnects, the spawned task from the handler continues to run, while the handler task itself is cancelled. Here's the setup (abbreviated):
I couldn't find a good (readable) way to keep the code inside the handler, which is why I put the loop on a different thread and just await the message instead. Perhaps I'm solving it in a bad way, but I can't figure out how to handle the handler being dropped (the loop would cancel if it was in the handler function itself, of course). I read #1094 but it doesn't really answer my use-case. Grateful for any assistance, axum version0.7.7 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I think #1094 is exactly what you should do in cases like this. That said you could also check in your worker thread whether the channel was closed. If the handler finished, it dropped the receiver and you can check that through the sender. |
Beta Was this translation helpful? Give feedback.
-
What's the actual problem with calling |
Beta Was this translation helpful? Give feedback.
What's the actual problem with calling
poll_until_ready
without a separate async task? If you definitely need the channel, how about usingfutures::future::join(poll_until_ready(value, tx), wait_for_completion(rx).await)
(or maybetry_join
)?