Explicitly allow spurious wakeups in emscripten_futex_wait - #27760
Merged
Merged
Conversation
kleisauke
approved these changes
Sep 22, 2026
sbc100
force-pushed
the
futex-wait-docs
branch
2 times, most recently
from
September 22, 2026 16:36
fabb519 to
44860ca
Compare
In emscripten-core#26735, `emscripten_futex_wait` was updated to return `-EINTR` when woken via side-channel notifications (`_emscripten_thread_notify`) or when a timer fired. However, side-channel notifications are not POSIX signals, and returning `-EINTR` creates the false expectation that a return value of `0` guarantees the underlying condition was met. In practice, spurious wakeups returning `0` can still occur (e.g. via races in `_emscripten_thread_notify`, collateral wakeups on shared wait addresses, or async wait cancellations). Furthermore, returning `-EINTR` could cause functions like `sem_timedwait` to fail prematurely. Revert the `-EINTR` reporting so that `emscripten_futex_wait` matches the Linux `futex(2)` contract: any non-error wakeup returns `0`, and callers must always verify their application condition in a loop. This change has to land after emscripten-core#27755. Fixes: emscripten-core#27759
sbc100
force-pushed
the
futex-wait-docs
branch
from
September 22, 2026 17:36
44860ca to
2421590
Compare
Collaborator
Author
|
I make some further cleanups here. PTAL. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In #26735,
emscripten_futex_waitwas updated to return-EINTRwhenwoken via side-channel notifications (
_emscripten_thread_notify) orwhen a timer fired.
In practice, however, spurious wakeups returning
0can still occur(e.g. via races in
_emscripten_thread_notify, collateral wakeups onshared wait addresses, or async wait cancellations). Furthermore,
returning
-EINTRcould cause functions likesem_timedwaitto failprematurely (and in musl's
__timedwait_cp,EINTRis alreadyconverted to
0because__eintr_valid_flagis always0).Revert the
-EINTRreporting so thatemscripten_futex_waitmatchesthe Linux
futex(2)contract: any non-error wakeup returns0, andcallers must always verify their application condition in a loop. When
max_wait_msis shortened on the main runtime thread to service_emscripten_next_timer(), track this viatimeout_adjustedso that anearly wakeup returns
0rather than-ETIMEDOUT.Finally, since no callers check the return value of
_emscripten_yieldanymore, change
_emscripten_yieldand_emscripten_check_timersbackto returning
void.Fixes: #27759