emscripten-core / emscripten-core/emscripten

Using WasmFS / OPFS from the main thread deadlocks on Safari

Open
#20,650 13 comments 2 reactions 1 assignee Claimed by @tlively View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

Please include the following in your bug report:

**Version of emscripten/emsdk:**
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.42 (6ede0b8fc1c979bb206148804bfb48b472ccc3da)
clang version 17.0.0 (https://github.com/llvm/llvm-project f3b64887de61020c09404bfee97b2fadd30df10a)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: ``

**Full link command and output with `-v` appended:**
emcc main.cpp -o hello.html -sWASMFS -pthread -sPTHREAD_POOL_SIZE=4

```cpp
#include
#include
#include

#include
#include

#include

extern "C"
{
std::thread* bgThread = nullptr;

EMSCRIPTEN_KEEPALIVE int main()
{
// Initialize wasmfs in bg thread
bgThread = new std::thread([]()
{
auto wasmfs_backend = wasmfs_create_opfs_backend();
const char* rootMount = "/persistent";
bool exists = access(rootMount, F_OK) == 0;
if (!exists)
wasmfs_create_directory(rootMount, 0777, wasmfs_backend);
});

// Keep the standard library runtime alive even after exiting main`
emscripten_exit_with_live_runtime();
return 0;
}

EMSCRIPTEN_KEEPALIVE void checkForDirectory()
{
bool exists = std::filesystem::exists("/persistent/data");
printf("exists = %d\n", exists);
}
}
```

Load the html with the necessary COOP settings (in order to get pthread to work). Then in the web debugger console, make a call to `window.Module._checkForDirectory()`. In Chrome, the output is printed out successfully. In Safari, the output is never printed out. The main thread is stuck in a tight loop, and the debugger is no longer useable. If there were other web workers running, their execution also seems to pause.

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.