Repository navigation
coop-threads: fix pthread_join on futex wait errors, and report -ENOMEM - #926
Merged
Merged
Conversation
`__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.
alexcrichton
approved these changes
Dec 2, 2026
`__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.
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.
__wasilibc_futex_waitreturned a positive ENOMEM when allocation failed. More importantly, coop-threads pthread_join treated any 0 from__timedwaitas "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.