Skip to content

Settle async WebAssembly on Linux JavaScriptCore - #2095

Open
bkaradzic-microsoft wants to merge 2 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:fix/linux-jsc-async-wasm
Open

bkaradzic-microsoft wants to merge 2 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:fix/linux-jsc-async-wasm

Conversation

@bkaradzic-microsoft

Copy link
Copy Markdown
Member

Fixes #2094 (follow-up to #2068).

Problem

On Linux, WebKitGTK's JavaScriptCore completes async WebAssembly.compile / WebAssembly.instantiate on the GLib main context of the thread that created the VM. The AppRuntime JS thread never iterated that context, so these promises never settled. As a result, SPZ (Gaussian splatting) and CSG2 (Manifold) scenes timed out.

Fix

  • AppRuntime_JavaScriptCore.cpp (Linux JavaScriptCore only):
    • The JS thread creates its own GMainContext and pushes it as the thread default before the VM is created, so WTF's RunLoop adopts it.
    • RunDispatchLoop alternates between running dispatcher work and g_main_context_iteration(ctx, TRUE). g_main_context_wakeup is the dispatcher waker.
  • GC timer crash: iterating the context also fires JSC's GC timers. FullGCActivityCallback reaches WTF::MemoryPressureHandler, which on Linux creates a timer on RunLoop::mainSingleton(). The JSC C API never creates that RunLoop, so the first full collection crashed.
    • The fix calls WTF::initializeMainThread() on the JS thread before creating the VM. libjavascriptcoregtk exports this function, but its public headers don't declare it, so this relies on an internal symbol. The jsc shell itself calls it.
  • AppRuntime links glib on Linux via pkg-config. Install/Test adds glib to the Linux link.

Tests

  • JsRuntimeHost: the WebAssembly unit tests now run on Linux JavaScriptCore. DestroyDoesNotDeadlock expectations match Apple.
  • Playground: removed Unix from excludedPlatforms on the 18 SPZ / CSG2 validations. Android JavaScriptCore stays excluded because jsc-android has no WebAssembly.

Local results (Ubuntu 24.04, clang, OpenGL, xvfb, libjavascriptcoregtk-4.1 2.52.6)

  • JsRuntimeHostUnitTests: 30/30 passed, including WebAssembly.
  • BN UnitTests: 160/160 passed.
  • Playground: all 18 re-enabled validations passed (38/38 in the gaussian/SPZ/CSG filter run).
  • Linux install test (Install/Test): builds and links.

WebKitGTK's JavaScriptCore completes async WebAssembly.compile/instantiate
through the GLib main context of the thread that created the VM. The
AppRuntime thread never iterated that context, so the promises never settled
on Linux.

Give the JS thread its own GMainContext (pushed as thread default before the
VM is created) and make RunDispatchLoop iterate it, using
g_main_context_wakeup as the dispatcher waker.

Iterating the context also fires JSC's GC timers. The full collection timer
reaches WTF::MemoryPressureHandler, which needs WTF's main RunLoop; the
JavaScriptCore C API never creates it, so the first full GC crashed. Call
WTF::initializeMainThread (exported by libjavascriptcoregtk, not in its public
headers) on the JS thread before creating the VM.

Re-enable the WebAssembly unit tests and the 18 SPZ / CSG2 Playground
validations on Linux JavaScriptCore, and link glib in the Linux install test.

Fixes BabylonJS#2094

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 8bd57bfd-bbb3-4e86-8a17-9a44fb995d0f

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Binding WTF’s process-global main loop to the first runtime’s disposable thread can cause a use-after-free when a later runtime triggers timer-driven full GC.

1 open issue
What changed in this PR

Extends the earlier Apple JavaScriptCore fix to Linux so asynchronous WebAssembly operations can settle, enabling SPZ Gaussian splatting and CSG2 scenes.

Changes:

  • Pumps a thread-default GLib context alongside dispatcher work and initializes WTF’s main run loop for GC timers.
  • Adds GLib linking to AppRuntime and the Linux install test.
  • Enables Linux JavaScriptCore WebAssembly tests and 18 Playground validations, while retaining Android JavaScriptCore exclusions.
