Closed Eliah-Lakhin closed 4 years ago
I encountered this problem, after looking at the code, it's the reason:
runtime::block_on
just wait for the feature you pass in, not care about other spawned futures, when block_on
finished, the runtime finished, then it can't call block_in_place
on the stoping runtime. This means that you need a blocking wait completion of spawn futures, which is inconsistent with the behavior of previous versions of tokio
the source code: https://github.com/tokio-rs/tokio/blob/4261ab6627d1da5e5c70a4b88ccdfc329334716b/tokio/src/runtime/enter.rs#L99-L105
I think you should write like this:
#[tokio::test(threaded_scheduler)]
async fn foo() {
let handle = tokio::task::spawn(async {
let res = tokio::task::spawn_blocking(|| {
});
println!("test1");
res
});
handle.await;
}
@driftluo Thank you for your reply!
As I mentioned in my post, the code snippet has a bug for a reason. And you absolutely right, await
ing for the top task resolves this issue. However, even if the code is buggy, I think it shouldn't lead to the Tokio internal errors inconsistently. There could be something deeper inside Tokio aside from this issue, idk. Anyway, the Issue was opened by @carllerche suggestion mostly for tracking purposes: to note we possibly have something that leads to internal error.
Thanks for the report. I have a fix pending in #1875.
Version
Tokio 0.2.1
Platform
Linux nuc-s 5.0.0-36-generic #39~18.04.1-Ubuntu SMP Tue Nov 12 11:09:50 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
Description
The following snippet has a known issue(
spawn_blocking
is called when the runtime is shutting down), but it leads to the internal Tokio panic from time to time:Stack trace is: