fix(libkernel): allocate the Windows thread-local slot on first use #6956

Merged
jradic merged 1 commit from upstream-pr/6956 into main 2026-10-17 11:31:01 +00:00
jradic commented 2026-10-16 21:10:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jradic on 2026-10-16T21:10:10Z.


What

Setting APS5_TRACE_TIMERS=1 crashed every title at startup on Windows with Cannot bind native thread state.

The timer self-test in Time.cpp runs during libkernel's static init. WindowsThreadLocal<T>::slot is a template static, so its init order isn't guaranteed, and here it hadn't run yet. The slot was still 0, so FlsSetValue failed.

The slot is now a function-local static, so it gets allocated on first use regardless of init order. Also added guest_nanosleep_timer_self_test, which reruns guest_nanosleep_tests with APS5_TRACE_TIMERS=1 (Windows only). #6658 adds a test right after the same guest_nanosleep block; no functional overlap.

Tested

Windows 11, WinLibs GCC 15.2.0, Release.

  • New test: fails on main with the same error, passes with the fix (~8.5 s).
  • ccleste (PPSA59322) and ScreenTester5 (PPSA69420) crashed at startup with the variable set. With the fix they run, and all 7 timer lines print.
  • Full ctest: no new failures. The only ones were #6791, the alignas build error fixed in #6452/#6829, and a symlink-permission failure in select_tests on my machine.

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
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01KwsvKtVsZDLefysUM9m2QS

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4894311149 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6956). Originally posted by [jradic](https://github.com/jradic) on 2026-10-16T21:10:10Z. --- ### What Setting `APS5_TRACE_TIMERS=1` crashed every title at startup on Windows with `Cannot bind native thread state`. The timer self-test in `Time.cpp` runs during libkernel's static init. `WindowsThreadLocal<T>::slot` is a template static, so its init order isn't guaranteed, and here it hadn't run yet. The slot was still 0, so `FlsSetValue` failed. The slot is now a function-local static, so it gets allocated on first use regardless of init order. Also added `guest_nanosleep_timer_self_test`, which reruns `guest_nanosleep_tests` with `APS5_TRACE_TIMERS=1` (Windows only). #6658 adds a test right after the same `guest_nanosleep` block; no functional overlap. ### Tested Windows 11, WinLibs GCC 15.2.0, Release. - New test: fails on main with the same error, passes with the fix (~8.5 s). - ccleste (PPSA59322) and ScreenTester5 (PPSA69420) crashed at startup with the variable set. With the fix they run, and all 7 timer lines print. - Full ctest: no new failures. The only ones were #6791, the `alignas` build error fixed in #6452/#6829, and a symlink-permission failure in `select_tests` on my machine. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01KwsvKtVsZDLefysUM9m2QS
github-actions[bot] commented 2026-10-16 21:27:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T21:27:28Z.


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 4972 core/libs/prx/libkernel/CMakeLists.txt
PR 5021 core/libs/prx/libkernel/CMakeLists.txt
PR 6044 core/libs/prx/libkernel/CMakeLists.txt
PR 6222 core/libs/prx/libkernel/CMakeLists.txt
PR 6287 core/libs/prx/libkernel/CMakeLists.txt
PR 6487 core/libs/prx/libkernel/CMakeLists.txt
PR 6884 core/libs/prx/libkernel/CMakeLists.txt
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6225028567 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6956#issuecomment-6225028567). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-16T21:27:28Z. --- <!-- 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 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` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6287](https://www.github.com/boykopovar/AnyPS5/pull/6287) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6487](https://www.github.com/boykopovar/AnyPS5/pull/6487) | `core/libs/prx/libkernel/CMakeLists.txt` | | [PR 6884](https://www.github.com/boykopovar/AnyPS5/pull/6884) | `core/libs/prx/libkernel/CMakeLists.txt` |
jradic commented 2026-10-16 21:47:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jradic on 2026-10-16T21:47:10Z.


Checked these by hand: none change the same lines. #6658 adds a test right after the same guest_nanosleep block, so whichever lands second needs a trivial rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6225314396 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6956#issuecomment-6225314396). Originally posted by [jradic](https://github.com/jradic) on 2026-10-16T21:47:10Z. --- Checked these by hand: none change the same lines. #6658 adds a test right after the same `guest_nanosleep` block, so whichever lands second needs a trivial rebase.
karimibrahim07 commented 2026-10-17 03:52:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-17T03:52:27Z.

Review: COMMENTED


Windows check of head 75d29f5b3 merged onto main e3a8e4430 (clean merge). Windows 11 Pro (26300), Ryzen 5 3600, WinLibs GCC 15.2.0 r7, Ninja Release.

  • guest_nanosleep and the new guest_nanosleep_timer_self_test pass, --repeat until-fail:3 (about 7 s each).
  • Negative control: with main's WindowsThreadLocal.hpp and the new test kept, guest_nanosleep_timer_self_test fails with what(): Cannot bind native thread state (0xc0000409), the same error as in the description.

Code: a function-local static is the right fix. Dynamic initialization of a class template's inline static member is unordered, so any static initializer in libkernel that reaches Get() can see a zero slot. libc's HostThreadLocal.hpp already uses the same pattern (static const DWORD slot = FlsAlloc(...) inside the function), so the two thread-local helpers now match. The other WindowsThreadLocal users (TimedWait.cpp, twice, Tsd.cpp, Thread.cpp) only call Get(), so they all benefit and none depends on the old member. Destroy calling slotIndex() is safe: the slot is necessarily initialized by the time FLS calls it. I have no requests.

AI-assisted: yes. I ran the build, the tests and the negative control above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5555933494 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6956#pullrequestreview-5555933494). Originally posted by [karimibrahim07](https://github.com/karimibrahim07) on 2026-10-17T03:52:27Z. **Review:** COMMENTED --- Windows check of head `75d29f5b3` merged onto `main` `e3a8e4430` (clean merge). Windows 11 Pro (26300), Ryzen 5 3600, WinLibs GCC 15.2.0 r7, Ninja Release. - `guest_nanosleep` and the new `guest_nanosleep_timer_self_test` pass, `--repeat until-fail:3` (about 7 s each). - Negative control: with `main`'s `WindowsThreadLocal.hpp` and the new test kept, `guest_nanosleep_timer_self_test` fails with `what(): Cannot bind native thread state` (`0xc0000409`), the same error as in the description. **Code:** a function-local static is the right fix. Dynamic initialization of a class template's `inline static` member is unordered, so any static initializer in libkernel that reaches `Get()` can see a zero slot. `libc`'s `HostThreadLocal.hpp` already uses the same pattern (`static const DWORD slot = FlsAlloc(...)` inside the function), so the two thread-local helpers now match. The other `WindowsThreadLocal` users (`TimedWait.cpp`, twice, `Tsd.cpp`, `Thread.cpp`) only call `Get()`, so they all benefit and none depends on the old member. `Destroy` calling `slotIndex()` is safe: the slot is necessarily initialized by the time FLS calls it. I have no requests. AI-assisted: yes. I ran the build, the tests and the negative control above.
screamm commented 2026-10-17 10:05:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T10:05:48Z.


Cross-validation on Windows 11 Pro 26220, NVIDIA RTX 4070 (596.21), MinGW GCC 15.2, PR head 75d29f5b3, built together with #7005 (disjoint files) on main 5f61879a9 (clean merge). Before-runs used a build of 3b10c7ef0; the libkernel Pthread/Time sources are identical between the two.

  • Full build and libs: pass.
  • Negative control on the main build: APS5_TRACE_TIMERS=1 with guest_nanosleep fails 3 of 3, exit 0xc0000409, terminate called after throwing an instance of 'std::runtime_error' / what(): Cannot bind native thread state, after the first [timers] line; without the variable it passes. No source-revert rebuild was done, the main build served as the control.
  • guest_nanosleep_timer_self_test, --repeat until-fail:3: 3 of 3 pass on an idle host (7.2 s each). On a busy host (other builds running) it took 21 s and 25 s and then hit its own 30 s limit twice, so the limit is tight under load; a larger TIMEOUT would avoid that. guest_nanosleep 3 of 3.
  • Titles with APS5_TRACE_TIMERS=1, relinked, 60 s limit:
    • ccleste PPSA59322: main aborts at 12.0 s with exit 0xC0000409 (terminate called after throwing an instance of 'std::runtime_error', what(): Cannot bind native thread state, FATAL: abort() (SIGABRT)) after one [timers] line. With this PR it runs until the kill (3323 frames, no stop line) and prints all 7 [timers] lines.
    • ScreenTester5 PPSA69420: main aborts at 10.7 s with the same message and exit code. With this PR it runs until the kill (2 frames presented, as in our earlier runs without the variable) and prints all 7 [timers] lines.
  • ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip': 855 of 877 pass, 2 skipped, 20 timeouts on a host with other builds running; all 20 pass on serial reruns (conventions/tooling tests and four libkernel/relinker tests, 0.6 to 55 s each). The new test passed in the suite.
  • tools/check_conventions.py --base origin/main and git diff --check: pass.

Not checked: Linux/macOS (the change is Windows-only), a revert-and-rebuild of the fix, the other open PRs that touch libkernel/CMakeLists.txt.

Written with Claude Code assistance (validation by a subagent, reviewed in this session).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6232624315 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6956#issuecomment-6232624315). Originally posted by [screamm](https://github.com/screamm) on 2026-10-17T10:05:48Z. --- Cross-validation on Windows 11 Pro 26220, NVIDIA RTX 4070 (596.21), MinGW GCC 15.2, PR head `75d29f5b3`, built together with #7005 (disjoint files) on main `5f61879a9` (clean merge). Before-runs used a build of `3b10c7ef0`; the libkernel Pthread/Time sources are identical between the two. - Full build and `libs`: pass. - Negative control on the `main` build: `APS5_TRACE_TIMERS=1` with `guest_nanosleep` fails 3 of 3, exit `0xc0000409`, `terminate called after throwing an instance of 'std::runtime_error'` / `what(): Cannot bind native thread state`, after the first `[timers]` line; without the variable it passes. No source-revert rebuild was done, the `main` build served as the control. - `guest_nanosleep_timer_self_test`, `--repeat until-fail:3`: 3 of 3 pass on an idle host (7.2 s each). On a busy host (other builds running) it took 21 s and 25 s and then hit its own 30 s limit twice, so the limit is tight under load; a larger `TIMEOUT` would avoid that. `guest_nanosleep` 3 of 3. - Titles with `APS5_TRACE_TIMERS=1`, relinked, 60 s limit: - ccleste PPSA59322: `main` aborts at 12.0 s with exit `0xC0000409` (`terminate called after throwing an instance of 'std::runtime_error'`, `what(): Cannot bind native thread state`, `FATAL: abort() (SIGABRT)`) after one `[timers]` line. With this PR it runs until the kill (3323 frames, no stop line) and prints all 7 `[timers]` lines. - ScreenTester5 PPSA69420: `main` aborts at 10.7 s with the same message and exit code. With this PR it runs until the kill (2 frames presented, as in our earlier runs without the variable) and prints all 7 `[timers]` lines. - `ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'`: 855 of 877 pass, 2 skipped, 20 timeouts on a host with other builds running; all 20 pass on serial reruns (conventions/tooling tests and four libkernel/relinker tests, 0.6 to 55 s each). The new test passed in the suite. - `tools/check_conventions.py --base origin/main` and `git diff --check`: pass. Not checked: Linux/macOS (the change is Windows-only), a revert-and-rebuild of the fix, the other open PRs that touch `libkernel/CMakeLists.txt`. Written with Claude Code assistance (validation by a subagent, reviewed in this session).
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!6956
No description provided.