Skip to content

fixed the Atomics.wait() result when the timeout races with Atomics.notify() (#636) - #637

Open
vzhn wants to merge 1 commit into
bellard:masterfrom
vzhn:fix-atomics-wait-result
Open

vzhn wants to merge 1 commit into
bellard:masterfrom
vzhn:fix-atomics-wait-result

Conversation

@vzhn

@vzhn vzhn commented Nov 28, 2026 •

Copy link
Copy Markdown

Fixes #636 (Atomics.wait() returns "timed-out" for a waiter that Atomics.notify() woke).

js_atomics_wait() took its result from the return code of pthread_cond_wait() / pthread_cond_timedwait(). A timeout that races with Atomics.notify() gave "timed-out" although notify counted the waiter, and a spurious wake-up gave "ok" with no notify. Now the wait loops while the waiter is still linked, and the result is "ok" exactly when Atomics.notify() removed it, as Atomics.waitAsync() already does with async_state. The first timed wait uses the requested timeout as before; after a wake-up the remaining time is taken from a deadline rounded up to the next ms, so the wait never ends before the (integer ms) timeout. A pthread error other than a timeout also ends the loop, so it cannot spin with the mutex held. js_atomics_get_time_ms() gets a forward declaration so js_atomics_wait() can use it.

Tested on Linux x86_64 (glibc 2.43); the __APPLE__ path was not built.

  • the reproducer from the issue: 0 mismatches (before: 6 to 57 in 20,000 rounds)
  • spurious wake-ups injected with an LD_PRELOAD shim: an untimed wait now returns "ok" only after the notify; a 100 ms wait returns "timed-out" after 101 ms (before: "ok" after 1 ms)
  • 480,000 waits of 1 ms: none shorter than 1 ms
  • pthread_cond_wait() / pthread_cond_timedwait() forced to fail with EINVAL (LD_PRELOAD shim): the waits return "timed-out" at once instead of spinning
  • test262 built-ins/Atomics (-a, 389 tests): 0 errors, before and after
  • make test: passes (with TZ=UTC; test_intl.js depends on the local time zone)

@vzhn
vzhn marked this pull request as draft November 28, 2026 11:39
@vzhn
vzhn force-pushed the fix-atomics-wait-result branch 2 times, most recently from f3d549b to c323249 Compare November 28, 2026 11:43
@vzhn
vzhn marked this pull request as ready for review November 28, 2026 11:48
@vzhn
vzhn force-pushed the fix-atomics-wait-result branch from c323249 to 6e1d535 Compare November 28, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Atomics.wait() returns "timed-out" for a waiter that Atomics.notify() woke

1 participant