Skip to content
Closed
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
26 changes: 15 additions & 11 deletions src/jsc/bindings/webcore/JSWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,9 @@ template<> __attribute__((minsize)) JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES
// Resolve the spawning thread's env tree (founding one if needed) so disjoint
// SHARE_ENV chains stay isolated. Runs only after every option validated,
// because founding a tree swaps this thread's process.env.
RefPtr<Bun::SharedEnvStore> sharedEnvStore;
if (shareEnv) {
options.sharedEnvStore = Bun::ensureSharedEnvStoreForWorker(globalObject);
sharedEnvStore = Bun::ensureSharedEnvStoreForWorker(globalObject);
RETURN_IF_EXCEPTION(throwScope, {});
}

Expand Down Expand Up @@ -392,16 +393,19 @@ template<> __attribute__((minsize)) JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES
transferredPorts = disentangleResult.releaseReturnValue();
}

options.workerDataAndEnvironmentData = serialized.releaseReturnValue();
options.dataMessagePorts = WTF::move(transferredPorts);

auto object = Worker::create(*context, WTF::move(scriptUrl), WTF::move(options));
if constexpr (IsExceptionOr<decltype(object)>)
RETURN_IF_EXCEPTION(throwScope, {});
static_assert(TypeOrExceptionOrUnderlyingType<decltype(object)>::isRef);
auto jsValue = toJSNewlyCreated<IDLInterface<Worker>>(*lexicalGlobalObject, *castedThis->globalObject(), throwScope, WTF::move(object));
if constexpr (IsExceptionOr<decltype(object)>)
RETURN_IF_EXCEPTION(throwScope, {});
auto created = Worker::create(*context, WTF::move(scriptUrl), WTF::move(options));
if (created.hasException()) [[unlikely]] {
WebCore::propagateException(*lexicalGlobalObject, throwScope, created.releaseException());
RELEASE_AND_RETURN(throwScope, {});
}
Ref worker = created.releaseReturnValue();
auto started = worker->start(serialized.releaseReturnValue(), WTF::move(transferredPorts), WTF::move(sharedEnvStore));
RETURN_IF_EXCEPTION(throwScope, {});
if (started.hasException()) [[unlikely]] {
WebCore::propagateException(*lexicalGlobalObject, throwScope, started.releaseException());
RELEASE_AND_RETURN(throwScope, {});
}
auto jsValue = toJSNewlyCreated<IDLInterface<Worker>>(*lexicalGlobalObject, *castedThis->globalObject(), WTF::move(worker));

setSubclassStructureIfNeeded<Worker>(lexicalGlobalObject, callFrame, asObject(jsValue));
RETURN_IF_EXCEPTION(throwScope, {});
Expand Down
11 changes: 8 additions & 3 deletions src/jsc/bindings/webcore/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,17 @@ ExceptionOr<Ref<Worker>> Worker::create(ScriptExecutionContext& context, const S
auto worker = adoptRef(*new Worker(context, WTF::move(options)));
worker->suspendIfNeeded();

auto started = worker->m_contextProxy->startWorkerGlobalScope(url);
if (started.hasException())
return started.releaseException();
auto prepared = worker->m_contextProxy->prepareWorkerGlobalScope(url);
if (prepared.hasException())
return prepared.releaseException();
return worker;
}

ExceptionOr<void> Worker::start(Ref<SerializedScriptValue>&& workerDataAndEnvironmentData, Vector<TransferredMessagePort>&& dataMessagePorts, RefPtr<Bun::SharedEnvStore>&& sharedEnvStore)
{
return m_contextProxy->startWorkerGlobalScope(WTF::move(workerDataAndEnvironmentData), WTF::move(dataMessagePorts), WTF::move(sharedEnvStore));
}

