From 7ac30a39fe0bcc5d047bd496539419cafa403833 Mon Sep 17 00:00:00 2001 From: Matija Kecman Date: Wed, 15 Apr 2026 08:56:35 -0700 Subject: [PATCH] Fix mj_loadXML hang in single-threaded WASM builds The top-level CMakeLists.txt unconditionally added -pthread for all Emscripten builds, but PTHREAD_POOL_SIZE was only set when MUJOCO_WASM_THREADS=ON. This gave the ST build a live pthread runtime with zero pre-spawned workers, causing CompileMeshesAndTextures to deadlock when it tried to spawn a ThreadPool on the main thread. Fix by making -pthread conditional on MUJOCO_WASM_THREADS. Without -pthread, hardware_concurrency() returns 0 and the existing nthread < 2 guards naturally select the serial compilation path. PiperOrigin-RevId: 900191420 Change-Id: I0481b81fc6f7cccb52c7d7592dcf9dd317b5bad1 --- CMakeLists.txt | 8 ++++++-- wasm/CMakeLists.txt | 2 -- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 645ea60d..2c349167 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,8 +53,12 @@ endif() if(EMSCRIPTEN) option(MUJOCO_BUILD_TESTS_WASM "Build tests for WASM bindings" ON) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -O3 -pthread -fexceptions") + option(MUJOCO_WASM_THREADS "Build with multi-threading support" ON) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -O3 -fexceptions") + if(MUJOCO_WASM_THREADS) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") + endif() endif() if(APPLE AND (MUJOCO_BUILD_EXAMPLES OR MUJOCO_BUILD_SIMULATE)) diff --git a/wasm/CMakeLists.txt b/wasm/CMakeLists.txt index 08004839..80ed4feb 100644 --- a/wasm/CMakeLists.txt +++ b/wasm/CMakeLists.txt @@ -31,8 +31,6 @@ if(NOT MUJOCO_WASM_FILES) message(FATAL_ERROR "No source files found in codegen/generated/") endif() -option(MUJOCO_WASM_THREADS "Build with multi-threading support" ON) - # Set Emscripten linker flags set(EMCC_LINKER_FLAGS "--bind"