Skip to content

Update emscripten_futex_wait to return -EINTR on spurious wakeups. NFC#26735

Merged
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:futex_EINTR
Apr 22, 2026
Merged

Update emscripten_futex_wait to return -EINTR on spurious wakeups. NFC#26735
sbc100 merged 1 commit into
emscripten-core:mainfrom
sbc100:futex_EINTR

Conversation

@sbc100
Copy link
Copy Markdown
Collaborator

@sbc100 sbc100 commented Apr 21, 2026

The internal musl code assumes that futex wait returns -EINTR if the wait is interrupted.

We return -EINTR here in case we wakeup to run a timer or if were notified to wake up by another thread using emscripten_thread_notify.

Followup to #26659

@sbc100 sbc100 force-pushed the futex_EINTR branch 3 times, most recently from ede491d to d251166 Compare April 21, 2026 16:32
Comment thread test/test_posixtest.py
}

unsupported = {
'test_pthread_spin_lock_1_1': 'alarm causes spurious wakeup during pthread_join',
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have realized this was not the correct behavior when I made this change.

@sbc100 sbc100 added this to the Wasm64 all tests passing milestone Apr 21, 2026
@sbc100 sbc100 requested review from kripken and tlively April 21, 2026 17:52
@sbc100 sbc100 changed the title Update emscripten_futex_wait to return -EINTR on spurious wakeups. NFC Update emscripten_futex_wait to return -EINTR on spurious wakeups. NFC Apr 21, 2026
@sbc100 sbc100 removed this from the Wasm64 all tests passing milestone Apr 21, 2026
// Returns -ETIMEOUT if the maxWaitMilliseconds timeout was exceeded.
// Returns -EINTR if the operation was interrupted (e.g. a timer fired, or an
// async signal was received).
// Returns 0 on success (i.e. Another thread signaled this address)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Returns 0 on success (i.e. Another thread signaled this address)
// Returns 0 on success (i.e. another thread signaled this address)

Comment thread system/lib/pthread/emscripten_futex_wait.c Outdated
// __builtin_wasm_memory_atomic_wait32 so we have busy wait instead.
if (!_emscripten_thread_supports_atomics_wait()) {
ret = futex_wait_main_browser_thread(addr, val, max_wait_ms, cancelable);
emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_WAITFUTEX, EM_THREAD_STATUS_RUNNING);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this go away?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The emscripten_conditional_set_current_thread_status pushing and popping is now handled by the wrapper function below. It allows simpler control flow in this function since no all exit paths need to remember to do this anymore (also removes some duplication).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks.

Comment thread system/lib/pthread/emscripten_futex_wait.c Outdated
Comment thread system/lib/pthread/threading_internal.h
Comment thread test/pthread/test_pthread_join_interrupted.c Outdated
Comment thread test/pthread/test_pthread_join_interrupted.c Outdated
Comment thread test/pthread/test_pthread_join_interrupted.c Outdated
Comment thread ChangeLog.md Outdated
@sbc100 sbc100 force-pushed the futex_EINTR branch 2 times, most recently from 96204a4 to 22298f0 Compare April 21, 2026 23:48
The internal musl code assumes that futex wait return -EINTR if the wait
is interrupted.

We return -EINTR here in case we wakeup to run a timer or if were
notified to wake up by another thread using `emscripten_thread_notify`.
Copy link
Copy Markdown
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not an expert on this but it looks right.

@sbc100 sbc100 merged commit 47e7d67 into emscripten-core:main Apr 22, 2026
29 checks passed
@sbc100 sbc100 deleted the futex_EINTR branch April 22, 2026 05:37
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.

3 participants