by codeflo · 4y ago · 0 comments
What dropping futures does in Rust is much more forceful, and possibly meant for a different usecase, or as a lower-level primitive.
Unless you treat threads like a full process (no shared state) then you need cooperative cancellation (CancellationToken) which generally works well its nice to have common agreed upon cancellation message most commonly used in async I/O calls like a long running a DB query and wanting to cancel it if the client disconnects because they navigated away.
If you have uncooperative code that may say go into an infinite loop then you need to somehow preempt it if it runs on too long so then you have Thread.Abort or really Process.Kill which is the only safe way to do this in .Net (run it in another process).
[1] https://docs.microsoft.com/en-us/dotnet/api/system.threading...
It also references Javas deprecated Thread.stop which looks very similar to .Nets Thread.Abort
Emphasis mine. In Rust, "cancellation" happens at well defined "await points".
https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.h...
I remember trying to use it once, in my more reckless days and despite the warnings, thinking it's all fine if I just wrap all intermediate states in try/finally. It turns out that sometimes this will cause a throw in the finally clean-up, so you need to use a try/catch+rethrow and repeat yourself a bit. It's still not quite right, though, because you can still throw in the catch...