-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spawn_blocking closures non-deterministically fail when runtime is dropping tasks #4834
Comments
This is fixed by the (currently unreleased) PR #4811. |
(The answer to what's going on is that they can be cancelled if they have not already started running.) |
Thanks for explaining and sharing the really helpful docs! In that case it seems like putting |
@ekzhang what exactly do you need to do in the |
You could just use |
@Noah-Kennedy The basic example is that I was trying to delete a This is a spawned task and several function calls deep, a tempdir is used in one of the functions. Since the task can be canceled I would like to make sure that the Another example in my specific case, besides FS resources, is a file system mount (OverlayFS and FUSE), which needs to be unmounted with the @cgwalters I don't think Thanks a lot for the help though!! I understand that this isn't in scope for the executor and can try to write this part of my system without using Tokio. |
I have an opinion on this one: https://internals.rust-lang.org/t/should-rust-programs-unwind-on-sigint/13800/11 |
Ugh, this is a case which has bitten me before. In a lot of these cases, you can usually get away with performing the calls inline since they are generally fast enough to be effectively non-blocking. That might actually be your best option possibly, combined with |
Version v1.19.2 (minimal reproduction)
Platform Darwin Kernel Version 21.5.0 ARM64. I'm running on an M1 Pro processor with 10 logical cores.
Description The documentation for
task::spawn_blocking
says the following:However, in some cases when adding a
spawn_blocking
call to theDrop
implementation of a structure, the blocking call does not execute.I tried this code:
When I run this code, it sometimes runs the blocking closure and sometimes does not. This is inconsistent between successive runs, even without recompiling the code. For example, I just ran it 5 times and pasted the terminal output below: In runs number 1, 2, and 5 below, it doesn't run the blocking closure. In runs 3 and 4, it runs the closure. In no cases does the runtime panic or otherwise show any signs of failure.
I expected to see this happen: The executor would schedule and wait indefinitely for the
spawn_blocking
tasks to finish, as described in the quoted documentation section. Or perhaps panic if this usage ofspawn_blocking
isn't valid, rather than silently fail? It's also a little bit surprising that the behavior is non-deterministic.For completeness, here are some variations and the behavior I noticed but don't know how to explain, maybe you find it helpful or not:
// thread::sleep(Duration::from_secs(1));
), then thespawn_blocking
call happens never, rather than sometimes.tokio::spawn
call that placesA
in a runtime task, thenA
is dropped at the end of themain
function instead of by the runtime internally, and the blocking closure always executes to completion, printingInside A blocking\nfinished A blocking
as I would expect.Thank you!
The text was updated successfully, but these errors were encountered: