libkernel: scePthreadJoin on self and scePthreadJoin/Detach on a null thread throw instead of returning EDEADLK/EINVAL; fix branch ready #4052

Closed
opened 2026-10-11 22:14:35 +00:00 by husseinkad · 1 comment
husseinkad commented 2026-10-11 22:14:35 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-11T22:14:35Z.


scePthreadJoin throws std::runtime_error (which ends the process) when a joinable thread joins itself, and scePthreadJoin and scePthreadDetach throw the same way on a null thread. FreeBSD 11.4 libthr returns EDEADLK for a self-join (join_common) and EINVAL for a null thread in both calls (_pthread_detach). A guest that probes these calls for an error code is terminated instead.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-join-self-edeadlk (one commit on current main, Pthread/src/Thread.cpp and tests/GuestThreadSelf.cpp).

Changed return values versus main (each was a thrown std::runtime_error): scePthreadJoin(null) and pthread_join(null) now return EINVAL; scePthreadDetach(null) and pthread_detach(null) now return EINVAL; scePthreadJoin(self) on a created joinable thread now returns EDEADLK (11 for POSIX). Callers (JobManager, Ult, Ampr) all compare against 0, and ToErrno maps EDEADLK to 11.

Checks:

  • The new guest_thread_self cases fail on main (abort) and pass with the change; guest_ampr_threads and guest_pthread_cancel pass.
  • Two independent reviews found nothing wrong with the change itself. check_conventions.py is clean and pr_overlap.py finds no overlap.

Not fixed here: the check order differs from FreeBSD, which tests null, then self (EDEADLK), then detached (EINVAL). Here the detached check runs first, so a self-join on a created detached thread or on the adopted main thread still returns EINVAL (an existing test asserts that for the main thread); a join after scePthreadDetach(self) also returns EINVAL. Joining an already joined thread aborts with "handle invalid" and concurrent joins double-release, where FreeBSD returns ESRCH; both are the same on main. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5805730039 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4052). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-11T22:14:35Z. --- `scePthreadJoin` throws `std::runtime_error` (which ends the process) when a joinable thread joins itself, and `scePthreadJoin` and `scePthreadDetach` throw the same way on a null thread. FreeBSD 11.4 libthr returns `EDEADLK` for a self-join (`join_common`) and `EINVAL` for a null thread in both calls (`_pthread_detach`). A guest that probes these calls for an error code is terminated instead. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-join-self-edeadlk (one commit on current main, `Pthread/src/Thread.cpp` and `tests/GuestThreadSelf.cpp`). Changed return values versus main (each was a thrown `std::runtime_error`): `scePthreadJoin(null)` and `pthread_join(null)` now return `EINVAL`; `scePthreadDetach(null)` and `pthread_detach(null)` now return `EINVAL`; `scePthreadJoin(self)` on a created joinable thread now returns `EDEADLK` (11 for POSIX). Callers (`JobManager`, `Ult`, `Ampr`) all compare against 0, and `ToErrno` maps `EDEADLK` to 11. Checks: - The new `guest_thread_self` cases fail on main (abort) and pass with the change; `guest_ampr_threads` and `guest_pthread_cancel` pass. - Two independent reviews found nothing wrong with the change itself. `check_conventions.py` is clean and `pr_overlap.py` finds no overlap. Not fixed here: the check order differs from FreeBSD, which tests null, then self (`EDEADLK`), then detached (`EINVAL`). Here the detached check runs first, so a self-join on a created detached thread or on the adopted main thread still returns `EINVAL` (an existing test asserts that for the main thread); a join after `scePthreadDetach(self)` also returns `EINVAL`. Joining an already joined thread aborts with "handle invalid" and concurrent joins double-release, where FreeBSD returns `ESRCH`; both are the same on main. Only Windows was built and run. AI-assisted: yes
oneandonlydean commented 2026-10-13 02:07:37 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T02:07:37Z.


Closing: fixed by #4063.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6136609288 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4052#issuecomment-6136609288). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T02:07:37Z. --- Closing: fixed by #4063.
Sign in to join this conversation.
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#4052
No description provided.