WasmFS: ProxyWorker's 50 ms heartbeat outlives its thread and runs the freed queue on a pooled Worker (`Assertion failed: pthread_self()` in `emscripten_proxy_execute_queue`)

未关闭
#27,858 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp, wasm
领域
devtools

调研方向

Start with the heartbeat in src/lib/libwasmfs.js and ProxyWorker lifecycle in system/lib/wasmfs/thread_utils.h, then review Worker pooling in src/lib/libpthread.js. Run the provided proxy_worker_exit.cpp command with -pthread, -sWASMFS, -sPTHREAD_POOL_SIZE=2, and -sEXIT_RUNTIME; done means repeated heartbeats no longer access the destroyed queue or abort after worker reuse.

由索引模型根据 Issue 内容生成。

描述

emscripten::ProxyWorker (system/lib/wasmfs/thread_utils.h), which the OPFS and fetch backends use for their dedicated thread, starts a heartbeat on that thread: a 50 ms setInterval that calls emscripten_proxy_execute_queue() on the ProxyWorker's queue. Nothing stops it except ABORT.

~ProxyWorker() cancels and joins the thread, and then the queue is freed. But the thread's Worker goes back to the pool rather than being terminated, and its timer goes with it. So every 50 ms the Worker runs a freed queue: while it is idle, pthread_self() is null and, with assertions, emscripten_proxy_execute_queue() aborts; without assertions, or once the Worker hosts another thread, it reads freed memory. Backends are destroyed at exit with EXIT_RUNTIME, so a program with an OPFS or fetch backend can abort at exit this way.

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 6.0.12-git (ce6d2aad8bbf1e508e5a6d112f986617198bdc0d)
clang version 24.0.0git (https:/github.com/llvm/llvm-project d3e9891b54da54deebe2a6df0eb67c8ec155fa65)

main at ce6d2aad8 with the toolchain from test/emsdk_version.txt (dbaebb43), Node.js v24.19.0, x86_64 Linux.

Test case (proxy_worker_exit.cpp, which includes the internal header as a backend does):

// A destroyed ProxyWorker must leave nothing running against its freed queue,
// both while its Worker is idle in the pool and once it hosts another thread.
// Each wait spans several of the thread's 50 ms heartbeats.

#include <assert.h>
#include <condition_variable>
#include <emscripten/emscripten.h>
#include <emscripten/eventloop.h>
#include <mutex>
#include <stdio.h>
#include <thread>

#include "thread_utils.h"

// A ProxyWorker can't be created on the main browser thread.
static void use_worker() {
  std::thread([] {
    emscripten::ProxyWorker worker;
    bool ran = false;
    worker([&] { ran = true; });
    assert(ran);
  }).join();
}

static void second_idle(void*) {
  printf("done\n");
  emscripten_force_exit(0);
}

static void first_idle(void*) {
  use_worker();
  emscripten_set_timeout(second_idle, 500, nullptr);
}

int main() {
  use_worker();
  emscripten_set_timeout(first_idle, 500, nullptr);
}

Failing command line in full:

$ em++ -pthread -sWASMFS -sPTHREAD_POOL_SIZE=2 -sEXIT_RUNTIME -I$EMSCRIPTEN/system/lib/wasmfs proxy_worker_exit.cpp -o t.js
$ node t.js
Aborted(Assertion failed: pthread_self(), at: /emsdk/emscripten/system/lib/pthread/proxying.c,113,emscripten_proxy_execute_queue)
worker sent an error! undefined:undefined: Aborted(Assertion failed: pthread_self(), at: /emsdk/emscripten/system/lib/pthread/proxying.c,113,emscripten_proxy_execute_queue)
node:internal/event_target:1136
  process.nextTick(() => { throw err; });
                           ^

RuntimeError: Aborted(Assertion failed: pthread_self(), at: /emsdk/emscripten/system/lib/pthread/proxying.c,113,emscripten_proxy_execute_queue)
    at abort (t.js:855:11)
    at ___assert_fail (t.js:1743:7)
    at wasm://wasm/0006d26e:wasm-function[189]:0x3aa3
    at t.js:887:12
    at Timeout._onTimeout (t.js:2262:13)
    at listOnTimeout (node:internal/timers:635:17)
    at process.processTimers (node:internal/timers:571:7)

Node.js v24.19.0
[exit status 1]

It aborts in 10 runs out of 10. At -O2 (no assertions) it prints done, since the freed queue is still readable and empty.

Where:

I have a fix ready and will send a PR: the destructor stops the heartbeat on the thread, with a synchronous proxied call, before cancelling it.

主要语言
C++
星标
27.8k
派生
3.6k
平均合并
1 天 6 小时
30 天内合并 PR
181

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

emscripten-core/emscripten 的其他 Issue

查看 emscripten-core/emscripten 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。