Skip to content

Commit

Permalink
runtime: fixed yield_now lost wakeup
Browse files Browse the repository at this point in the history
  • Loading branch information
quininer committed Nov 29, 2024
1 parent 38151f3 commit 35c4ab9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tokio/src/runtime/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
8 changes: 8 additions & 0 deletions tokio/tests/task_yield_now.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 35c4ab9

Please sign in to comment.