pthread: wake cond/sem waiters when cancelled - #428
Closed
derfsss wants to merge 1 commit into
Closed
Conversation
pthread_cancel() signals the target thread''s dedicated cancel signal
(inf->cancel_signal_mask), but _pthread_cond_timedwait() does not
include that signal in its Wait() mask -- it only waits on the condvar
waiter bit, the optional timer bit and SIGBREAKF_CTRL_C. As a result a
thread parked in pthread_cond_wait / pthread_cond_timedwait / sem_wait
(which is built on the cond wait) never wakes when cancelled, and a
subsequent pthread_join on it blocks forever.
Repro:
pthread_create(&t, NULL, fn_that_cond_waits_forever, NULL);
sleep(1);
pthread_cancel(t); /* returns 0 */
pthread_join(t, &res); /* hangs forever */
Fix: include the dedicated cancel signal in the wait mask and run
pthread_testcancel() when woken by it, exactly as is already done for
the SIGBREAKF_CTRL_C fallback. The existing cancellation cleanup
handler (CondWaitCleanupHandler) already handles waiter-node removal
and mutex reacquisition for this path, and the mutex is reacquired
before cancellation cleanup runs, as POSIX requires.
Found porting OpenJDK 17 to AmigaOS 4: JVM daemon threads parked in
pthread_cond_wait could never be cancelled at VM shutdown.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Collaborator
|
Can you please try all pthreads examples in test_library/threads folder? just go in test_library and compile everything with make. We need to be sure that we don't broke anything on that examples |
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.
Problem
pthread_cancel()of a thread blocked inpthread_cond_wait/pthread_cond_timedwait/sem_wait(which is built on the cond wait) is never acted upon: the target never wakes, and a subsequentpthread_joinon it blocks forever.The cause:
pthread_cancel()signals the target's dedicated cancel signal (inf->cancel_signal_mask), but_pthread_cond_timedwait()does not include that signal in itsWait()mask — it only waits on the condvar waiter bit, the optional timer bit andSIGBREAKF_CTRL_C.Repro
Fix
Include the dedicated cancel signal in the wait mask and run
pthread_testcancel()when woken by it — exactly as is already done for theSIGBREAKF_CTRL_Cfallback. The existing cancellation cleanup handler (CondWaitCleanupHandler) already handles waiter-node removal and mutex reacquisition for this path, so cancellation semantics match POSIX (mutex is reacquired before cleanup handlers run).Validation
Tested on AmigaOS 4.1 FE (QEMU amigaone) with a clean
GNUmakefile.os4build of this branch: cancel + join of a cond-parked thread and of a sem-parked thread both complete withPTHREAD_CANCELED. Found while porting OpenJDK 17 to AmigaOS 4, where JVM daemon threads parked inpthread_cond_waitcould never be cancelled at VM shutdown.🤖 Generated with Claude Code