Worker::~Worker()
{
m_contextProxy->workerObjectDestroyed();
Expand Down
2 changes: 2 additions & 0 deletions src/jsc/bindings/webcore/Worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class Worker final : public RefCounted<Worker>, public EventTargetWithInlineData
WTF_MAKE_TZONE_ALLOCATED(Worker);

public:
// Validates the options and allocates the worker. start() spawns its thread.
static ExceptionOr<Ref<Worker>> create(ScriptExecutionContext&, const String& url, WorkerOptions&&);
ExceptionOr<void> start(Ref<SerializedScriptValue>&& workerDataAndEnvironmentData, Vector<TransferredMessagePort>&& dataMessagePorts, RefPtr<Bun::SharedEnvStore>&&);
~Worker();

// ActiveDOMObject.
Expand Down
28 changes: 22 additions & 6 deletions src/jsc/bindings/webcore/WorkerMessagingProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ WTF_MAKE_TZONE_ALLOCATED_IMPL(WorkerMessagingProxy);
// ---- The native thread object (src/jsc/web_worker.rs) --------------------------------------------
extern "C" {

// Allocates the thread object holding one ref for the caller, takes the keep-alive on the parent
// event loop, and spawns the thread. Null (with errorMessage set) if nothing was started.
// Validates the options and allocates the thread object holding one ref for the caller. Null (with errorMessage set) otherwise.
void* WebWorker__create(
WorkerMessagingProxy*,
void* parentVM,
Expand All @@ -60,7 +59,6 @@ void* WebWorker__create(
uint32_t parentContextId,
uint32_t contextId,
bool miniMode,
bool unrefByDefault,
bool evalMode,
bool isNodeWorker,
StringImpl** argvPtr,
Expand All @@ -70,6 +68,8 @@ void* WebWorker__create(
size_t execArgvLen,
BunString* preloadModulesPtr,
size_t preloadModulesLen);
// Takes the keep-alive on the parent event loop and spawns the thread. False if the OS refused it.
bool WebWorker__start(void*, bool unrefByDefault);
// Raise a TerminationException in the worker VM at its next safepoint and wake its loop. Any thread.
void WebWorker__requestTermination(void*);
// Toggle the keep-alive this worker holds on the parent event loop. Parent thread.
Expand Down Expand Up @@ -109,7 +109,7 @@ WorkerMessagingProxy::~WorkerMessagingProxy()

// ---- WorkerGlobalScopeProxy (parent thread) ------------------------------------------------------

ExceptionOr<void> WorkerMessagingProxy::startWorkerGlobalScope(const String& scriptURL)
ExceptionOr<void> WorkerMessagingProxy::prepareWorkerGlobalScope(const String& scriptURL)
{
ASSERT(m_scriptExecutionContext && m_scriptExecutionContext->isContextThread());
ASSERT(!m_workerThread);
Expand Down Expand Up @@ -157,7 +157,6 @@ ExceptionOr<void> WorkerMessagingProxy::startWorkerGlobalScope(const String& scr
m_loaderContextIdentifier,
m_workerContextIdentifier,
m_options.mini,
m_options.unref,
m_options.evalMode,
m_options.kind == WorkerOptions::Kind::Node,
reinterpret_cast<WTF::StringImpl**>(m_options.argv.begin()),
Expand All @@ -177,6 +176,23 @@ ExceptionOr<void> WorkerMessagingProxy::startWorkerGlobalScope(const String& scr
return {};
}

ExceptionOr<void> WorkerMessagingProxy::startWorkerGlobalScope(Ref<SerializedScriptValue>&& workerDataAndEnvironmentData, Vector<TransferredMessagePort>&& dataMessagePorts, RefPtr<Bun::SharedEnvStore>&& sharedEnvStore)
{
ASSERT(!m_scriptExecutionContext || m_scriptExecutionContext->isContextThread());
m_options.workerDataAndEnvironmentData = WTF::move(workerDataAndEnvironmentData);
m_options.dataMessagePorts = WTF::move(dataMessagePorts);
m_options.sharedEnvStore = WTF::move(sharedEnvStore);

// Stopped at birth: prepareWorkerGlobalScope() made no thread object.
if (!m_workerThread)
return {};
if (!WebWorker__start(m_workerThread, m_options.unref)) {
releaseWorkerThread();
return Exception { TypeError, "Failed to spawn worker thread"_s };
}
return {};
}

void WorkerMessagingProxy::terminateWorkerGlobalScope()
{
if (m_askedToTerminate)
Expand Down Expand Up @@ -539,7 +555,7 @@ void WorkerMessagingProxy::releaseWorkerThread()
WebWorker__join(workerThread);
WebWorker__deref(workerThread);
m_state.store(State::Closed);
// The ref startWorkerGlobalScope() took on behalf of the thread.
// The ref prepareWorkerGlobalScope() took on behalf of the thread.
deref();
}

Expand Down
9 changes: 6 additions & 3 deletions src/jsc/bindings/webcore/WorkerMessagingProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class WorkerMessagingProxy final : public ThreadSafeRefCounted<WorkerMessagingPr

public:
enum class State : uint8_t {
Pending, // created; worker thread starting up
Pending, // created; the worker thread is not spawned yet, or is starting up
Running, // workerGlobalScopeStarted() has run on the worker thread
Closing, // workerGlobalScopeDestroyedInternal() is dispatching 'close' on the parent
Closed, // the thread is joined and released; nothing further will happen
Expand All @@ -79,7 +79,10 @@ class WorkerMessagingProxy final : public ThreadSafeRefCounted<WorkerMessagingPr
~WorkerMessagingProxy();

// -- WorkerGlobalScopeProxy (parent thread) --------------------------------------------------
ExceptionOr<void> startWorkerGlobalScope(const String& scriptURL);
// Validates the options and allocates the thread object. startWorkerGlobalScope() spawns the thread.
ExceptionOr<void> prepareWorkerGlobalScope(const String& scriptURL);
// Cannot reject an option: what it reads was validated above. Fails only if the OS refuses the thread.
ExceptionOr<void> startWorkerGlobalScope(Ref<SerializedScriptValue>&& workerDataAndEnvironmentData, Vector<TransferredMessagePort>&& dataMessagePorts, RefPtr<Bun::SharedEnvStore>&&);
void terminateWorkerGlobalScope();
void postMessageToWorkerGlobalScope(MessageWithMessagePorts&&);
// Queued while Pending, posted while Running, refused (false) once Closing.
Expand Down Expand Up @@ -143,7 +146,7 @@ class WorkerMessagingProxy final : public ThreadSafeRefCounted<WorkerMessagingPr
WorkerOptions m_options;

// The native thread object (src/jsc/web_worker.rs). Holds one ref on it from
// startWorkerGlobalScope() until releaseWorkerThread().
// prepareWorkerGlobalScope() until releaseWorkerThread().
void* m_workerThread { nullptr };

std::atomic<State> m_state { State::Pending };
Expand Down
119 changes: 70 additions & 49 deletions src/jsc/web_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//!
//! Threads: everything the worker thread needs from its parent VM (transform
//! options, an env snapshot, the standalone graph) is copied on the parent
//! thread in `create()`, and the thread holds a `Ticket` on the parent for its
//! thread in `start()`, and the thread holds a `Ticket` on the parent for its
//! whole life, so the parent cannot be destroyed under it. The parent (or an
//! exiting ancestor) reaches the worker's VM only through `vm_handle` — the
//! worker VM's uncounted handle, published once the VM exists — never through
Expand Down Expand Up @@ -81,6 +81,8 @@ pub struct WebWorker {
exec_argv_ptr: *const WTFStringImpl,
exec_argv_len: usize,
inherit_exec_argv: bool,
/// `None` when the worker inherits the parent's execArgv.
exec_argv_flags: Option<ExecArgvFlags>,
unresolved_specifier: Box<[u8]>,
preloads: Vec<Box<[u8]>>,
name: bun_core::ZBox,
Expand All @@ -97,7 +99,7 @@ pub struct WebWorker {
vm_handle: bun_threading::Guarded<Option<crate::VmHandle>>,

// ---- Parent-thread only ---------------------------------------------------
/// Keep-alive on the parent's event loop: taken in `create()`, toggled by
/// Keep-alive on the parent's event loop: taken in `start()`, toggled by
/// `.ref()`/`.unref()`, released when the parent releases the thread.
parent_poll_ref: JsCell<KeepAlive>,
/// Taken by the parent to join the OS thread.
Expand All @@ -123,6 +125,12 @@ pub struct WebWorker {
terminated_by_parent: AtomicBool,
}

#[derive(Copy, Clone)]
struct ExecArgvFlags {
allow_addons: bool,
allow_ffi_cc: bool,
}

/// Copied from the parent VM on its thread at `new Worker()`; consumed by
/// `start_vm()` on the worker thread.
struct WorkerVmInit {
Expand Down Expand Up @@ -229,7 +237,7 @@ impl WebWorker {

/// Closure-scoped `&mut KeepAlive` accessor for `parent_poll_ref`. The cell
/// is touched only on the parent thread (`set_ref`,
/// `release_parent_poll_ref`, `create`) so no lock is required; `JsCell`
/// `release_parent_poll_ref`, `start`) so no lock is required; `JsCell`
/// provides the interior mutability because `WebWorker` is shared `&self`
/// across threads.
#[inline]
Expand Down Expand Up @@ -274,9 +282,7 @@ impl WebWorker {
// Construction (parent thread)
// =========================================================================

/// Allocate the thread object (one ref, owned by the calling proxy), take a
/// keep-alive on the parent event loop, register as a child of the parent VM,
/// and spawn the thread. On any failure returns null with `error_message`
/// Validate the options and allocate the thread object (one ref, the proxy's). On failure: null, `error_message`
/// set and nothing to clean up.
#[unsafe(export_name = "WebWorker__create")]
pub(crate) unsafe extern "C" fn create(
Expand All @@ -289,7 +295,6 @@ impl WebWorker {
_parent_context_id: u32,
this_context_id: u32,
mini: bool,
default_unref: bool,
eval_mode: bool,
is_node_worker: bool,
argv_ptr: *const WTFStringImpl,
Expand Down Expand Up @@ -347,11 +352,7 @@ impl WebWorker {
}
}

// Everything the worker thread needs from this VM is copied here, on
// its own thread; the worker never dereferences `parent`.
// SAFETY: `parent` is the calling thread's live VM.
let parent_ref = unsafe { &*parent };
let mut transform_options = (*parent_ref.transpiler.options.transform_options).clone();
let mut exec_argv_flags = None;
if !inherit_exec_argv {
let hooks = runtime_hooks().expect("RuntimeHooks not installed");
// SAFETY: caller passed valid (ptr,len) borrowed from C++ WorkerOptions;
Expand All @@ -373,36 +374,16 @@ impl WebWorker {
*error_is_invalid_exec_argv = true;
return core::ptr::null_mut();
}
let parent_allows_addons = transform_options.allow_addons.unwrap_or(true);
transform_options.allow_addons = Some(parent_allows_addons && flags.allow_addons);
let parent_allows_ffi_cc = transform_options.allow_ffi_cc.unwrap_or(true);
transform_options.allow_ffi_cc = Some(parent_allows_ffi_cc && flags.allow_ffi_cc);
exec_argv_flags = Some(ExecArgvFlags {
allow_addons: flags.allow_addons,
allow_ffi_cc: flags.allow_ffi_cc,
});
}
}
// The worker's `process.env` starts as a copy of the parent's now (as in
// Node). Proxy-env values may be RefCountedEnvValue bytes owned by the
// parent's proxy_env_storage: snapshot slots + map under its lock so
// every slice copied is backed by a ref the snapshot holds.
let mut proxy_env_slots = jsc::rare_data::ProxyEnvSlots::default();
let mut env_loader = {
let parent_slots = parent_ref.proxy_env_storage.lock();
proxy_env_slots.clone_from(&parent_slots);
match parent_ref.env_loader().clone_for_worker() {
Ok(loader) => loader,
Err(_) => {
*error_message = BunString::static_("Out of memory");
return core::ptr::null_mut();
}
}
};
proxy_env_slots.sync_into(&mut env_loader.map);
let init = WorkerVmInit {
transform_options,
env_loader,
proxy_env_slots,
};

// The construction ref: handed to C++ on success, dropped on failure.
// SAFETY: `parent` is the calling thread's live VM.
let parent_ref = unsafe { &*parent };
// The construction ref, handed to C++.
let worker = bun_ptr::RefPtr::new(WebWorker {
messaging_proxy: proxy,
parent,
Expand All @@ -420,6 +401,7 @@ impl WebWorker {
exec_argv_ptr,
exec_argv_len,
inherit_exec_argv,
exec_argv_flags,
unresolved_specifier: spec_slice.slice().to_vec().into_boxed_slice(),
preloads,
name: if name_str.is_empty() {
Expand All @@ -438,8 +420,50 @@ impl WebWorker {
worker_env_loader: Cell::new(core::ptr::null_mut()),
exit_called: AtomicBool::new(false),
terminated_by_parent: AtomicBool::new(false),
});
let worker_ref = bun_ptr::ParentRef::from(worker.as_non_null());
})
.into_raw();
// SAFETY: `parent` is the calling thread's VM; parent-thread-only list.
unsafe { (*parent).child_workers.push(worker) };
worker
}

/// Spawn the thread of a worker `create()` made, once, on the same thread. False if the OS refused it.
#[unsafe(export_name = "WebWorker__start")]
pub(crate) unsafe extern "C" fn start(this: *mut WebWorker, default_unref: bool) -> bool {
let worker_ref = bun_ptr::ParentRef::from(NonNull::new(this).expect("WebWorker FFI ptr"));
debug_assert!(
worker_ref.join_handle.with_mut(|h| h.is_none()),
"started twice"
);
log!("[{}] start", worker_ref.execution_context_id);

// Everything the worker thread needs from this VM is copied here, on
// its own thread; the worker never dereferences `parent`.
// SAFETY: `parent` is the calling thread's live VM.
let parent_ref = unsafe { &*worker_ref.parent };
let mut transform_options = (*parent_ref.transpiler.options.transform_options).clone();
if let Some(flags) = worker_ref.exec_argv_flags {
let parent_allows_addons = transform_options.allow_addons.unwrap_or(true);
transform_options.allow_addons = Some(parent_allows_addons && flags.allow_addons);
let parent_allows_ffi_cc = transform_options.allow_ffi_cc.unwrap_or(true);
transform_options.allow_ffi_cc = Some(parent_allows_ffi_cc && flags.allow_ffi_cc);
}
// The worker's `process.env` starts as a copy of the parent's now (as in
// Node). Proxy-env values may be RefCountedEnvValue bytes owned by the
// parent's proxy_env_storage: snapshot slots + map under its lock so
// every slice copied is backed by a ref the snapshot holds.
let mut proxy_env_slots = jsc::rare_data::ProxyEnvSlots::default();
let mut env_loader = {
let parent_slots = parent_ref.proxy_env_storage.lock();
proxy_env_slots.clone_from(&parent_slots);
bun_core::handle_oom(parent_ref.env_loader().clone_for_worker())
};
proxy_env_slots.sync_into(&mut env_loader.map);
let init = WorkerVmInit {
transform_options,
env_loader,
proxy_env_slots,
};

// Keep the parent's event loop alive until the parent releases this
// thread, unless the user opted out with `{ ref: false }`.
Expand All @@ -449,7 +473,8 @@ impl WebWorker {
}

// The thread's own ref, taken before it exists so it can never observe zero.
let thread_ref = worker.clone();
// SAFETY: fn contract. `this` is live.
let thread_ref = unsafe { bun_ptr::RefPtr::init_ref(this) };
// The thread is something of this VM's on another thread for as long as
// it runs: the parent joins it before its own teardown's wait, which
// this ticket would otherwise hold.
Expand Down Expand Up @@ -483,16 +508,12 @@ impl WebWorker {
match spawn {
Ok(handle) => {
worker_ref.join_handle.set(Some(handle));
let worker = worker.into_raw();
// SAFETY: `parent` is the calling thread's VM; parent-thread-only list.
unsafe { (*parent).child_workers.push(worker) };
worker
true
}
Err(_) => {
// The thread's ref went down with the closure; ours drops on return.
// The thread's ref went down with the closure.
worker_ref.with_parent_poll_ref(|p| p.unref(bun_io::js_vm_ctx()));
*error_message = BunString::static_("Failed to spawn worker thread");
core::ptr::null_mut()
false
}
}
}
Expand Down
Loading