Skip to content

Commit f8948ea

Browse files
authored
runtime: do not defer yield_now inside block_in_place (#6999)
1 parent bce9780 commit f8948ea

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

tokio/src/runtime/context.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,14 @@ cfg_rt! {
183183
#[track_caller]
184184
pub(super) fn with_scheduler<R>(f: impl FnOnce(Option<&scheduler::Context>) -> R) -> R {
185185
let mut f = Some(f);
186-
CONTEXT.try_with(|c| c.scheduler.with(f.take().unwrap()))
186+
CONTEXT.try_with(|c| {
187+
let f = f.take().unwrap();
188+
if matches!(c.runtime.get(), EnterRuntime::Entered { .. }) {
189+
c.scheduler.with(f)
190+
} else {
191+
f(None)
192+
}
193+
})
187194
.unwrap_or_else(|_| (f.take().unwrap())(None))
188195
}
189196

tokio/tests/task_yield_now.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(unknown_lints, unexpected_cfgs)]
2-
#![cfg(all(feature = "full", tokio_unstable))]
2+
#![cfg(all(feature = "full", not(target_os = "wasi"), tokio_unstable))]
33

44
use tokio::task;
55
use tokio_test::task::spawn;
@@ -15,3 +15,11 @@ fn yield_now_outside_of_runtime() {
1515
assert!(task.is_woken());
1616
assert!(task.poll().is_ready());
1717
}
18+
19+
#[tokio::test(flavor = "multi_thread")]
20+
async fn yield_now_external_executor_and_block_in_place() {
21+
let j = tokio::spawn(async {
22+
task::block_in_place(|| futures::executor::block_on(task::yield_now()));
23+
});
24+
j.await.unwrap();
25+
}

0 commit comments

Comments
 (0)