From 4be8403d50de50f0be12263d95cd436d401f8345 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sat, 10 Oct 2026 16:37:07 +0000 Subject: [PATCH] Worker: split create into create (validate, allocate) and start (spawn) Worker::create, WorkerMessagingProxy and WebWorker__create validated the options and spawned the thread in one step. Split each into a create step that validates and allocates, and a start step that spawns. The serialized workerData, its ports and the shared env store become arguments of start. The constructor calls create and start back to back, where it called create. No behaviour change. --- src/jsc/bindings/webcore/JSWorker.cpp | 26 ++-- src/jsc/bindings/webcore/Worker.cpp | 11 +- src/jsc/bindings/webcore/Worker.h | 2 + .../bindings/webcore/WorkerMessagingProxy.cpp | 28 ++++- .../bindings/webcore/WorkerMessagingProxy.h | 9 +- src/jsc/web_worker.rs | 119 ++++++++++-------- 6 files changed, 123 insertions(+), 72 deletions(-) diff --git a/src/jsc/bindings/webcore/JSWorker.cpp b/src/jsc/bindings/webcore/JSWorker.cpp index 1b3938ec8d26..29cc7e70b7f2 100644 --- a/src/jsc/bindings/webcore/JSWorker.cpp +++ b/src/jsc/bindings/webcore/JSWorker.cpp @@ -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 sharedEnvStore; if (shareEnv) { - options.sharedEnvStore = Bun::ensureSharedEnvStoreForWorker(globalObject); + sharedEnvStore = Bun::ensureSharedEnvStoreForWorker(globalObject); RETURN_IF_EXCEPTION(throwScope, {}); } @@ -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) - RETURN_IF_EXCEPTION(throwScope, {}); - static_assert(TypeOrExceptionOrUnderlyingType::isRef); - auto jsValue = toJSNewlyCreated>(*lexicalGlobalObject, *castedThis->globalObject(), throwScope, WTF::move(object)); - if constexpr (IsExceptionOr) - 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>(*lexicalGlobalObject, *castedThis->globalObject(), WTF::move(worker)); setSubclassStructureIfNeeded(lexicalGlobalObject, callFrame, asObject(jsValue)); RETURN_IF_EXCEPTION(throwScope, {}); diff --git a/src/jsc/bindings/webcore/Worker.cpp b/src/jsc/bindings/webcore/Worker.cpp index 15b0b9ff83e8..3de02b739e7f 100644 --- a/src/jsc/bindings/webcore/Worker.cpp +++ b/src/jsc/bindings/webcore/Worker.cpp @@ -80,12 +80,17 @@ ExceptionOr> 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 Worker::start(Ref&& workerDataAndEnvironmentData, Vector&& dataMessagePorts, RefPtr&& sharedEnvStore) +{ + return m_contextProxy->startWorkerGlobalScope(WTF::move(workerDataAndEnvironmentData), WTF::move(dataMessagePorts), WTF::move(sharedEnvStore)); +} + Worker::~Worker() { m_contextProxy->workerObjectDestroyed(); diff --git a/src/jsc/bindings/webcore/Worker.h b/src/jsc/bindings/webcore/Worker.h index eaf0f3fcd957..61c699883f33 100644 --- a/src/jsc/bindings/webcore/Worker.h +++ b/src/jsc/bindings/webcore/Worker.h @@ -48,7 +48,9 @@ class Worker final : public RefCounted, public EventTargetWithInlineData WTF_MAKE_TZONE_ALLOCATED(Worker); public: + // Validates the options and allocates the worker. start() spawns its thread. static ExceptionOr> create(ScriptExecutionContext&, const String& url, WorkerOptions&&); + ExceptionOr start(Ref&& workerDataAndEnvironmentData, Vector&& dataMessagePorts, RefPtr&&); ~Worker(); // ActiveDOMObject. diff --git a/src/jsc/bindings/webcore/WorkerMessagingProxy.cpp b/src/jsc/bindings/webcore/WorkerMessagingProxy.cpp index fee6c0d4f0b6..236b5bc7b5ea 100644 --- a/src/jsc/bindings/webcore/WorkerMessagingProxy.cpp +++ b/src/jsc/bindings/webcore/WorkerMessagingProxy.cpp @@ -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, @@ -60,7 +59,6 @@ void* WebWorker__create( uint32_t parentContextId, uint32_t contextId, bool miniMode, - bool unrefByDefault, bool evalMode, bool isNodeWorker, StringImpl** argvPtr, @@ -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. @@ -109,7 +109,7 @@ WorkerMessagingProxy::~WorkerMessagingProxy() // ---- WorkerGlobalScopeProxy (parent thread) ------------------------------------------------------ -ExceptionOr WorkerMessagingProxy::startWorkerGlobalScope(const String& scriptURL) +ExceptionOr WorkerMessagingProxy::prepareWorkerGlobalScope(const String& scriptURL) { ASSERT(m_scriptExecutionContext && m_scriptExecutionContext->isContextThread()); ASSERT(!m_workerThread); @@ -157,7 +157,6 @@ ExceptionOr 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(m_options.argv.begin()), @@ -177,6 +176,23 @@ ExceptionOr WorkerMessagingProxy::startWorkerGlobalScope(const String& scr return {}; } +ExceptionOr WorkerMessagingProxy::startWorkerGlobalScope(Ref&& workerDataAndEnvironmentData, Vector&& dataMessagePorts, RefPtr&& 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) @@ -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(); } diff --git a/src/jsc/bindings/webcore/WorkerMessagingProxy.h b/src/jsc/bindings/webcore/WorkerMessagingProxy.h index 273a0f129f95..155efa6a1df2 100644 --- a/src/jsc/bindings/webcore/WorkerMessagingProxy.h +++ b/src/jsc/bindings/webcore/WorkerMessagingProxy.h @@ -69,7 +69,7 @@ class WorkerMessagingProxy final : public ThreadSafeRefCounted startWorkerGlobalScope(const String& scriptURL); + // Validates the options and allocates the thread object. startWorkerGlobalScope() spawns the thread. + ExceptionOr prepareWorkerGlobalScope(const String& scriptURL); + // Cannot reject an option: what it reads was validated above. Fails only if the OS refuses the thread. + ExceptionOr startWorkerGlobalScope(Ref&& workerDataAndEnvironmentData, Vector&& dataMessagePorts, RefPtr&&); void terminateWorkerGlobalScope(); void postMessageToWorkerGlobalScope(MessageWithMessagePorts&&); // Queued while Pending, posted while Running, refused (false) once Closing. @@ -143,7 +146,7 @@ class WorkerMessagingProxy final : public ThreadSafeRefCounted m_state { State::Pending }; diff --git a/src/jsc/web_worker.rs b/src/jsc/web_worker.rs index af673c56f55b..7c247f9fca90 100644 --- a/src/jsc/web_worker.rs +++ b/src/jsc/web_worker.rs @@ -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 @@ -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, unresolved_specifier: Box<[u8]>, preloads: Vec>, name: bun_core::ZBox, @@ -97,7 +99,7 @@ pub struct WebWorker { vm_handle: bun_threading::Guarded>, // ---- 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, /// Taken by the parent to join the OS thread. @@ -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 { @@ -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] @@ -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( @@ -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, @@ -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; @@ -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, @@ -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() { @@ -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 }`. @@ -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. @@ -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 } } }