fix(libkernel): avoid cancellation wakeups during thread exit #10856
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10856"
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 Felipebmaitan on 2026-10-25T05:36:24Z.
What
scePthreadCancelrepeatedly notifies a registered wait until the target unregisters it. A thread already exiting can enter that wait from a cleanup handler or a thread-specific destructor, whileThreadCancel::Requested()excludes it from cancellation. If the callback needs progress from the requester, waiting for acknowledgement prevents that progress.Record the wait pointer and a terminal-exit marker together under
cancelLock. A cancellation request still becomes pending, but returns without notifying a wait registered during exit. Ordinary notification or timeout completes that wait, andEndWaitclears both fields before reacquiring the application lock. Non-exiting waits, including disabled-to-enabled cancellation transitions, retain main's behavior. No exported signatures change.The 24 independent regressions gate the actual production
ThreadCancel::Waittemplate during explicit-exit cleanup, explicit-exit TSD destruction, or TSD destruction after the entry returns naturally. Three SCE or POSIX cancellation requests must return before the target gate is released or an ordinary notification is sent. A failed observation exits unsuccessfully without releasing the gate, so recovery cannot manufacture a pass. Each lifecycle trigger covers timed/untimed waits, both request APIs, and normal/coarse backends. Completion preserves the exit result and cleanup-before-TSD ordering. The natural-return mode pushes no cleanup handler, requires zero cleanup calls and one destructor, and preserves the entry's return value; it specifically coversfinishThreadsetting the exit marker.The component debt records remaining non-exit acknowledgement dependencies, nested-registration loss, allocation-failure cleanup, and skipped unwinding. This is a host progress repair, with no new PS5 measurement. The gates do not inject an exception handler or establish general signal/cancellation safety.
Overlap: #7565 adds
hostMxcsrat the same header insertion point; any merge must retain both it andcancelWaitExiting. #7135 and #8916 touch adjacent debt text; preserve their entries too. None is a semantic prerequisite. The overlap tool also lists older PRs whose shared files could not be compared because they conflict with main; no exhaustive composition validation or merge-order agreement is claimed. These textual overlaps still need coordination before merge.Tested
Windows 11 Pro, AMD Ryzen 7 9800X3D, WinLibs MinGW-w64 GCC 15.2.0 r7 UCRT/POSIX, Release,
BUILD_TESTING=ON, bundled dependencies. Full CMake configuration; selected libc/libkernel and test targets built from source with--parallel 2in this branch's own build directory.e7e3e9c1984c293f06bff08428b8c89b7b932ac2. Current main inspected for this follow-up:b986c11a344eb094eec31cbc3977fe2ae8f6eb30. The PR remains cleanly mergeable; no current-main composition run is claimed.64ad39e457b3ba7ecf86a2c17a26b4849d03bcc0contains only the new test and its registrations, with main's production code. All 16/16 new cases fail at the cancellation-return gate before release (ctestexit 8); the five existing cancellation/semaphore controls pass 5/5.84340f6d6391eb4e60d61a864f35a261ec1477e0: focused runtime selection passes 39/39, including all 16 new cases, with no skips. Original production headf68eb887c91aaf20767ef327f367b9f21b264383only adds a debt clarification;git diff --exit-code 84340f6d6391 f68eb887c91a -- coreis empty.f68eb887c91afound no scoped blocking defect. Their static/source and log review is separate from the runtime executions above.f75e51c5d417080a228b6c8a5782577eba0b78ebadds eight natural-return cases and changes only the fixture and CMake registrations. The production Pthread sources and headers are unchanged fromf68eb887c91a. All 47/47 focused Windows runtime cases pass with no skips (10.16 seconds), including all 24 teardown gates. No public-header rerun was needed for this test-only follow-up; the 65/65 result above belongs to the original artifact.BeginWaitandscePthreadCanceldefinitions from current mainb986c11a344einto this PR fixture. The new natural-return cases fail 8/8 at the cancellation-return observation, before gate release or normal notification; five existing cancel/semaphore controls pass 5/5. The relevant wait templates,Requested,EndWait, andfinishThreadmatch that main head. This is selected-path evidence, not a build of the whole current-main tree.CurrentGuestState().exiting = truefromfinishThreadin the repaired production source. The eight natural-return cases fail 8/8 at the same observation, while the 16 explicit-exit cases still pass 16/16. Both controls terminate unsuccessfully within the two-second observation bound; no CTest timeout, gate release, or cleanup recovery supplies the result. Production source was restored byte-for-byte and rebuilt before the 47/47 run.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T05:39:47Z.
Review: APPROVED
Reviewed exact head
f68eb887c91aaf20767ef327f367b9f21b264383against current maine7e3e9c1984c293f06bff08428b8c89b7b932ac2. The exit marker is set before cleanup handlers and TSD destructors run;BeginWaitpublishes the wait pointer and whether it belongs to exit undercancelLock, andscePthreadCancelchecks both under that same lock before notifying. This closes the requester-waits-for-exiting-callback deadlock while preserving repeated notification for ordinary registered waits. The POSIX entry delegates to the same implementation. The new tests gate the actual wait template in cleanup and destructor paths across SCE/POSIX APIs, timed/untimed waits, and normal/coarse backends, and require the cancel requester to return before the gate is released. The author reports 16/16 new negative-control failures on main, 39/39 focused runtime tests on the fix, and a successful 65/65 public-header retry; I did not rerun the Windows runtime suite locally. I independently ran the current-main conventions checker andgit diff --checkon the exact head; both pass. GitHub policy checks pass; hosted product jobs are skipped. I found no blocking issue in this change.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T11:56:56Z.
Review: APPROVED
Reviewed head
f68eb887c91amerged onto maina5a780eaaa7f(PR basee7e3e9c1984c). Linux: WSL2 Ubuntu 24.04, GCC 13.3, Release,BUILD_TESTING=ON. I think this is the first Linux run.What I ran:
--repeat until-fail:50.Thread.cppandPthread.hpp: all 16 new cases fail at thecancelReturnedAwait (line 25; a temporary print confirmed the gate Await passes first). The 13 controls pass.tools/check_conventions.py --base origin/mainpasses on the merged tree.One note on the Linux red. Off Windows
Coarse()is always true, soNotifyAlliscondition_variable_any::notify_all. While the gate parks inside the CV's unlock, the waiter holds the CV's internal mutex, so on Linux main's cancel blocks insideNotifyAllinstead of spinning. To check the spin itself I ran a throwaway probe without the gate: the exiting thread waits inThreadCancel::Wait(cleanup handler or TSD destructor) for a flag that the main thread sets only after 3 cancels return.FreeBSD 11.4 agrees.
_pthread_exit_masksetscancellingandno_cancelbefore cleanup handlers and TSD destructors (thr_exit.c L221-L222), andcheck_cancelreturns early onno_cancel(thr_sig.c L281-L283)._pthread_cancelonly setscancel_pending, sends SIGCANCEL and returns; it never waits for the target (thr_cancel.c L55-L75).Two suggestions, neither blocking:
scePthreadExit. A TSD destructor that waits after the entry returns normally only getsexitingfromfinishThread(Thread.cpp L701), and no case covers it. I tried it with your gate andWorkerdoingreturn &context;without the cleanup push: main fails 3/3, this PR passes 3/3. One more argv mode like that would pin it.Requested()already ignores exiting threads, soBeginWaitcan skip the registration:state.current->cancelWait = state.exiting ? nullptr : condition;, andscePthreadCancelkeeps main'sif (!thread->cancelWait) return SCE_OK;. That also drops thePthread.hppline that overlaps #7565. I ran that variant locally: the 16 new cases and 17 neighbouring controls pass (33/33).Approving on the above. Linux only; no console or title run.