emscripten-core / emscripten-core/emscripten

WASMFS JS proxy thread assertion too strict

Open
#23,636 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Right now, JS code in Module.onRuntimeInitialized can’t create certain WASMFS backends before main runs, which is something that’s possible in the JS filesystem and possible under wasmfs with JSPI. This is because e.g. fetchfs and opfs backends (as well as thread_util.h’s ProxyWorker) currently assert at construction time that they are either not on the main thread or they are running with JSPI.

The motivation behind these assertions is that the main thread may not be able to spawn a thread in a fresh worker without causing a deadlock, but sometimes it’s possible to do this safely (either to succeed or at least to fail if a worker is not available). Specifically, if the worker pool has a free unused worker, spawning a thread will definitely succeed.

While there is currently no api the C code could use to detect whether a worker is free, when run under PTHREAD_POOL_STRICT > 0 the worker fetching code will throw a warning (if strict = 1) or error out (if strict = 2) in the condition where a deadlock could happen by spawning a new worker from the main thread. I therefore think the assertions should be conditionally compiled in only when PTHREAD_POOL_STRICT is 0, and the overly strict condition now amounts to a bug.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.