Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/napi-native-concurrency.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:
- "lib/**"
- "rust-toolchain.toml"
- "src/**"
- "tests/foreground_task_waker.rs"
- "tests/concurrent_*.rs"
- "tests/wat_guest/**"
- "v8/**"
Expand All @@ -28,6 +29,7 @@ on:
- "lib/**"
- "rust-toolchain.toml"
- "src/**"
- "tests/foreground_task_waker.rs"
- "tests/concurrent_*.rs"
- "tests/wat_guest/**"
- "v8/**"
Expand Down Expand Up @@ -75,7 +77,8 @@ jobs:

# WAT guests, each in its own Wasmer store and thread, against the real
# C++ bridge and V8. No WASIX C toolchain is needed.
- name: Test concurrent environments and stores
- name: Test concurrent environments and stores, and the foreground task waker
run: >-
./cargo-standalone.sh test --locked
--test foreground_task_waker
--test concurrent_envs --test concurrent_stores
89 changes: 89 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,95 @@ impl Display for WorkerThreadsAlreadyFixed {

impl std::error::Error for WorkerThreadsAlreadyFixed {}

/// What [`set_foreground_task_waker`] installed.
#[cfg(not(all(target_arch = "wasm32", feature = "js")))]
type ForegroundTaskWaker = std::sync::Arc<dyn Fn() + Send + Sync>;

#[cfg(not(all(target_arch = "wasm32", feature = "js")))]
static FOREGROUND_TASK_WAKER: std::sync::RwLock<Option<ForegroundTaskWaker>> =
std::sync::RwLock::new(None);

/// What the V8 platform calls while a waker is installed.
#[cfg(not(all(target_arch = "wasm32", feature = "js")))]
extern "C" fn wake_for_foreground_task() {
// A panic must not unwind into V8, so the clone and its drop run inside
// this guard. The call has a guard of its own: the clone can be the last
// reference to the waker, and if its drop ran while a panic in the waker
// unwound, a panicking destructor among its captures would abort the
// process.
let result = std::panic::catch_unwind(|| {
// The lock is released before the call, so the waker may replace
// itself.
let waker = match FOREGROUND_TASK_WAKER.read() {
Ok(slot) => slot.clone(),
Err(poisoned) => poisoned.into_inner().clone(),
};
if let Some(waker) = waker {
let called = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| waker()));
// Dropping a payload could panic in turn: leak it instead.
if let Err(payload) = called {
std::mem::forget(payload);
}
}
});
if let Err(payload) = result {
std::mem::forget(payload);
}
}

