Skip to content

Tolerate spurious wakeups on the background worker thread.#350

Merged
iCharlesHu merged 1 commit into
swiftlang:mainfrom
iCharlesHu:charles/pthread-spurious-wakeup
Jul 9, 2026
Merged

Tolerate spurious wakeups on the background worker thread.#350
iCharlesHu merged 1 commit into
swiftlang:mainfrom
iCharlesHu:charles/pthread-spurious-wakeup

Conversation

@iCharlesHu

Copy link
Copy Markdown
Contributor

Subprocess spawns run on a single shared background worker thread that sleeps on a POSIX condition variable while its work queue is empty. pthread_cond_wait is permitted by POSIX to return without a matching signal and does so in practice on Linux when the waiting thread is interrupted by a signal (aka 'spurious wakeup').

Update the wait logic by looping instead of branching, so a spurious wakeup with an empty queue simply re-checks the predicate and goes back to sleep. This change prevents the worker thread from exiting prematurely, which leads to hangs and continuation leaks.

Resolves: #348

Subprocess spawns run on a single shared background worker thread that sleeps on a POSIX condition variable while its work queue is empty. pthread_cond_wait is permitted by POSIX to return without a matching signal and does so in practice on Linux when the waiting thread is interrupted by a signal (aka 'spurious wakeup').

Update the wait logic by looping instead of branching, so a spurious wakeup with an empty queue simply re-checks the predicate and goes back to sleep. This change prevents the worker thread from exiting prematurely, which leads to hangs and continuation leaks.
@iCharlesHu iCharlesHu requested a review from jakepetroules July 8, 2026 21:03
@iCharlesHu iCharlesHu merged commit 1648fea into swiftlang:main Jul 9, 2026
89 of 92 checks passed
@iCharlesHu iCharlesHu deleted the charles/pthread-spurious-wakeup branch July 9, 2026 03:12
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.

Subprocess spawns stop working, and their continuations leak, if the worker thread is signalled while empty

2 participants