fix(libkernel): refuse to detach or rejoin a thread that is being joined #6487

Merged
klosixgaming-arch merged 1 commit from upstream-pr/6487 into main 2026-10-19 02:03:57 +00:00
klosixgaming-arch commented 2026-10-15 17:09:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-15T17:09:13Z.


What

EA SPORTS FC 27 (PPSA34015) stopped once on Windows with:

terminate called after throwing an instance of 'std::system_error'
  what():  Joining guest thread: Access is denied
FATAL: abort() (SIGABRT)

The join comes from the title's thread wrapper (eboot 0x1d4b365, scePthreadJoin(handle, NULL) after its status poll). scePthreadJoin waits for _finished and then for the native handle. If another call releases the thread in the meantime, the join waits on a closed handle in freed memory. That other call is a scePthreadDetach, or a second scePthreadJoin whose ReleaseThread frees the thread. WaitForSingleObject then fails with ERROR_ACCESS_DENIED.

FreeBSD 11.4 libthr keeps a joiner field for this case (thr_join.c, thr_detach.c). This PR follows it, under _join_mtx:

  • scePthreadDetach returns EINVAL while a thread waits to join the target.
  • A second concurrent scePthreadJoin returns EOPNOTSUPP (ENOTSUP, 45), as join_common does.
  • A join cancelled while it waits clears the joiner before it exits (backout_join).
  • A join that completes sets _joined under the same lock, so a detach racing with its end returns EINVAL instead of releasing the thread a second time.

The libkernel debt entry for scePthreadJoin records this. main now has the _joined flag (second join after a completed one → ESRCH); this PR keeps it and adds the lock, so a detach cannot close the handle while a joiner waits.

Tested

  • New guest_pthread_join_detach:
    • A detach while a joiner waits returns EINVAL.
    • A second joiner gets EOPNOTSUPP.
    • The first join completes with the return value.
    • Detach then join returns EINVAL.
  • It passes on Linux (GCC 13.3, 20 consecutive runs) and on Windows 11 (WinLibs GCC 15.2.0). guest_pthread_start still passes on both.
  • Rebased onto main 66702f1cff: guest_pthread_join_detach passes again on Windows 11. The Linux runs and guest_pthread_start above are from before the rebase.
  • Not confirmed in the title. The stop above happened once. Since then PPSA34015 stops earlier in every run, at a pixel shader (v_readlane_b32 of lane 63 is outside the 32-lane host subgroup, see #6344), so I could not run it to the point of this race again. A run with this change and a log on the refused paths reached that earlier stop without the join error, which does not prove much either way.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: PPSA34015 #6344
  • GPUs tested: n/a
  • AI-assisted: yes. Written with AI assistance and reviewed by me.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4877049819 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6487). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-15T17:09:13Z. --- ### What EA SPORTS FC 27 (PPSA34015) stopped once on Windows with: ``` terminate called after throwing an instance of 'std::system_error' what(): Joining guest thread: Access is denied FATAL: abort() (SIGABRT) ``` The join comes from the title's thread wrapper (eboot `0x1d4b365`, `scePthreadJoin(handle, NULL)` after its status poll). `scePthreadJoin` waits for `_finished` and then for the native handle. If another call releases the thread in the meantime, the join waits on a closed handle in freed memory. That other call is a `scePthreadDetach`, or a second `scePthreadJoin` whose `ReleaseThread` frees the thread. `WaitForSingleObject` then fails with `ERROR_ACCESS_DENIED`. FreeBSD 11.4 libthr keeps a `joiner` field for this case ([thr_join.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_join.c), [thr_detach.c](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libthr/thread/thr_detach.c)). This PR follows it, under `_join_mtx`: - `scePthreadDetach` returns `EINVAL` while a thread waits to join the target. - A second concurrent `scePthreadJoin` returns `EOPNOTSUPP` (`ENOTSUP`, 45), as `join_common` does. - A join cancelled while it waits clears the joiner before it exits (`backout_join`). - A join that completes sets `_joined` under the same lock, so a detach racing with its end returns `EINVAL` instead of releasing the thread a second time. The libkernel debt entry for `scePthreadJoin` records this. `main` now has the `_joined` flag (second join after a completed one → `ESRCH`); this PR keeps it and adds the lock, so a detach cannot close the handle while a joiner waits. ### Tested - New `guest_pthread_join_detach`: - A detach while a joiner waits returns `EINVAL`. - A second joiner gets `EOPNOTSUPP`. - The first join completes with the return value. - Detach then join returns `EINVAL`. - It passes on Linux (GCC 13.3, 20 consecutive runs) and on Windows 11 (WinLibs GCC 15.2.0). `guest_pthread_start` still passes on both. - Rebased onto `main` 66702f1cff: `guest_pthread_join_detach` passes again on Windows 11. The Linux runs and `guest_pthread_start` above are from before the rebase. - **Not confirmed in the title.** The stop above happened once. Since then PPSA34015 stops earlier in every run, at a pixel shader (`v_readlane_b32 of lane 63 is outside the 32-lane host subgroup`, see #6344), so I could not run it to the point of this race again. A run with this change and a log on the refused paths reached that earlier stop without the join error, which does not prove much either way. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA34015 #6344 - [x] GPUs tested: n/a - [x] AI-assisted: yes. Written with AI assistance and reviewed by me.
