fix(libkernel): target pthread cond signalto wakeups #26174
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/26174"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-11-23T11:34:24Z.
What
Make
scePthreadCondSignalto/pthread_cond_signalto_npwake only the requested condition-variable waiter, matching the payload measurement already recorded from PS5 firmware 13.60.The previous implementation registered the target thread but then called the condition variable's shared
NotifyAll(), so every waiter returned. This change gives each registered waiter its ownTimedWait::Conditionand pending-wake state:signalselects one waiter;broadcastselects every currently unsignaled waiter;signalto(thread)selects only that thread;signalis cancelled, that one-waiter signal is transferred to another waiter. Targeted and broadcast wakes are not reassigned;cancelWaitcondition.scePthreadCanceltherefore queuescancelPendingand returns without waiting for an unrelated condition signal, while enabled cancellation keeps the existing wake path.The existing
signaltoargument/error behavior is unchanged: null threadEINVAL, unknown/dead threadESRCH, and a live thread not waiting on this conditionEPERM.Evidence
The target-only behavior is not newly guessed here. #11263 records a PS5 firmware 13.60 payload measurement for both SCE and POSIX forms: with two waiters,
signalto(A)returned 0, A woke, and B timed out. #13832 separately measured the invalid-threadESRCHcase.Review found two host-wait races after the initial implementation. oneandonlydean identified the notify-before-coarse-registration and timeout/signal races; head
cae95fc35fmoved the per-waiter wait onto the waiter-registry predicate mutex and added an unlocked-signaler stress case plus a Windows coarse-wait invocation. mihiii1337 then identified that cancellation-disabled waits still publishedcancelWait; head71525e1b4ffixes that atThreadCancel::BeginWaitand adds normal/recursive regressions provingscePthreadCancelreturns before an independent condition signal.Debt keeps the remaining unverified points: a finished-but-unjoined thread and console behavior when cancellation races a targeted signal.
Tested
Initial head
0b56103a89:guest_cond_timedwait_testson Ubuntu 24.04 / GCC 13.3 Release and passedguest_cond_timedwait.mainfailed the new target-isolation assertion.Race-fix head
cae95fc35f:guest_cond_timedwaiton Ubuntu 24.04 / GCC 13.3 Release; the same runtime-source negative control failed.Current head
71525e1b4f8f9bef30b0c5fe946818fc35dea5fdadds only the cancellation-state-aware wait registration and its two focused regression cases. Fresh hosted checks are running for this exact head.Local build/test execution of the current head is not claimed: machine
main's Prime B workspace volume is full, and deleting disposable build/probe artifacts is operation-bound-approval gated. Hosted CI and independent review are therefore the compile/runtime gates for this head.Not tested on console hardware in this turn or on macOS. The remaining console semantics for cancellation racing a targeted signal are not claimed.
Checklist
main; no open PR found implementing the remaining target-only wake behaviorSummary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-23T11:34:33Z.
Review: COMMENTED
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T11:35:15Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
0b56103a89:tools/check_conventions.pypassed.guest_cond_timedwait_testsbuilt cleanly.guest_cond_timedwait):core/libs/prx/libkernel/Pthread/src/Cond.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T12:02:17Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
cae95fc35f:tools/check_conventions.pypassed.guest_cond_timedwait_testsbuilt cleanly.guest_cond_timedwait):core/libs/prx/libkernel/Pthread/src/Cond.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by mihiii1337 on 2026-11-23T12:09:30Z.
Review: CHANGES_REQUESTED
Reviewed exact head
cae95fc35fb94a41a3b38fd6768d7add7b54e0d7. Dean's two wakeup races are fixed, but the new wait registration introduces a separate cancellation-disabled hang.WaitRegisteredalways publishes its per-waiter condition throughThreadCancel::BeginWait. When the target hasPTHREAD_CANCEL_DISABLE, a cancel notification wakes it, butThreadCancel::Requested()remains false, so the predicate wait resumes whilecancelWaitstays registered.scePthreadCancelloops until that pointer is removed. An untimed condition wait therefore makes a valid cancel request block until some unrelated condition signal arrives, even though POSIX specifies that processing the request is asynchronous topthread_cancelreturning.A native Windows A/B fixture used the exact condition/cancel helpers, real threads and mutexes, and both real
TimedWaitbackends. Across normal and recursive guest mutexes, cancel returned before the independent cleanup signal in 12/12 merge-base cases and 0/12 head cases; the disabled target remained alive and the request remained pending. The head correctly preserved targeted isolation in 4/4 cases, and ordinary-signal transfer with enabled cancellation worked in 4/4 on both revisions. A narrow control that registers the cancel wake only when cancellation is enabled restored 12/12 and preserved both positive behaviors in fine and coarse modes.Please avoid publishing
cancelWaitfor a cancellation-disabled waiter (or otherwise letscePthreadCancelreturn without waiting forEndWait) and add a regression that the cancel call returns before a separate condition signal. The fixture replaced guest-thread registry/exit with test doubles; it did not run a console. The existing exact-head Linux approval does not cover this state.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T12:17:41Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
71525e1b4f:tools/check_conventions.pypassed.guest_cond_timedwait_testsbuilt cleanly.guest_cond_timedwait):core/libs/prx/libkernel/Pthread/src/Cond.cpp,core/libs/prx/libkernel/Pthread/src/Thread.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.scePthreadCondSignaltoincore/libs/prx/libkernel/Pthread/src/Cond.cpp#L373-L382: properly checks nullresolvedand nullthread(EINVAL), verifies thread is alive viaGuestThreadIsLive(ESRCH), and checks waiter registration viaSignalTarget(EPERM).AddWaiter,SelectWaiter,SignalOne,SignalTarget, andSignalAll:SelectWaitersetssignaled = truewith release semantics while holdingWaiterMutex(), ensuring wakeups are not lost.WaitRegisteredand mutex release/re-acquire sequencing inCond.cpp#L149-L172andwaitUntil: atomic/reacquire lock order correctly synchronizes waiter registration, cancel wait registration, and timeout handling.ThreadCancel::BeginWaitincore/libs/prx/libkernel/Pthread/src/Thread.cpp#L970-L976: only publishescancelWaitwhen cancellation isGUEST_CANCEL_ENABLE, preventing cancellation hangs when cancellation is disabled.CMakeLists.txtandGuestCondTimedwait.cpp: verifiedSignalToTargetOnly,SignalToWithoutMutex, and cancellation disabled tests under normal and coarse timed wait environments.docs/dev/debt/changes/fix-libkernel-cond-signalto-target.mdcomplies with AGENTS.md conventions and accurately documents behavior and remaining gaps.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-23T12:38:00Z.
Review: APPROVED
Windows check of head
71525e1b4fwith GCC 16, merged ontomain071799dc88(clean merge). The runs so far are Linux, and the per-waiter wait goes throughTimedWait::Condition, whose Windows path differs. I also rancae95fc35fin a larger batch before the last push, and give that below too.Setup: Windows 11 Home 10.0.26200, Ryzen 7 7700X, WinLibs GCC 16.2.0 (
x86_64-ucrt-posix-seh, r3), Release full build, run from PowerShell.71525e1b4f: the condition tests (guest_cond_timedwait,guest_cond_timedwait_coarse,guest_pthread_cond*,guest_cond_recursive_mutex,guest_cond_unowned_mutex) and everyguest_pthread_cancel*test, including the 24inactive_waitvariants: 34 of 34, in 2 of 2 runs.guest_cond_timedwaitandguest_cond_timedwait_coarserun 20 times each in a row: 40 of 40 pass.Thread.cpptaken back tomain(so onlyBeginWait's change is undone),guest_cond_timedwaitandguest_cond_timedwait_coarseboth abort, so the new cancellation-disabled case guards it.cae95fc35f(earlier, in a batch with three other PRs, onmain071799dc88): the condition set is 6 of 6 onmainand 7 of 7 with the PR, andguest_cond_timedwaitpasses 30 of 30 in a row. WithCond.cppfrommain,guest_cond_timedwaitaborts, as in PrivacyOnion's Linux control.The code, mainly the races raised so far:
signaledalready set.WaitRegisteredreadssignaledunderWaiterMutex()in the wait predicate, andSelectWaiter/SignalAllset it under that same mutex. A signal that lands between the predicate and the host wait is held off until the wait has registered.WaitUntilreturns, the waiter still holdsWaiterMutex()when it erases itself. So a timed-out waiter can't be selected afterwards, and a signal that came in before it took the lock back counts as a wake, not a timeout.scePthreadCancelkeeps callingNotifyAllon the registered condition until the waiter callsEndWait, so a cancel can't be lost in the gap. I missed the disabled case mihiii1337 found oncae95fc35f.71525e1b4ffixes it wherecancelWaitis published (ThreadCancel::BeginWait), so it applies to every cancellable wait, not only condition variables. Only the thread itself changes its cancel state, and it can't do that while it waits, so the check can't race.WaiterMutex(), on both the wait and signal sides.NotifyOneruns afterWaiterMutex()is released, and theshared_ptrkeeps the waiter alive if it has already left.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by mihiii1337 on 2026-11-23T13:08:15Z.
Review: COMMENTED
I verified the original cancellation-disabled condition hang is fixed at
71525e1b4f8f9bef30b0c5fe946818fc35dea5fd. In a native Windows fixture with the extracted condition/cancel helpers and the real fine and coarseTimedWaitimplementations, cancel returned before the independent signal in 0/12 cases atcae95fc35fand 12/12 at this head. The request stayed pending and the disabled target completed its condition wait normally. The head also passed 200/200 untimed targeted wakes without a held guest mutex, both signal/timeout orders for normal and recursive mutexes on both backends, and targeted isolation and enabled-cancel signal transfer in 4/4 cases each.Dean's semaphore finding is now confirmed by the Windows job on this head.
guest_sem_canceled_wakeandguest_sem_canceled_wake_coarsewere the two failures out of 5,572 tests, both atwaiter did not enter semaphore wait.A local control keeping the previous shared
BeginWaitand skipping registration only for disabled condition waits also returned before the signal in 12/12 cases. With an exactThreadCancel::WaitUntilfixture using a token predicate, cancellation-disabled wait registration was observable on both backends in this control and the previous head, and on neither backend in the new head. An explicit post woke that fixture on all arms. The local fixture substitutes guest-thread registry and exit, so the hosted job is the integrated semaphore-test evidence. This gives a tested way to scope the fix while preserving the existing semaphore regression checks.AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-23T13:37:25Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
7806fda7e2:tools/check_conventions.pypassed.guest_cond_timedwait_testsbuilt cleanly.guest_cond_timedwait):core/libs/prx/libkernel/Pthread/include/Cancel.hpp,core/libs/prx/libkernel/Pthread/src/Cond.cpp,core/libs/prx/libkernel/Pthread/src/Thread.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by mihiii1337 on 2026-11-23T13:43:45Z.
Review: APPROVED
Both findings are resolved at
7806fda7e28ed34068c4eb5ecc98d22353b0b301. The cancellation-state check now applies insideWaitRegisteredfor condition variables, and the sharedBeginWaitagain registers semaphore waiters.I built the exact head on Windows with WinLibs GCC 15.2 Release. The 35 selected condition, semaphore and pthread cancellation CTests all passed with zero skips. That includes
guest_cond_timedwait,guest_sem_canceled_wakeand both of their_coarseentries.I also rebuilt the native fine/coarse fixture from the extracted helpers at all three revisions. At
cae95fc35f, a cancel request for a cancellation-disabled condition waiter returned before the separate signal in 0/12 cases. At71525e1b4f, that count was 12/12 but disabled shared-wait registration was lost in both modes. At this head cancel returned first in 12/12, the request stayed pending, and the shared-wait registration and explicit post worked in both modes. This head also passed 200/200 untimed targeted signals without a held guest mutex, both timeout/signal orderings, and the ordinary-signal transfer cases. The standalone fixture uses guest-thread registry and exit doubles. The 35 CTests used the built runtime. No console hardware was tested.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-23T14:55:37Z.
Review: APPROVED
Follow-up on Windows for head
7806fda7e2, with a correction to my approval of71525e1b4f.Correction: on
71525e1b4fI wrote that moving the fix intoThreadCancel::BeginWait"applies to every cancellable wait, not only condition variables", as if that were a plus. It was the problem oneandonlydean then raised, and the hosted Windows job confirmed it:guest_sem_canceled_wakeand_coarsefailed, because a semaphore waiter with cancellation disabled no longer publishedcancelWait. My run didn't include theguest_sem*tests, so it missed that. Sorry for the noise.Setup: Windows 11 Home 10.0.26200, Ryzen 7 7700X, WinLibs GCC 16.2.0 (
x86_64-ucrt-posix-seh, r3), Release full build, run from PowerShell. Merged ontomain26508fa3f9(clean merge), built together with #21948 and #26179, which touch other files.^(guest_cond.*|guest_pthread_cond.*|guest_pthread_cancel.*|guest_sem.*)$, includingguest_sem_canceled_wakeand_coarse):main39 of 39, and with this PR 40 of 40 (addingguest_cond_timedwait_coarse) in 2 of 2 runs.guest_cond_timedwait,guest_cond_timedwait_coarse,guest_sem_canceled_wakeandguest_sem_canceled_wake_coarserun 15 times each in a row: 60 of 60 pass.WaitRegisteredregistering the cancel wake even while cancellation is disabled (if (ThreadCancel::Enabled())→if (true)),guest_cond_timedwaitand_coarseabort, andguest_pthread_cancel_recoveryand_coarsetime out. The semaphore tests still pass.The code:
BeginWaitis back to registering every wait, as onmain. OnlyWaitRegisterednow skips it whileThreadCancel::Enabled()is false, so a condition wait with cancellation disabled leavescancelWaitempty andscePthreadCancelreturns at once, while semaphore waits behave as before.EndWaitstill clears the (empty) registration, which is harmless.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T10:26:51Z.
Review: COMMENTED
Reviewed — comments
The review covers head
7806fda7e28ed34068c4eb5ecc98d22353b0b301; the changed files were read, but no checks were run and console behavior remains partly unverified.What I ran
No commands or tests were run. I reviewed the prepared diff and the repository files at the pinned head.
Results
Findings
Blocking
None.
Non-blocking
readybefore signaling, but that increment occurs before either worker enters its condition wait (core/libs/tests/GuestCondTimedwait.cpp:231-232). If scheduling pauses a worker between those operations, the test may signal before that worker registers and then wait for a timeout. Consider synchronizing on actual waiter registration or using a longer deadline and explicit readiness protocol.scePthreadJoin(state.target, nullptr)if the target fails to return after the signal, despite the one-second check only bounding the wait forcancelReturned(core/libs/tests/GuestCondTimedwait.cpp:325-333). Consider bounding the overall regression with a test-level timeout or timed join.Not verified
core/libs/prx/libkernel/CMakeLists.txt,core/libs/prx/libkernel/Pthread/include/Cancel.hpp,core/libs/prx/libkernel/Pthread/src/Cond.cpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/tests/GuestCondTimedwait.cpp, anddocs/dev/debt/changes/fix-libkernel-cond-signalto-target.md. The manifest lists all six as included; no changed files were excluded.To get this to a good state
Nothing is required. The optional test synchronization and timeout improvements above would make the regression tests more robust.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-27T16:40:17Z.
Review: COMMENTED
Actionable comments posted: 2
a696151f-ef27-410f-876f-87f5c40fbc60Reviewing files that changed from the base of the PR and between
1bc92752d2andefef1bbc92.core/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/Pthread/include/Cancel.hppcore/libs/prx/libkernel/Pthread/src/Cond.cppcore/libs/prx/libkernel/Pthread/src/Thread.cppcore/libs/tests/GuestCondTimedwait.cppdocs/dev/debt/changes/fix-libkernel-cond-signalto-target.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-27T16:46:16Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this change; the remaining points below are optional.
What I ran
Reviewed head
efef1bbc92f9ada9945059d4a4a0a0fe9c7a5196using the prepared diff and the pinned reader. No commands or tests were run.Results
Findings
Blocking
None.
Non-blocking
Not verified
No tests were run. Console behavior and macOS behavior were not verified. Read:
description.md,commits.md,manifest.json,diff.patch, and all six changed files listed in the manifest:core/libs/prx/libkernel/CMakeLists.txt,core/libs/prx/libkernel/Pthread/include/Cancel.hpp,core/libs/prx/libkernel/Pthread/src/Cond.cpp,core/libs/prx/libkernel/Pthread/src/Thread.cpp,core/libs/tests/GuestCondTimedwait.cpp, anddocs/dev/debt/changes/fix-libkernel-cond-signalto-target.md. No changed files were excluded by the manifest.To get this to a good state
Nothing is required. Optional: add the current-head hosted check results when available, and record console measurements for the two behaviors noted in the debt entry.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T06:38:49Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change based on the source review; hosted checks for this exact head were still running according to the description.
What I ran
Reviewed head
9ed235419cf1420a7d59fab6bd7976c05d62f5cc. Read the prepared description, commits, diff, manifest, README, and all six changed files; inspected cancellation registration and cancellation callers inThread.cpp. No build or tests were run.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No build, test, negative control, or console run was performed in this review. The description reports pending hosted checks for this head and a prior firmware 13.60 measurement; neither was independently confirmed here. All six changed files listed in the manifest were read; no changed files were excluded.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T06:39:07Z.
Review: COMMENTED
Architecture findings (1)
Advisory architecture findings (1)
9688f3c2-ccd4-4fbd-a93e-61a93589c0b9Reviewing files that changed from the base of the PR and between
efef1bbc92and9ed235419c.core/libs/prx/libkernel/Pthread/src/Cond.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T10:50:16Z.
Review: COMMENTED
Architecture findings (1)
Advisory architecture findings (1)
f7b7b70d-7bcd-44ab-81b8-0e246ca3c595Reviewing files that changed from the base of the PR and between
9ed235419cand985664e2eb.core/libs/prx/libkernel/Pthread/src/Thread.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T10:38:18Z.
Review: COMMENTED
The GuestCondTimedwait fixture coverage passed on Linux with the exact implementation from this head. The fixture covers targeted condition-variable wakeups alongside the existing wait cases.
AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.