From d5c2ba04fe15f02b171fd8157550af0bfb4490cc Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Sun, 4 Aug 2024 10:36:39 +0200 Subject: [PATCH] Reduce concurrency to 1 on the main browser thread (#69) --- CHANGELOG.md | 6 ++++++ src/CMakeLists.txt | 9 +++++---- src/vips-library.js | 7 +++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0b028dd8..29dce1aa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,12 @@ Uses libvips v8.15.2, compiled with Emscripten v3.1.64. [#70](https://github.com/kleisauke/wasm-vips/pull/70) [@marcosc90](https://github.com/marcosc90) +### Changed + +- Reduce concurrency to 1 when wasm-vips was initialized on the main + browser thread. + [#69](https://github.com/kleisauke/wasm-vips/issues/69) + ## [v0.0.9] - 2024-06-01 Uses libvips v8.15.2, compiled with Emscripten v3.1.60. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 679c1aaa0..e297afc9a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -131,7 +131,6 @@ set(MAIN_LINK_OPTIONS -sINITIAL_MEMORY=1GB -sSTACK_SIZE=256KB -sALLOW_TABLE_GROWTH - -sALLOW_BLOCKING_ON_MAIN_THREAD -sTEXTDECODER=2 -sASSERTIONS=0 -sFORCE_FILESYSTEM @@ -174,12 +173,14 @@ if ("web" IN_LIST ENVIRONMENT) -sMIN_CHROME_VERSION=91 ) - # Build with `-sPTHREAD_POOL_SIZE="navigator.hardwareConcurrency + 2 + 1"` since `vips_sink_disc` uses two - # write-behind background threads, plus we need another one to run a maximum of two pipelines simultaneously. + # libvips requires spawning at least VIPS_CONCURRENCY threads synchronously, with a minimum of 3 threads per + # pipeline. This count includes the two write-behind background threads used by `vips_sink_disc`. To support up to + # two concurrent pipelines, we need to double that number. Therefore, build with: + # `-sPTHREAD_POOL_SIZE='Math.max(navigator.hardwareConcurrency, 6)'`. # Only needed on the web, pthreads on Node.js can be spawned synchronously without waiting for an event loop tick. set(WEB_LINK_OPTIONS --use-preload-plugins - -sPTHREAD_POOL_SIZE='navigator.hardwareConcurrency+2+1' + -sPTHREAD_POOL_SIZE='navigator.hardwareConcurrency>6?navigator.hardwareConcurrency:6' ${WEB_MIN_TARGETS} ) diff --git a/src/vips-library.js b/src/vips-library.js index b7a128b24..b21548f47 100644 --- a/src/vips-library.js +++ b/src/vips-library.js @@ -13,6 +13,13 @@ var LibraryVips = { addOnPreRun(() => { // Enforce a fixed thread pool by default on web ENV['VIPS_MAX_THREADS'] = {{{ PTHREAD_POOL_SIZE }}}; + + // We cannot safely spawn dedicated workers when wasm-vips was initialized on the main browser thread. + // Therefore, to avoid any potential deadlocks, we reduce the concurrency to 1. For more details, + // see: https://emscripten.org/docs/porting/pthreads.html#blocking-on-the-main-browser-thread + if (!ENVIRONMENT_IS_WORKER) { + ENV['VIPS_CONCURRENCY'] = 1; + } }); #endif