runtime: do not defer yield_now inside block_in_place (#6999)

This commit is contained in:
quininer 2024-12-02 20:52:00 +08:00 committed by GitHub
parent bce9780dd3
commit f8948ea021
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View File

@ -183,7 +183,14 @@ cfg_rt! {
#[track_caller]
pub(super) fn with_scheduler<R>(f: impl FnOnce(Option<&scheduler::Context>) -> R) -> R {
let mut f = Some(f);
CONTEXT.try_with(|c| c.scheduler.with(f.take().unwrap()))
CONTEXT.try_with(|c| {
let f = f.take().unwrap();
if matches!(c.runtime.get(), EnterRuntime::Entered { .. }) {
c.scheduler.with(f)
} else {
f(None)
}
})
.unwrap_or_else(|_| (f.take().unwrap())(None))
}

View File

@ -1,5 +1,5 @@
#![allow(unknown_lints, unexpected_cfgs)]
#![cfg(all(feature = "full", tokio_unstable))]
#![cfg(all(feature = "full", not(target_os = "wasi"), tokio_unstable))]
use tokio::task;
use tokio_test::task::spawn;
@ -15,3 +15,11 @@ fn yield_now_outside_of_runtime() {
assert!(task.is_woken());
assert!(task.poll().is_ready());
}
#[tokio::test(flavor = "multi_thread")]
async fn yield_now_external_executor_and_block_in_place() {
let j = tokio::spawn(async {
task::block_in_place(|| futures::executor::block_on(task::yield_now()));
});
j.await.unwrap();
}