fix(libc): run std::_Execute_once callbacks under a per-flag lock #5027

Closed
0xRickx wants to merge 1 commit from upstream-pr/5027 into main
0xRickx commented 2026-10-13 08:52:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 0xRickx on 2026-10-13T08:52:41Z.


What

libc's guest export of Dinkumware's std::_Execute_once, which std::call_once uses, held one process-wide std::recursive_mutex while the callback ran. Every call_once in the process was serialized behind it, so a callback that waited for another thread running call_once on a different flag deadlocked, or its timed wait expired.

[thread.once.callonce] only orders executions on the same once_flag, and MSVC's STL implements Dinkumware's _Execute_once per flag with InitOnceExecuteOnce, whose callback type is this one. Callers now wait only while their own flag is being initialized. The callback runs with no lock held, and a guard publishes the flag and wakes the waiters on every exit. A callback that returns 0 or throws leaves the flag at 0, so the next caller runs it, as before; the flag word still only holds 0 or 1. A recursive call on the flag the calling thread is initializing now throws std::runtime_error: the recursive mutex let the callback run a second time, where C++ deadlocks.

A callback that ends its thread (scePthreadExit) skips the guard, so later callers of that one flag wait forever. Before this change every call_once in the process did.

Tested

Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release:

  • guest_execute_once gains three cases:

    • a callback that waits up to 5 s for another thread initializing a different flag; without the fix the wait times out and the test aborts after about 5 s;
    • a recursive call on the same flag, which throws, leaves the flag at 0 and lets a later call succeed;
    • 8 threads racing on a flag whose first run throws: exactly one throw, one successful retry, 7 calls returning 1.

    The 16-thread run-once check is unchanged.

  • guest_execute_once, guest_std_execute_once and guest_once pass with --repeat until-fail:5 on main 4bb2c7f3.

  • Full ctest on main ad653c9b: 1044/1045 passed (8 skipped on this machine). The one failure, patched_libraries, is main's libkernel_sys.prx load failure on Windows, fixed by #5025.

  • tools/check_conventions.py --base upstream/main passes.

Rebased onto main ffa2a6ec without conflicts (head 684dd88f), then tested again on main c496fd53:

  • Windows (same toolchain), with my eight other open PRs merged as well: guest_execute_once, guest_std_execute_once and guest_once pass. Full ctest: 1088/1088 passed (8 skipped on this machine).
  • Linux (Ubuntu 24.04 in WSL, GCC 13.3, Release, the CI's configure options, lavapipe):
    • this branch alone: guest_execute_once, guest_std_execute_once and guest_once pass.
    • full ctest with my eight other open PRs merged as well: 1067/1070 passed. guest_math, agc_driver_depth_compare_copy and guest_socket_read_write fail the same way on main b8683834 without any of them in this setup.

No title was run.

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: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4840779089 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5027). Originally posted by [0xRickx](https://github.com/0xRickx) on 2026-10-13T08:52:41Z. --- ### What libc's guest export of Dinkumware's `std::_Execute_once`, which `std::call_once` uses, held one process-wide `std::recursive_mutex` while the callback ran. Every `call_once` in the process was serialized behind it, so a callback that waited for another thread running `call_once` on a different flag deadlocked, or its timed wait expired. [thread.once.callonce] only orders executions on the same `once_flag`, and MSVC's STL implements Dinkumware's `_Execute_once` per flag with `InitOnceExecuteOnce`, whose callback type is this one. Callers now wait only while their own flag is being initialized. The callback runs with no lock held, and a guard publishes the flag and wakes the waiters on every exit. A callback that returns 0 or throws leaves the flag at 0, so the next caller runs it, as before; the flag word still only holds 0 or 1. A recursive call on the flag the calling thread is initializing now throws `std::runtime_error`: the recursive mutex let the callback run a second time, where C++ deadlocks. A callback that ends its thread (`scePthreadExit`) skips the guard, so later callers of that one flag wait forever. Before this change every `call_once` in the process did. ### Tested Windows 11 (26200), WinLibs GCC 15.2.0 r7, Release: - `guest_execute_once` gains three cases: - a callback that waits up to 5 s for another thread initializing a different flag; without the fix the wait times out and the test aborts after about 5 s; - a recursive call on the same flag, which throws, leaves the flag at 0 and lets a later call succeed; - 8 threads racing on a flag whose first run throws: exactly one throw, one successful retry, 7 calls returning 1. The 16-thread run-once check is unchanged. - `guest_execute_once`, `guest_std_execute_once` and `guest_once` pass with `--repeat until-fail:5` on `main` 4bb2c7f3. - Full ctest on `main` ad653c9b: 1044/1045 passed (8 skipped on this machine). The one failure, `patched_libraries`, is `main`'s `libkernel_sys.prx` load failure on Windows, fixed by #5025. - `tools/check_conventions.py --base upstream/main` passes. Rebased onto `main` ffa2a6ec without conflicts (head 684dd88f), then tested again on `main` c496fd53: - Windows (same toolchain), with my eight other open PRs merged as well: `guest_execute_once`, `guest_std_execute_once` and `guest_once` pass. Full ctest: 1088/1088 passed (8 skipped on this machine). - Linux (Ubuntu 24.04 in WSL, GCC 13.3, Release, the CI's configure options, lavapipe): - this branch alone: `guest_execute_once`, `guest_std_execute_once` and `guest_once` pass. - full ctest with my eight other open PRs merged as well: 1067/1070 passed. `guest_math`, `agc_driver_depth_compare_copy` and `guest_socket_read_write` fail the same way on `main` b8683834 without any of them in this setup. No title was run. ### 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: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-14 02:34:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:34:20Z.


Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159604047 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5027#issuecomment-6159604047). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T02:34:20Z. --- Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

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!5027
No description provided.