rust-lang / rust-lang/rust

Run TLS destructors for wasm32-wasip1-threads make Node.js stuck

Open
#137,510 20 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-thread-locals C-bug O-wasi O-wasip1-threads O-wasm T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

https://github.com/Brooooooklyn/rust-wasi-thread-local-stuck

use std::cell::RefCell;
use std::collections::HashMap;

thread_local! {
  static THREAD_LOCAL_DATA: RefCell<HashMap<u32, u32>> = RefCell::new(HashMap::new());
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn set_thread_local_data(key: u32, value: u32) {
    THREAD_LOCAL_DATA.with(|data| {
        data.borrow_mut().insert(key, value);
    });
}

I expected to see this happen: Program run and exit normally

Instead, this happened: hang forever

(edit): See https://github.com/toyobayashi/emnapi/issues/136#issuecomment-2677400889 for where and why

Meta

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: aarch64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7
Background

The NAPI-RS project enables the compilation of Rust projects to the wasm32-wasip1-threads target, allowing them to run in Node.js. However, since Node.js doesn’t natively support WASI threads, we simulate a threaded environment using emnapi. This setup worked effectively until version 1.85.0. Many projects, such as rolldown and rspack, have been successfully compiled to WebAssembly targets to run seamlessly on platforms like StackBlitz and in web browsers.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked rust-wasi-thread-local-stuck reproducer and the thread_local! entry point, running it for wasm32-wasip1-threads under Rust 1.85.0 in Node.js. Trace the TLS destructor behavior and verify that the program exits normally rather than hanging forever.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, rust, wasm
Domain
compilers, operating-systems
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.