File Description
Install/​Test/​CMakeLists.txt Links GLib in the Linux install test.
Dependencies/​JsRuntimeHost/​Tests/​UnitTests/​Source/​Tests.AppRuntime.cpp Adjusts Linux JavaScriptCore shutdown-test expectations.
Dependencies/​JsRuntimeHost/​Tests/​UnitTests/​Source/​Scripts/​tests.webAssembly.ts Enables async WebAssembly tests on Linux JavaScriptCore.
Dependencies/​JsRuntimeHost/​Core/​AppRuntime/​Source/​AppRuntime_JavaScriptCore.cpp Adds GLib context lifecycle, dispatch-loop pumping, and WTF initialization.
Dependencies/​JsRuntimeHost/​Core/​AppRuntime/​CMakeLists.txt Configures GLib dependencies for non-Apple, non-Android Unix JavaScriptCore builds.
Apps/​Playground/​Scripts/​config.json Removes Unix exclusions from 18 SPZ and CSG2 validations.

Review effort: Balanced

…Core

WTF::initializeMainThread is process-once and binds WTF's main RunLoop to
the calling thread, whose thread-specific holder owns the only reference.
Calling it on an AppRuntime thread left a dangling RunLoop::mainSingleton()
once that runtime exited, so a later runtime's first timer-driven full
collection (FullGCActivityCallback -> MemoryPressureHandler::singleton(),
whose timer is created on the main RunLoop) used freed memory.

Call it once from a dedicated detached thread that pushes a private GLib
context and keeps it pumped for the rest of the process.

Add AppRuntime.TimerDrivenCollectionAfterFirstRuntimeExits, which re-runs
itself in a fresh process, destroys a runtime that never collects, then
lets a second runtime run timer-driven full collections.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 8bd57bfd-bbb3-4e86-8a17-9a44fb995d0f
@bkaradzic-microsoft

Copy link
Copy Markdown
Member Author

CI note (run 44434039032, attempt 1)

Failed: iOSTests / test (JsRuntimeHost UnitTests on the iOS simulator). ci-complete failed only because of it.

This looks like the simulator's console capture losing output, not a test failure:

  • xcrun simctl launch --console returned 0, but the captured stdout stops in the middle of the mocha run (after "cyclic prototype chains / still reports a long acyclic chain in full"), and stderr is empty. The step then fails because the exit code the harness prints to stderr is missing.
  • The simulator log shows the app running past that point. It logged the later StandardStreamLogger test, and SpringBoard reported a voluntary exit (RBSProcessExitContext| voluntary). There's no crash report in the diagnostics artifact.
  • This PR changes nothing on Apple. All runtime changes are behind JSRUNTIMEHOST_JAVASCRIPTCORE_GLIB (Linux only). The same tests pass on master and Restore visual-test coverage and enable deferred culling checks #2092. iOS_Xcode26, MacOS, and all other jobs passed.

Job timings (queue wait → run time):

Job Queue Run
Win32_x64_V8_D3D11 0m 37m
Ubuntu_Clang_JSC 0m 33m
MacOS 6m 12m
Android_Ubuntu_V8 0m 11m
MacOS_QuickJS 6m 10m
iOS_Xcode26 2m 10m
Android_Ubuntu_JSC 3m 9m
UWP_arm64_JSI 0m 9m
MacOS_Ninja 5m 8m
Win32_x64_D3D11_PrecompiledShaderTest 0m 8m
Android_Ubuntu_QuickJS 2m 8m
UWP_arm64 0m 8m
Win32_x64_D3D11 0m 7m
Win32_x86_D3D11 0m 7m
UWP_x64_JSI 0m 7m
UWP_x64_V8 0m 7m
iOSTests (failure) 0m 7m
Win32_Installation 0m 7m
UWP_x64 0m 6m
Win32_x64_QuickJS_D3D11 0m 6m
Win32_x64_JSI_D3D11 0m 6m
Linux_Installation 0m 6m
MacOS_Installation 6m 5m
iOS_Installation 0m 4m
Win32_x64_D3D12 6m 4m
Ubuntu_GCC_JSC 0m 3m
AndroidTests 0m 3m
Ubuntu_Clang_QuickJS 0m 2m
Ubuntu_Clang_Vulkan 0m 2m

Total wall-clock: 37 min. The critical path is Win32_x64_V8_D3D11 (37m) and Ubuntu_Clang_JSC (33m). macOS runners queued 5–6 min, Windows 0–6 min.

Rerunning failed jobs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Settle async WebAssembly on Linux JavaScriptCore

2 participants