Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Allow calling thread.yield-to in sync contexts (#12241)

Sy Brandfc4020ba6e6a
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index bd1a8b6f74..d42e6aea0d 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -585,7 +585,10 @@ enum SuspendReason {
NeedWork,
/// The fiber is yielding and should be resumed once other tasks have had a
/// chance to run.
- Yielding { thread: QualifiedThreadId },
+ Yielding {
+ thread: QualifiedThreadId,
+ skip_may_block_check: bool,
+ },
/// The fiber was explicitly suspended with a call to `thread.suspend` or `thread.switch-to`.
ExplicitlySuspending {
thread: QualifiedThreadId,
@@ -1557,6 +1560,9 @@ impl StoreOpaque {
} | SuspendReason::Waiting {
skip_may_block_check: true,
..
+ } | SuspendReason::Yielding {
+ skip_may_block_check: true,
+ ..
}
) || old_guest_thread
.map(|thread| self.concurrent_state_mut().may_block(thread.task))
@@ -3265,6 +3271,10 @@ impl Instance {
let reason = if yielding {
SuspendReason::Yielding {
thread: guest_thread,
+ // Tell `StoreOpaque::suspend` it's okay to suspend here since
+ // we're handling a `thread.yield-to` call; otherwise it would
+ // panic if we called it in a non-blocking context.
+ skip_may_block_check: to_thread.is_some(),
}
} else {
SuspendReason::ExplicitlySuspending {
@@ -3477,7 +3487,12 @@ impl Instance {
};
concurrent_state.push_high_priority(item);
- store.suspend(SuspendReason::Yielding { thread: caller })?;
+ store.suspend(SuspendReason::Yielding {
+ thread: caller,
+ // `subtask.cancel` is not allowed to be called in a
+ // sync context, so we cannot skip the may-block check.
+ skip_may_block_check: false,
+ })?;
break;
}
}