Skip to content

coop-threads: fix pthread_join on futex wait errors, and report -ENOMEM - #926

Merged
alexcrichton merged 3 commits into
WebAssembly:mainfrom
ricochet:coop-join-recheck-exit
Dec 3, 2026
Merged

alexcrichton merged 3 commits into
WebAssembly:mainfrom
ricochet:coop-join-recheck-exit

Conversation

@ricochet

Copy link
Copy Markdown
Contributor

__wasilibc_futex_wait returned a positive ENOMEM when allocation failed. More importantly, coop-threads pthread_join treated any 0 from __timedwait as "the thread exited". That includes spurious wakeups and allocation failure, so join could free a running thread's stack. Join now loops until DT_EXITED, yielding between retries and checking a timed join's deadline.

The second commit moves the negative relative tv_sec check before the futex entry is created, so it no longer leaks. Each commit adds a test that fails without it.

I found this same pattern in the shared musl code as well. Under coop threads it can spin without yielding when allocation fails. Fixing that touches common thread code so I'm opting to leave that as it's own change/follow-up.

`__wasilibc_futex_wait` returned a positive `ENOMEM` when it couldn't
allocate the futex table or a waitlist entry. The header documents a
negated errno, `-ENOMEM`, like the function's other error returns.

`pthread_join` treated any zero return from `__timedwait` as the thread
having exited. `__timedwait` also returns 0 for a spurious wakeup, and
folds errors other than EINTR, ETIMEDOUT and ECANCELED into 0, so a
failed allocation made join return while the thread was still running.
Join then read a result that hadn't been written yet and freed the
thread's stack while it was in use.

Join now waits until the thread's `detach_state` is `DT_EXITED`, which
the exiting thread sets before it wakes the joiner. If a wait returns
early, it yields before waiting again: threads are cooperative, so
retrying straight away after an allocation failure would never let the
joined thread run. A timed join also checks its deadline between
retries, because a wait that fails to allocate returns before it looks
at the deadline.

The new test fails allocations with `-Wl,--wrap=malloc`, so it's
static-only: the wrap can't reach allocations made inside `libc.so`.
`__wasilibc_futex_wait` checked for a relative timeout with a negative
`tv_sec` only after it had created and referenced the waitlist entry for
the address. It then returned `-EINVAL` without releasing the entry, so
the entry and its allocation were never freed.

Check it together with the other timespec checks, before anything is
allocated.
@ricochet
ricochet requested a review from TartanLlama November 30, 2026 13:58
Comment thread test/CMakeLists.txt Resolved
`__timedwait` already returns `ETIMEDOUT` once a timed join's deadline
passes, so the join loop doesn't need to re-check the deadline itself.

Remove the `--wrap=malloc` tests, which were too specific to this
situation.
@alexcrichton
alexcrichton merged commit c556c74 into WebAssembly:main Dec 3, 2026
37 checks passed
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.

2 participants