github-actions[bot] commented 2026-10-15 17:29:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T17:29:23Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 3986 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 4972 core/libs/prx/libkernel/CMakeLists.txt
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/prx/libkernel/CMakeLists.txt
PR 6149 docs/dev/debt/libkernel.md
PR 6226 core/libs/prx/libkernel/CMakeLists.txt
PR 6446 docs/dev/debt/libkernel.md
PR 6510 docs/dev/debt/libkernel.md
PR 6519 docs/dev/debt/libkernel.md
PR 6534 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 6879 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 6908 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6942 core/libs/prx/libkernel/CMakeLists.txt
PR 6977 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7039 docs/dev/debt/libkernel.md
PR 7049 docs/dev/debt/libkernel.md
PR 7075 docs/dev/debt/libkernel.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7129 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7149 core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 7332 core/libs/prx/libkernel/CMakeLists.txt
core/libs/prx/libkernel/Pthread/include/Pthread.hpp
core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 7450 core/libs/prx/libkernel/CMakeLists.txt
PR 7509 docs/dev/debt/libkernel.md
PR 7565 core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 7653 docs/dev/debt/libkernel.md
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6198203039 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6487#issuecomment-6198203039). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T17:29:23Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 3986](https://www.github.com/boykopovar/AnyPS5/pull/3986) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 4972](https://www.github.com/boykopovar/AnyPS5/pull/4972) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/include/Pthread.hpp`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6149](https://www.github.com/boykopovar/AnyPS5/pull/6149) | `docs/dev/debt/libkernel.md` | | [PR 6226](https://www.github.com/boykopovar/AnyPS5/pull/6226) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6446](https://www.github.com/boykopovar/AnyPS5/pull/6446) | `docs/dev/debt/libkernel.md` | | [PR 6510](https://www.github.com/boykopovar/AnyPS5/pull/6510) | `docs/dev/debt/libkernel.md` | | [PR 6519](https://www.github.com/boykopovar/AnyPS5/pull/6519) | `docs/dev/debt/libkernel.md` | | [PR 6534](https://www.github.com/boykopovar/AnyPS5/pull/6534) | `core/libs/prx/libkernel/Pthread/include/Pthread.hpp`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 6908](https://www.github.com/boykopovar/AnyPS5/pull/6908) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 6936](https://www.github.com/boykopovar/AnyPS5/pull/6936) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 6942](https://www.github.com/boykopovar/AnyPS5/pull/6942) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6977](https://www.github.com/boykopovar/AnyPS5/pull/6977) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 7039](https://www.github.com/boykopovar/AnyPS5/pull/7039) | `docs/dev/debt/libkernel.md` | | [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) | `docs/dev/debt/libkernel.md` | | [PR 7075](https://www.github.com/boykopovar/AnyPS5/pull/7075) | `docs/dev/debt/libkernel.md` | | [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `docs/dev/debt/libkernel.md` | | [PR 7105](https://www.github.com/boykopovar/AnyPS5/pull/7105) | `docs/dev/debt/libkernel.md` | | [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) | `docs/dev/debt/libkernel.md` | | [PR 7129](https://www.github.com/boykopovar/AnyPS5/pull/7129) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 7149](https://www.github.com/boykopovar/AnyPS5/pull/7149) | `core/libs/prx/libkernel/Pthread/include/Pthread.hpp`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7192](https://www.github.com/boykopovar/AnyPS5/pull/7192) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`docs/dev/debt/libkernel.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp` | | [PR 7332](https://www.github.com/boykopovar/AnyPS5/pull/7332) | `core/libs/prx/libkernel/CMakeLists.txt`<br>`core/libs/prx/libkernel/Pthread/include/Pthread.hpp`<br>`core/libs/prx/libkernel/Pthread/src/Thread.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7450](https://www.github.com/boykopovar/AnyPS5/pull/7450) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 7509](https://www.github.com/boykopovar/AnyPS5/pull/7509) | `docs/dev/debt/libkernel.md` | | [PR 7565](https://www.github.com/boykopovar/AnyPS5/pull/7565) | `core/libs/prx/libkernel/Pthread/src/Thread.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7653](https://www.github.com/boykopovar/AnyPS5/pull/7653) | `docs/dev/debt/libkernel.md` |
klosixgaming-arch commented 2026-10-15 19:17:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-15T19:17:33Z.


