Skip to content

Multithreaded WASM build: -sPTHREAD_POOL_SIZE too low for multi-engine apps #363

@kbatchu

Description

@kbatchu

Problem

The multithreaded WASM build (@ladybugdb/wasm-core/multithreaded) exhausts the Emscripten thread pool when running alongside other WASM engines (DuckDB-WASM, WebLLM) in the same browser tab.

Console output:

[LadybugDB] OPFS mounted at /persist
Tried to spawn a new thread, but the thread pool is exhausted.
This might result in a deadlock unless some threads eventually exit or the code explicitly breaks out to the event loop.
If you want to increase the pool size, use setting `-sPTHREAD_POOL_SIZE=...`.

Context

Our application runs three WASM engines concurrently in one browser tab:

  • DuckDB-WASM (geospatial SQL queries)
  • WebLLM (in-browser LLM inference via WebGPU)
  • LadybugDB-WASM (graph database)

OPFS persistence (PR #360) works correctly — the mount succeeds. But when LadybugDB tries to spawn threads for database operations, the browser's pthread pool is already partially consumed by the other engines, causing exhaustion and deadlock.

Reproduction

  1. Load DuckDB-WASM + WebLLM in the same page
  2. Import @ladybugdb/wasm-core/multithreaded
  3. Mount OPFS: await fs.mountOpfs('/persist')
  4. Create a database: new lbug.Database('/persist/db') — thread pool exhaustion

Suggested Fix

Increase -sPTHREAD_POOL_SIZE in the multithreaded CMake build configuration (currently defaults to Emscripten's default of 4). A value of 8-12 would accommodate multi-engine browser applications.

Alternatively, consider making it configurable at runtime or using -sPTHREAD_POOL_SIZE_STRICT=0 to allow dynamic growth.

Environment

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions