rust-lang / rust-lang/rust

Mutex::try_lock doesn't error in wasm32-emscripten

Open
#156,350 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-unsound needs-triage O-emscripten P-high T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Mutex::try_lock doesn't produce an Err in wasm32-emscripten.

use std::sync::Mutex;
fn main() {
    let m: Mutex<u32> = Mutex::new(1);
    let a = m.try_lock().unwrap();
    let b = m.try_lock().unwrap();
    println!("a + b = {}", *a + *b)
}

In x64, compiling and running this produces:

thread 'main' panicked at try_lock.rs:6:26:
called `Result::unwrap()` on an `Err` value: "WouldBlock"

When built with --target=wasm32-unknown-emscripten (default non-pthreads) there is no error:

a + b = 2

Discovered by @joelburton in https://github.com/curiousdannii/remglk-rs/pull/11

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 by reproducing the example with the wasm32-unknown-emscripten target in its default non-pthreads configuration, then compare Mutex::try_lock behavior with the x64 result described here. The issue names no source file or test; done means the second try_lock produces the expected Err rather than allowing both locks and printing a + b = 2.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, wasm
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.