No dependency: this PR builds on neither of them.

  • #4481 makes a second join, after a completed one, return ESRCH. It does that by keeping the joined thread alive and checking _joined.
  • #4533 makes pthread_getname_np on a joined thread return ESRCH, on top of the same _joined state.
  • This PR covers a join or detach that runs while another join is waiting. It applies FreeBSD's joiner rule under _join_mtx.

They edit the same lines in scePthreadJoin and scePthreadDetach, so whichever merges second needs a small rebase. If #4481 lands first, its _joined early return can sit before the _join_mtx block here unchanged. The remaining PRs in the list are the AOT stack and CMake-only overlaps.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6200321212 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6487#issuecomment-6200321212). Originally posted by [klosixgaming-arch](https://github.com/klosixgaming-arch) on 2026-10-15T19:17:33Z. --- No dependency: this PR builds on neither of them. - #4481 makes a second join, after a completed one, return `ESRCH`. It does that by keeping the joined thread alive and checking `_joined`. - #4533 makes `pthread_getname_np` on a joined thread return `ESRCH`, on top of the same `_joined` state. - This PR covers a join or detach that runs *while* another join is waiting. It applies FreeBSD's `joiner` rule under `_join_mtx`. They edit the same lines in `scePthreadJoin` and `scePthreadDetach`, so whichever merges second needs a small rebase. If #4481 lands first, its `_joined` early return can sit before the `_join_mtx` block here unchanged. The remaining PRs in the list are the AOT stack and CMake-only overlaps.
blawesom commented 2026-10-17 14:31:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by blawesom on 2026-10-17T14:31:44Z.


Rebase handoff, with one contract repair — rebased onto current main (authorship unchanged). The conflicts were main's newer atomic thread state (std::atomic<bool> _joined, _handleStorage) meeting this PR's mutex-guarded _joining flag: the merged version keeps main's atomics plus your _joining flag under _join_mtx, and the detach check is the union (detached, joining, or already joined).

The repair: your join completion set _detached = true, which made a second scePthreadJoin return EINVAL where main's guest_pthread_second_join requires ESRCH. The join no longer marks the thread detached — the detach-after-join refusal your change is after is already covered by the _joined atomic in the detach path, and that test passes.

Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, AMD RX 9070 XT / RADV):

  • builds; tools/check_conventions.py --base origin/main passes
  • full suite: green apart from the known main regression (agc_driver_tessellation segfault, #4727), not this PR

To pick it up:

git fetch https://github.com/blawesom/AnyPS5 rebase/6487
git checkout <your branch>
git merge --ff-only FETCH_HEAD

Head is 785ae9120.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6235117438 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6487#issuecomment-6235117438). Originally posted by [blawesom](https://github.com/blawesom) on 2026-10-17T14:31:44Z. --- **Rebase handoff, with one contract repair** — rebased onto current `main` (authorship unchanged). The conflicts were `main`'s newer atomic thread state (`std::atomic<bool> _joined`, `_handleStorage`) meeting this PR's mutex-guarded `_joining` flag: the merged version keeps `main`'s atomics plus your `_joining` flag under `_join_mtx`, and the detach check is the union (detached, joining, or already joined). **The repair:** your join completion set `_detached = true`, which made a **second** `scePthreadJoin` return `EINVAL` where `main`'s `guest_pthread_second_join` requires **`ESRCH`**. The join no longer marks the thread detached — the detach-after-join refusal your change is after is already covered by the `_joined` atomic in the detach path, and that test passes. Verified on Linux (CachyOS, kernel 7.2.9, GCC 16.2.1, Ninja, Release, `BUILD_TESTING=ON`, AMD RX 9070 XT / RADV): - builds; `tools/check_conventions.py --base origin/main` passes - **full suite: green apart from the known `main` regression** (`agc_driver_tessellation` segfault, #4727), not this PR **To pick it up:** ``` git fetch https://github.com/blawesom/AnyPS5 rebase/6487 git checkout <your branch> git merge --ff-only FETCH_HEAD ``` Head is `785ae9120`.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!6487
No description provided.