diff --git a/include/JobQueue.hh b/include/JobQueue.hh index a83089dd..83caa692 100644 --- a/include/JobQueue.hh +++ b/include/JobQueue.hh @@ -129,6 +129,25 @@ js::UniquePtr saveJobQueue(JSContext *) override; */ static bool dispatchToEventLoop(void *closure, js::UniquePtr &&dispatchable); +/** + * @brief Called on the JS thread when SpiderMonkey starts an off-thread task whose promise settles + * through dispatchToEventLoop (e.g. WebAssembly.compile); counts it as a pending event-loop job, + * so that pm.wait() waits for it + * + * @param closure - closure, currently the javascript context + * @param dispatchable - the task + */ +static void asyncTaskStarted(void *closure, JS::Dispatchable *dispatchable); + +/** + * @brief Called on the JS thread when a task counted by asyncTaskStarted has run or been cancelled; + * uncounts it + * + * @param closure - closure, currently the javascript context + * @param dispatchable - the task + */ +static void asyncTaskFinished(void *closure, JS::Dispatchable *dispatchable); + /** * @brief The callback for dispatching an off-thread promise to the event * loop after a delay. @@ -162,11 +181,11 @@ static void promiseRejectionTracker(JSContext *cx, bool mutedErrors, }; // class /** - * @brief Send job to the Python event-loop on main thread - * (Thread-Safe) - * @param pyFunc - the Python job function - * @return success + * @brief Send an off-thread task's Dispatchable to the Python event-loop on the main thread, or hand + * it back to SpiderMonkey when no event-loop is running. Runs on its own thread, started by + * JobQueue::dispatchToEventLoop. + * @param job - the DispatchJob (JSContext and Dispatchable); deleted here */ -bool sendJobToMainLoop(PyObject *pyFunc); +void sendJobToMainLoop(void *job); #endif \ No newline at end of file diff --git a/src/JobQueue.cc b/src/JobQueue.cc index 36ce7d85..433d5ec1 100644 --- a/src/JobQueue.cc +++ b/src/JobQueue.cc @@ -132,9 +132,7 @@ js::UniquePtr JobQueue::saveJobQueue(JSContext *cx) bool JobQueue::init(JSContext *cx) { JS::SetJobQueue(cx, this); - // Last two args (asyncTaskStarted/FinishedCallback) are optional; this - // embedding doesn't need to track background-task liveness. - JS::InitAsyncTaskCallbacks(cx, dispatchToEventLoop, delayedDispatchToEventLoop, nullptr, nullptr, cx); + JS::InitAsyncTaskCallbacks(cx, dispatchToEventLoop, delayedDispatchToEventLoop, asyncTaskStarted, asyncTaskFinished, cx); JS::SetPromiseRejectionTrackerCallback(cx, promiseRejectionTracker); return true; } @@ -155,24 +153,25 @@ static PyObject *callDispatchFunc(PyObject *dispatchFuncTuple, PyObject *Py_UNUS static PyMethodDef callDispatchFuncDef = {"JsDispatchCallable", callDispatchFunc, METH_NOARGS, NULL}; -bool JobQueue::dispatchToEventLoop(void *closure, js::UniquePtr &&dispatchable) { - JSContext *cx = (JSContext *)closure; - - // The `dispatchToEventLoop` function is running in a helper thread, so - // we must acquire the Python GIL (global interpreter lock) - // see https://docs.python.org/3/c-api/init.html#non-python-created-threads - PyGILState_STATE gstate = PyGILState_Ensure(); - - // Release ownership into a raw pointer to smuggle it through the Python - // closure; reclaimed by callDispatchFunc via Dispatchable::Run above. - JS::Dispatchable *raw = dispatchable.release(); - PyObject *dispatchFuncTuple = PyTuple_Pack(2, PyLong_FromVoidPtr(cx), PyLong_FromVoidPtr(raw)); - PyObject *pyFunc = PyCFunction_New(&callDispatchFuncDef, dispatchFuncTuple); - - // Avoid using the current, JS helper thread to send jobs to event-loop as it may cause deadlock - PyThread_start_new_thread((void (*)(void *)) &sendJobToMainLoop, pyFunc); +/** + * A Dispatchable on its way from a SpiderMonkey helper thread to the Python event-loop. + */ +struct DispatchJob { + JSContext *cx; + JS::Dispatchable *dispatchable; +}; - PyGILState_Release(gstate); +bool JobQueue::dispatchToEventLoop(void *closure, js::UniquePtr &&dispatchable) { + // SpiderMonkey calls this on a helper thread that holds the helper-thread lock, which the JS thread + // can be waiting for while it holds the GIL (e.g. when it starts another WebAssembly.compile). So this + // must not take the GIL: it starts a thread, without the GIL, that takes it and enqueues the job. + DispatchJob *job = new DispatchJob{(JSContext *)closure, dispatchable.release()}; + + if (PyThread_start_new_thread(sendJobToMainLoop, job) == PYTHREAD_INVALID_THREAD_ID) { + JS::Dispatchable::ReleaseFailedTask(js::UniquePtr(job->dispatchable)); + delete job; + return false; + } return true; } @@ -183,20 +182,53 @@ bool JobQueue::delayedDispatchToEventLoop(void *closure, js::UniquePtr(job->dispatchable)); + delete job; + return; } + + // Ownership travels through the Python closure as a raw pointer, reclaimed by callDispatchFunc + PyObject *cxArg = PyLong_FromVoidPtr(job->cx); + PyObject *dispatchableArg = PyLong_FromVoidPtr(job->dispatchable); + PyObject *dispatchFuncTuple = PyTuple_Pack(2, cxArg, dispatchableArg); + Py_DECREF(cxArg); + Py_DECREF(dispatchableArg); + PyObject *pyFunc = PyCFunction_New(&callDispatchFuncDef, dispatchFuncTuple); + Py_DECREF(dispatchFuncTuple); + delete job; + loop.enqueue(pyFunc); + Py_DECREF(pyFunc); loop._loop = nullptr; // the `Py_XDECREF` Python API call in `PyEventLoop`'s destructor will not be accessible once we hand over the GIL by `PyGILState_Release` PyGILState_Release(gstate); - return true; +} + +void JobQueue::asyncTaskStarted(void *closure, JS::Dispatchable *dispatchable) { + // Counted like a queued job, so that pm.wait() waits for the task (e.g. an off-thread + // WebAssembly.compile) instead of returning while it runs and leaving its promise unsettled + PyEventLoop::_locker->incCounter(); +} + +void JobQueue::asyncTaskFinished(void *closure, JS::Dispatchable *dispatchable) { + PyObject *type, *value, *traceback; + PyErr_Fetch(&type, &value, &traceback); // decCounter() calls into Python, which needs a clear error indicator + PyEventLoop::_locker->decCounter(); + PyErr_Restore(type, value, traceback); } void JobQueue::promiseRejectionTracker(JSContext *cx, diff --git a/tests/js/wasm-compile-alone.simple b/tests/js/wasm-compile-alone.simple new file mode 100644 index 00000000..f794c09e --- /dev/null +++ b/tests/js/wasm-compile-alone.simple @@ -0,0 +1,51 @@ +/** + * @file wasm-compile-alone.simple + * An off-thread WebAssembly.compile() with nothing else pending on the event-loop + * settles, and the program then exits: pm.wait() counts the compile as pending work. + * @author Wes Garland, wes@distributive.network + * @date Nov 2026 + * + * timeout: 10 + */ +'use strict'; + +/* A module of `count` functions, each `per` times (i32.const 0; drop): big enough to compile off-thread */ +function makeModule(count, per) +{ + function leb(n) + { + const out = []; + do + { + const b = n & 0x7f; + n >>>= 7; + out.push(n ? b | 0x80 : b); + } while (n); + return out; + } + function section(id, payload) + { + return [ id, ...leb(payload.length), ...payload ]; + } + + const body = [ 0 ]; + for (let i = 0; i < per; i++) + body.push(0x41, 0, 0x1a); + body.push(0x0b); + + const code = leb(count); + for (let i = 0; i < count; i++) + code.push(...leb(body.length), ...body); + + return new Uint8Array([ 0, 0x61, 0x73, 0x6d, 1, 0, 0, 0, + ...section(1, [ 1, 0x60, 0, 0 ]), + ...section(3, [ ...leb(count), ...new Array(count).fill(0) ]), + ...section(10, code) ]); +} + +const bytes = makeModule(2000, 30); +WebAssembly.compile(bytes).then((module) => { + if (!(module instanceof WebAssembly.Module)) + throw new Error('compile did not resolve with a WebAssembly.Module'); + console.log(`compiled ${bytes.length} bytes`); +}); diff --git a/tests/js/wasm-compile-concurrent.simple b/tests/js/wasm-compile-concurrent.simple new file mode 100644 index 00000000..4be1e73d --- /dev/null +++ b/tests/js/wasm-compile-concurrent.simple @@ -0,0 +1,52 @@ +/** + * @file wasm-compile-concurrent.simple + * Several WebAssembly.compile() calls in flight at once all settle: a compile that + * finishes on a helper thread while the JS thread starts another one does not deadlock. + * @author Wes Garland, wes@distributive.network + * @date Nov 2026 + * + * timeout: 10 + */ +'use strict'; + +/* A module of `count` functions, each `per` times (i32.const 0; drop): big enough to compile off-thread */ +function makeModule(count, per) +{ + function leb(n) + { + const out = []; + do + { + const b = n & 0x7f; + n >>>= 7; + out.push(n ? b | 0x80 : b); + } while (n); + return out; + } + function section(id, payload) + { + return [ id, ...leb(payload.length), ...payload ]; + } + + const body = [ 0 ]; + for (let i = 0; i < per; i++) + body.push(0x41, 0, 0x1a); + body.push(0x0b); + + const code = leb(count); + for (let i = 0; i < count; i++) + code.push(...leb(body.length), ...body); + + return new Uint8Array([ 0, 0x61, 0x73, 0x6d, 1, 0, 0, 0, + ...section(1, [ 1, 0x60, 0, 0 ]), + ...section(3, [ ...leb(count), ...new Array(count).fill(0) ]), + ...section(10, code) ]); +} + +const bytes = makeModule(2000, 30); +const tiny = new Uint8Array([ 0, 0x61, 0x73, 0x6d, 1, 0, 0, 0 ]); +Promise.all([ WebAssembly.compile(bytes), WebAssembly.compile(tiny), WebAssembly.compile(bytes) ]).then((modules) => { + if (!modules.every((module) => module instanceof WebAssembly.Module)) + throw new Error('a compile did not resolve with a WebAssembly.Module'); + console.log(`compiled ${modules.length} modules concurrently`); +});