/// Sets what is called, process-wide, when V8 queues a foreground task that
/// only a guest's next event-loop checkpoint runs.
///
/// A guest's foreground tasks wait in a queue that
/// `unofficial_napi_event_loop_checkpoint` runs, one task per checkpoint. A
/// guest can't have them handed to a hook of its own instead: the bridge gives
/// `unofficial_napi_attach_env` no native hooks. V8 posts these tasks from any
/// thread: to resolve an `Atomics.waitAsync` that another thread's
/// `Atomics.notify` woke, to finish a `WebAssembly.compile`, to run
/// `FinalizationRegistry` callbacks. A guest that sleeps until its next host
/// event is not told, so such a task waits until something else wakes the
/// guest. A host passes a waker that ends that sleep; the guest's next turn
/// takes the checkpoint that runs the task.
///
/// The waker is called after an immediate task is queued, on the thread that
/// posted it: another guest's thread, a V8 worker thread, or the guest's own
/// thread in the middle of a guest call or a garbage collection. V8 may hold
/// its own locks then. So the waker must be quick, must not call into V8 or
/// run guest code, and must not take a lock that the host holds around guest
/// calls. It is called for every isolate's tasks, so a host running several
/// guests wakes them all, and a guest with nothing to run waits again. Delayed
/// tasks don't call it: each runs at the first checkpoint after its delay. A
/// panic in the waker is caught, and its payload leaked, so that it never
/// unwinds into V8.
///
/// `None` removes the waker. A waker may call this function itself. A call
/// that was already under way may still run the waker it replaced after this
/// returns, and then drop the last reference to it on the posting thread, so
/// what a waker captures must not call into V8 when it is dropped either.
#[cfg(not(all(target_arch = "wasm32", feature = "js")))]
pub fn set_foreground_task_waker(waker: Option<std::sync::Arc<dyn Fn() + Send + Sync>>) {
let installed = waker.is_some();
let previous = {
let mut slot = match FOREGROUND_TASK_WAKER.write() {
Ok(slot) => slot,
Err(poisoned) => poisoned.into_inner(),
};
let previous = std::mem::replace(&mut *slot, waker);
// Under the same lock, so that concurrent calls leave the slot and the
// platform's pointer in agreement. Without a waker, the platform gets
// no function at all and calls nothing.
// Safety: a plain function that lives as long as the process.
unsafe {
snapi::snapi_bridge_set_foreground_task_waker(
installed.then_some(wake_for_foreground_task as extern "C" fn()),
);
}
previous
};
// Dropped once unlocked: what it captured may call back in.
drop(previous);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Sequence)]
pub enum NapiVersion {
V10,
Expand Down
7 changes: 7 additions & 0 deletions src/napi_bridge_init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ extern "C" int snapi_bridge_init() {
return 1;
}

// Host-only: the function to call, from any thread, after V8 queues a
// foreground task that only an env's next event-loop checkpoint runs
// (EdgeV8Platform::SetForegroundTaskWaker). NULL clears it.
extern "C" void snapi_bridge_set_foreground_task_waker(void (*waker)()) {
EdgeV8Platform::SetForegroundTaskWaker(waker);
}

// ============================================================
// Value creation
// ============================================================
Expand Down
3 changes: 3 additions & 0 deletions src/snapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct SnapiUnofficialHeapCodeStatistics {
// through `snapi_js::host_js_*` entry points or direct guest memory access.
unsafe extern "C" {
pub fn snapi_bridge_init() -> i32;
// Host only: the JS backend has no V8 platform queue to wake for.
#[cfg(not(all(target_arch = "wasm32", feature = "js")))]
pub fn snapi_bridge_set_foreground_task_waker(waker: Option<extern "C" fn()>);
pub fn snapi_bridge_set_v8_worker_thread_count(count: u32) -> i32;
pub fn snapi_bridge_unofficial_configure_runtime(
engine_flags: *const c_char,
Expand Down
18 changes: 18 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,21 @@ if(_napi_test_engine STREQUAL "v8")
set_tests_properties(napi_v8.background_job_limit napi_v8.background_job_limit_single_worker
PROPERTIES TIMEOUT 120)
endif()

if(_napi_test_engine STREQUAL "v8")
add_executable(napi_v8_foreground_task_waker_test src/v8_foreground_task_waker_test.cc)
target_include_directories(napi_v8_foreground_task_waker_test PRIVATE
"${NAPI_INCLUDE_ROOT}"
"${${_napi_engine_include_var}}"
)
target_link_libraries(napi_v8_foreground_task_waker_test PRIVATE
${_napi_test_engine_target}
"${${_napi_engine_library_var}}"
)
if(NAPI_V8_DEFINES)
target_compile_definitions(napi_v8_foreground_task_waker_test PRIVATE ${NAPI_V8_DEFINES})
endif()
add_test(NAME napi_v8.foreground_task_waker
COMMAND napi_v8_foreground_task_waker_test)
set_tests_properties(napi_v8.foreground_task_waker PROPERTIES TIMEOUT 120)
endif()
230 changes: 230 additions & 0 deletions tests/foreground_task_waker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
//! `wasmer_napi::set_foreground_task_waker` reaches the V8 platform through
//! the bridge. A guest's `Atomics.waitAsync`, woken by another guest's
//! `Atomics.notify` on another thread, resolves only at the waiting guest's
//! next checkpoint, so the host's waker must run, on the notifying thread,
//! when the notify queues that task. The platform side is covered by
//! `tests/src/v8_foreground_task_waker_test.cc`.
//!
//! The waker is process-wide, so this test needs a process to itself, which
//! is what a separate integration test binary with one test gives it.

#![cfg(not(target_arch = "wasm32"))]

use std::{
mem::ManuallyDrop,
sync::{Arc, Mutex, mpsc},
thread::{self, ThreadId},
time::Duration,
};

use wasmer_napi::NapiCtx;

mod wat_guest;
use wat_guest::{Guest, compile};

const TIMEOUT: Duration = Duration::from_secs(10);
/// More checkpoints than an idle env needs to run what V8 queued for it.
const MAX_CHECKPOINTS: usize = 64;

/// The threads the installed waker ran on, in order.
type WakerLog = Arc<Mutex<Vec<ThreadId>>>;

fn calls(log: &WakerLog) -> usize {
log.lock().unwrap().len()
}

fn calls_on(log: &WakerLog, thread: ThreadId) -> usize {
log.lock()
.unwrap()
.iter()
.filter(|&&id| id == thread)
.count()
}

/// Starts an `Atomics.waitAsync` on the shared cell; `woken` becomes 1 once
/// its promise resolves.
fn wait_async(page: &mut Guest) {
assert_eq!(
page.eval(
"globalThis.woken = 0; \
Atomics.waitAsync(new Int32Array(shared), 0, 0).value.then(() => { woken = 1; });"
),
0
);
}

/// Runs checkpoints until the waitAsync promise has resolved.
fn checkpoint_until_woken(page: &mut Guest) -> bool {
for _ in 0..MAX_CHECKPOINTS {
assert_eq!(page.checkpoint(), 0);
if page.number("woken") == 1 {
return true;
}
}
false
}

/// Records a waker call like the installed wakers do, and panics when
/// dropped.
struct PanicsOnDrop(WakerLog);

impl PanicsOnDrop {
fn record(&self) {
self.0.lock().unwrap().push(thread::current().id());
}
}

impl Drop for PanicsOnDrop {
fn drop(&mut self) {
panic!("a waker's capture that panics when dropped");
}
}

/// What the notifying guest saw: how many waiters `Atomics.notify` woke, and
/// how many times the waker ran on its thread meanwhile.
struct Notified {
woke: i32,
waker_calls: usize,
}

#[test]
fn waker_runs_when_another_thread_queues_a_task_for_a_waiting_guest() {
let (engine, module) = compile();
let ctx = NapiCtx::default();
let (signal_tx, _signal_rx) = mpsc::channel();
let log: WakerLog = Arc::default();

// Dropped only once the test has passed. A regression can leave the
// notifying guest stuck inside V8 (a waker that deadlocks, say), and
// dropping this guest while the test fails would then wait on it forever.
let mut page = ManuallyDrop::new(Guest::new(&ctx, &engine, &module, signal_tx.clone()));
page.create();
assert_eq!(
page.eval("globalThis.shared = new SharedArrayBuffer(8); shared"),
0
);
assert_eq!(page.message(), 0);
let token = page.token();

// A guest on another thread that notifies the shared cell on request.
let (notify_tx, notify_rx) = mpsc::channel::<()>();
let (notified_tx, notified_rx) = mpsc::channel::<Notified>();
let notifier = {
let (ctx, engine, module, log) = (ctx.clone(), engine.clone(), module.clone(), log.clone());
thread::spawn(move || {
let mut guest = Guest::new(&ctx, &engine, &module, signal_tx);
guest.create();
assert_eq!(guest.take(token), 0);
assert_eq!(guest.install(), 0);
let me = thread::current().id();
while notify_rx.recv().is_ok() {
let before = calls_on(&log, me);
let woke = guest.number("Atomics.notify(new Int32Array(shared), 0)");
let waker_calls = calls_on(&log, me) - before;
if notified_tx.send(Notified { woke, waker_calls }).is_err() {
break;
}
}
assert_eq!(guest.release(), 0);
})
};
let notify = || {
notify_tx.send(()).expect("the notifying guest is gone");
notified_rx
.recv_timeout(TIMEOUT)
.expect("the notifying guest did not answer")
};

// Run what V8 queued while the env started, before the waker is set.
for _ in 0..MAX_CHECKPOINTS {
assert_eq!(page.checkpoint(), 0);
}

{
let log = log.clone();
wasmer_napi::set_foreground_task_waker(Some(Arc::new(move || {
log.lock().unwrap().push(thread::current().id());
})));
}
wait_async(&mut page);
let notified = notify();
assert_eq!(notified.woke, 1, "the waitAsync was not waiting");
assert!(
notified.waker_calls >= 1,
"the waker did not run on the notifying thread when the notify queued \
the waiting guest's task"
);
// Only a checkpoint runs the task that resolves the promise.
assert_eq!(page.number("woken"), 0);
assert!(
checkpoint_until_woken(&mut page),
"the waitAsync promise did not resolve"
);

// Without a waker nothing is called, and a checkpoint still runs the task.
wasmer_napi::set_foreground_task_waker(None);
let before = calls(&log);
wait_async(&mut page);
assert_eq!(notify().woke, 1);
assert_eq!(calls(&log), before, "a removed waker ran");
assert!(
checkpoint_until_woken(&mut page),
"the waitAsync promise did not resolve without a waker"
);

// A waker that panics: the panic stays out of V8, and the task still runs.
{
let log = log.clone();
wasmer_napi::set_foreground_task_waker(Some(Arc::new(move || {
log.lock().unwrap().push(thread::current().id());
panic!("a waker that panics");
})));
}
wait_async(&mut page);
let notified = notify();
assert_eq!(notified.woke, 1);
assert!(notified.waker_calls >= 1, "the panicking waker did not run");
assert!(
checkpoint_until_woken(&mut page),
"the waitAsync promise did not resolve after the waker panicked"
);

// A waker that removes itself and then panics, holding a capture that
// panics when dropped. It runs with no lock held, so it may call the
// setter. Once removed, the trampoline holds the last reference to it,
// and drops it after the call: if that drop came while the waker's panic
// unwound, the capture's panic would abort the process. Whichever thread
// calls it first removes it, so its calls are counted on every thread.
let before = calls(&log);
{
let capture = PanicsOnDrop(log.clone());
wasmer_napi::set_foreground_task_waker(Some(Arc::new(move || {
capture.record();
wasmer_napi::set_foreground_task_waker(None);
panic!("a waker that removed itself");
})));
}
wait_async(&mut page);
assert_eq!(notify().woke, 1);
assert!(
calls(&log) > before,
"the waker that removes itself did not run"
);
assert!(
checkpoint_until_woken(&mut page),
"the waitAsync promise did not resolve after the waker removed itself"
);
let before = calls(&log);
wait_async(&mut page);
assert_eq!(notify().woke, 1);
assert_eq!(calls(&log), before, "the waker did not remove itself");
assert!(
checkpoint_until_woken(&mut page),
"the waitAsync promise did not resolve once the waker removed itself"
);

drop(notify_tx);
notifier.join().expect("the notifying guest panicked");
assert_eq!(page.release(), 0);
drop(ManuallyDrop::into_inner(page));
}
Loading
Loading