fix(runtime_wasm, rive_native): load the build without threads where threads lose heap growth (#15355) Rive-Mono-Commit: 8af24f4cfccae0460020d9a492e72b30d270a001
diff --git a/renderer/include/rive/renderer/cmd/web_replay_worker.hpp b/renderer/include/rive/renderer/cmd/web_replay_worker.hpp index 5602ccd..b0783af 100644 --- a/renderer/include/rive/renderer/cmd/web_replay_worker.hpp +++ b/renderer/include/rive/renderer/cmd/web_replay_worker.hpp
@@ -47,21 +47,6 @@ m_queue.proxyAsync(emscripten_main_runtime_thread_id(), std::move(job)); } - // Safari 26.2 keeps a stale heap size on threads that did not grow it, so - // their copies into new pages trap. Waits for the worker to start. - bool seesHeapGrowth() - { - __builtin_wasm_memory_grow(0, 1); - size_t pages = __builtin_wasm_memory_size(0); - size_t workerPages = 0; - return emscripten_proxy_sync( - m_queue.queue, - m_thread, - &ReadHeapPages, - &workerPages) && - workerPages >= pages; - } - private: WebReplayWorker() { @@ -78,11 +63,6 @@ return nullptr; } - static void ReadHeapPages(void* pages) - { - *static_cast<size_t*>(pages) = __builtin_wasm_memory_size(0); - } - emscripten::ProxyingQueue m_queue; pthread_t m_thread{}; std::atomic<bool> m_listening{false};
diff --git a/src/wasm/web/README.md b/src/wasm/web/README.md index 349c6c1..38ea7ad 100644 --- a/src/wasm/web/README.md +++ b/src/wasm/web/README.md
@@ -75,7 +75,8 @@ - `rive_threads_pre.js`, hand written. Every build with threads links it. On a page that cannot share memory such a build would wait forever for threads that never start, so this fails its load at once and says why, - and the host loads its build without threads instead. + and the host loads its build without threads instead. It also fails the + load in Safari 26.2 to 26.3.1, whose threads lose each other's heap growth. Regenerate with `python3 src/wasm/idl/generate.py`. Never edit the generated files. `generate.py --check` fails when they are stale, and that includes a
diff --git a/src/wasm/web/rive_threads_pre.js b/src/wasm/web/rive_threads_pre.js index 9e60825..8089ca5 100644 --- a/src/wasm/web/rive_threads_pre.js +++ b/src/wasm/web/rive_threads_pre.js
@@ -16,4 +16,85 @@ "; load the build without threads" ); } + + // emcc's abort leaves the pool's workers running, so a build that fails to + // start, and gives way to the build without threads, ends them here. + const riveThreadsOnAbort = Module["onAbort"]; + Module["onAbort"] = (what) => { + if (!runtimeInitialized) { + PThread.terminateAllThreads(); + } + riveThreadsOnAbort?.(what); + }; + + // Safari 26.2 to 26.3.1 keep a stale heap size on threads that did not grow + // it, so their copies into new pages trap. Only WebKit needs the check. + const riveUserAgent = globalThis.navigator?.userAgent ?? ""; + if (/AppleWebKit\//.test(riveUserAgent) && !/Chrome\//.test(riveUserAgent)) { + // Asked now, so the answer overlaps the download. + const riveMissesHeapGrowth = riveThreadsMissHeapGrowth(); + Module["preRun"] = [].concat(Module["preRun"] || [], () => { + addRunDependency("riveHeapGrowth"); + // A worker that never answers must not hold the load back. + const timeout = new Promise((resolve) => + setTimeout(resolve, 1000, false) + ); + Promise.race([riveMissesHeapGrowth, timeout]).then((misses) => { + if (misses) { + // abort throws once it has failed the load. + try { + abort( + "this build runs threads, and this browser loses heap growth " + + "between them (WebKit bug 303387); load the build without " + + "threads" + ); + } catch {} + return; + } + removeRunDependency("riveHeapGrowth"); + }); + }); + } +} + +function riveThreadsMissHeapGrowth() { + // A module that reads the size of the shared memory it imports. + const probe = new Uint8Array([ + 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60, + 0x00, 0x01, 0x7f, 0x02, 0x09, 0x01, 0x01, 0x6d, 0x01, 0x6d, 0x02, 0x03, + 0x01, 0x02, 0x03, 0x02, 0x01, 0x00, 0x07, 0x05, 0x01, 0x01, 0x73, 0x00, + 0x00, 0x0a, 0x06, 0x01, 0x04, 0x00, 0x3f, 0x00, 0x0b, + ]); + return new Promise((resolve) => { + let worker = null; + function done(misses) { + worker?.terminate(); + resolve(misses); + } + try { + const memory = new WebAssembly.Memory({ + "initial": 1, + "maximum": 2, + "shared": true, + }); + worker = new Worker( + URL.createObjectURL( + new Blob([ + "onmessage=(e)=>{const i=new WebAssembly.Instance(" + + "new WebAssembly.Module(e.data[0]),{m:{m:e.data[1]}});" + + "postMessage(0);onmessage=()=>postMessage(i.exports.s())}", + ]) + ) + ); + worker.onerror = () => done(false); + worker.onmessage = () => { + memory.grow(1); + worker.onmessage = (event) => done(event.data < 2); + worker.postMessage(0); + }; + worker.postMessage([probe, memory]); + } catch { + done(false); + } + }); }