Rust Async, Cancellation and transactions question
I work in a domain where we more than occasionally deal with web requests that touch on purchases that people make. We need to go out, check their state, redeem purchases etc.
This is where Rust Async, and particularly cancellation, makes me nervous: In the realm of purchases, not giving something to a person who purchased it is bad. It can be taken to various legal venues in countries, and so it's important not just for the user experience, but legally, to try to ensure that you do everything you can to ensure that you either give the user what they bought, or ensure that you enable it to be recoverable.
Now imagine a Rust web server that's handling those things in an async context - if it's Axum (eg), then your web handler can be cancelled at any await point if the request is cancelled. That could happen in the middle of a request to mark items as consumed, without us being able to know if the request got to a point where it was handled. You really want to guarantee that you have some logs that tell you exactly where it got to, but even this is somewhat complex.
In order to catch the cancellation, you need to write something that's effectively an RAII scopeguard - you need to create a type which logs on drop, and keep it updated with state changes as you progress. You might be able to put a blocking DB call in there, at least until async drop potentially materializes.
However, async drop may also bring complexity with it - I'd assume that it could introduce arbitrary and invisible await points, which in turn could create more invisible points for cancellation and function exit, requiring even more defensive coding against cancellation.
Another approach is to spawn a task that will be detached on drop, ensuring that things like http requests and db updates do not get cancelled part way through due to client disconnection (although, this seems to vary between runtimes).
Once async drop exists, I imagine you could run your sensitive transaction inside an async drop, because it would seem unreasonable for a drop to get cancelled, so it effectively makes it a cancellation-free section. Throw it in an async lambda and you have a cancellation free block with some borrow challenges.
Async drop, and RAII in general, has always seemed to me to be good for simple cleanup. I find it a bit deficient if you're trying to guess at what might have happened or not with an important request or db update.
You could also use channels move the work over to non-async thread pools or async "workers" that loop through and respond to these things specifically, with a join_all on your http server and the workers (although this is basically recreating detach on drop futures).
What approaches do people use or suggest for this sort of thing? Essentially, it would be nice to simply opt out of cancellation on certain sections to avoid having to code as if an "exception" could strike at any time.
Posted Jun 23, 2023, 1:06 PM UTC