fix(libkernel): reject exception redirects during thread teardown #1802
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1802"
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 Sanjaysrinivas on 2026-10-08T19:32:31Z.
What
Fix two Windows exception/exit races from #1668. A target can finish after the initial
sceKernelRaiseExceptioncheck but before suspension takes effect. Redirecting it after guest-thread unregistration makes the handler'sscePthreadSelf()adopt a different handle. A terminating native thread can also reject suspension before its Windows handle is signaled, causing a runtime exception instead ofESRCH.The Windows path now checks guest completion as well as native exit, rechecks
_finishedafterGetThreadContexthas stopped the target, and treats context-read failures during completion asESRCH. Other host API failures retain their diagnostics.Add
guest_exception_wait_recoverywith Release-active requirements, atomic gates, five-second progress checks and a 20-second CTest timeout. Each run covers:The address-wait tests include the wait-entry race. Publishing a changed value before wake makes both registration orderings valid; these tests do not claim every raise interrupts an already enrolled host wait. Independent raiser/waker threads let the coordinator release a held handler without waiting on host locks it may retain. The raiser is a guest thread and is joined, avoiding leaked adopted host-thread handles.
Update the existing architecture documentation with the delivery flow, rejection ordering, wait invariants, test matrix and support boundaries. Update TechnicalDebt for the remaining teardown and host-lock limits.
Dependencies and coordination
Depends on #1431 at
77143010efe1555449b6f11ee6a91b1517fd4b6dfor Linux delivery.Depends on #1421 at
24dc98d7c035bde08e679e3c62a59402a2b57cc5for Windows delivery robustness. The new tests exposed MinGW TLS failures without it.Based on upstream
mainat2cc7f09d1d3c90fa3daf4ba2c5b170b036361770. Both prerequisite histories and authors are preserved through merges. Their test-file conflict is resolved by retaining Linux register/startup coverage, guarding Windows-only SEH/continuation cases, and correcting delivery-count offsets for the combined phases. The Linux MXCSR handling and Windows retry/context changes are both retained. The final main refresh adds upstream image-by shader support and leaves the tested kernel source unchanged.The new change is commit
694a126b, five files. #1562 also changes Windows delivery; its alternative implementation is not stacked here. #1652 changes signal-mask handling, which this follow-up does not implement. #1431's current published head has two commits; this PR does not assume the later mask work described in its body is present.The overlap bot also flags #1746, which replaces the Linux thread-start promise and preserves configured stack bounds. Its overlap is with startup code included from #1431, not the new Windows teardown guard. Neither #1562 nor #1746 is an additional prerequisite for this PR. Whichever merges second must reconcile the shared code: retain Linux signal masking and native-handle publication alongside #1746's startup changes, and preserve the finished-target rejection if Windows delivery is replaced by #1562. Rerun exception, thread creation and stack tests after integration.
Related cancellation coverage is in #1739. This PR addresses part of workstream B and leaves #1668 open.
Tested
Local Release builds use the production libc/libkernel CMake source lists and test registrations through a small untracked validation parent. They do not replace production functions or build unrelated graphics/relinker dependencies.
That is 6,000 address-wait cases and 6,400 exit races per host. The existing
guest_exception_handler,guest_raise_exceptionandguest_sync_on_addresstests also pass three repetitions per host. No title, console or GPU behavior was tested.Full GitHub CI passes on combined head
0623b090f8955b68a8e481a6df4cf42bda82b5c4: Linux, Windows, conventions, and progress report. The relinker job is skipped by the path filter.Before the new guard, the regression failed the target-handle identity requirement. With only the post-suspension check, further stress reproduced
cannot suspend the target threadduring teardown; consulting guest completion in failure paths fixes that window too.Two temporary Linux mutations were caught: removing address-wake notification failed bounded progress; forcing successful address-wait returns failed the timeout requirement. Production sources were restored byte-for-byte and ten restored runs passed. Conventions, whitespace and native test-registration checks pass.
Standard checkout reproduction:
Remaining limits and reproduced evidence
#1488's host-lock mechanism is reproduced on Windows. A preliminary version kept the handler waiting while the coordinator synchronously called
sceKernelSyncOnAddressWake. GDB showed the target paused inHandler, interrupted beneathRtlAllocateHeapduring address-wait enqueue, and the coordinator blocked in winpthreads fromsceKernelSyncOnAddressWakeon the address-queue mutex. The handler's release depended on that wake returning. This is documented, not fixed by the teardown guard; a general host-critical-section deferral design is still needed.Windows coarse address-wait/delivery races also failed to complete reliably in the preliminary matrix. Their root cause remains unresolved. This CTest explicitly unsets
APS5_COARSE_TIMED_WAITSon Windows and claims default alertable-wait coverage only. Linux uses its existing condition-variable path.Signal masks, teardown delivery-completion guarantees, stale non-null handles, full console context equivalence and Windows upper-YMM preservation remain unverified or unsupported. Accepted raises are not counted as executed handlers during exit races.
Checklist
main; checked overlapping work and explicitly listed prerequisites.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T19:32:55Z.
Progress report (
2cc7f09→0623b09)📈 System libraries: 84.79% (+0.03%, +1 implemented functions)
✅ 1 implemented
sceKernelRaiseExceptionImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T20:18:40Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libkernel/Pthread/include/Pthread.hppL14-15, L82core/libs/prx/libkernel/Pthread/src/Thread.cppL20-21, L317-335core/libs/prx/libkernel/CMakeLists.txtL292-321core/libs/prx/libkernel/Pthread/src/Thread.cppL180-181, L420-421core/libs/prx/libkernel/System/src/Exception.cppL5-267, L272-275, L280-282, L287-296core/libs/tests/GuestRaiseException.cppL1-297docs/dev/ARCHITECTURE.mdL1-69docs/dev/TechnicalDebt.mdL239-254core/libs/tests/GuestRaiseException.cppL283Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T08:55:35Z.
Closing: no title hits this teardown race, and the branch merges #1421 and #1431 in instead of building on main. If a title hits it once those land, please reopen rebased on main, without merge commits or the ARCHITECTURE.md section.
Pull request closed