test(libkernel): time blocking waits from before the writer thread starts #7670

Merged
thuanquang merged 1 commit from upstream-pr/7670 into main 2026-10-19 10:53:41 +00:00
thuanquang commented 2026-10-18 21:25:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-18T21:25:10Z.


What

guest_kqueue (PipeRead) starts a writer thread that sleeps 50 ms and then writes one byte to a pipe, and requires the main thread's blocking kevent to have waited at least 40 ms for it. The wait was measured from a start taken after the writer thread was created. When the main thread is preempted between creating the writer and taking start, the writer's 50 ms run partly or wholly before start, so kevent returns sooner after start than 40 ms although it did wait for the write, and the test aborts at GuestKqueue.cpp:246. This is the abort under parallel load that @wachi35676 (kevent returned 13 us to 35 ms into the wait) and @screamm reported on #7492.

start is now taken before the writer thread is created, so the measured wait always includes the writer's sleep. The blocking read in guest_socket_events (GuestSocketEvents.cpp) has the same order, a sender thread that sends after 50 ms and a 40 ms check on a start taken after the thread, and gets the same change. That sleep does not end early on Windows: with WinLibs GCC 15.2.0 r7, 120 calls of sleep_for(50 ms) here took 58.2 to 77.6 ms (winpthreads rounds timed waits to GetTickCount64 steps, #1471). A kevent that returns before the write still fails the test: before 40 ms at the same check, later at the data == 1 check.

Tested

Windows 11 26200, Ryzen 5 5600X, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON.

  • The race, on main 6685795f7, 8 parallel copies of guest_kqueue plus 12 busy processes: 9 of 120 runs aborted (0xC0000409). With a local probe (not part of this PR) that printed the timings of PipeRead, every abort looked the same (4 of 4): the main thread took start 38.7 to 67.7 ms after creating the writer, the write came 28.5 to 39.6 ms after start, and kevent returned 0.3 to 1.4 ms after the write with data 1. So the pipe was never reported before the write.
  • sleep_for(50 ms): 120 calls took 58.2 to 77.6 ms (median 62.1 ms).
  • With this change on main 48d61f53b: guest_kqueue and guest_socket_events pass 3 of 3 under ctest --repeat until-fail:3. Run 15 times in a row with 24 busy threads, neither main nor this change failed, so this machine needs the parallel load above to show it.
  • guest_socket_events did not fail on main in our load runs; it is changed because its wait is measured the same way.
  • python tools/check_conventions.py --base origin/main with this title and body passes.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4912942990 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7670). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-18T21:25:10Z. --- ### What `guest_kqueue` (`PipeRead`) starts a writer thread that sleeps 50 ms and then writes one byte to a pipe, and requires the main thread's blocking `kevent` to have waited at least 40 ms for it. The wait was measured from a `start` taken after the writer thread was created. When the main thread is preempted between creating the writer and taking `start`, the writer's 50 ms run partly or wholly before `start`, so `kevent` returns sooner after `start` than 40 ms although it did wait for the write, and the test aborts at `GuestKqueue.cpp:246`. This is the abort under parallel load that @wachi35676 (`kevent` returned 13 us to 35 ms into the wait) and @screamm reported on #7492. `start` is now taken before the writer thread is created, so the measured wait always includes the writer's sleep. The blocking read in `guest_socket_events` (`GuestSocketEvents.cpp`) has the same order, a sender thread that sends after 50 ms and a 40 ms check on a `start` taken after the thread, and gets the same change. That sleep does not end early on Windows: with WinLibs GCC 15.2.0 r7, 120 calls of `sleep_for(50 ms)` here took 58.2 to 77.6 ms (winpthreads rounds timed waits to `GetTickCount64` steps, #1471). A `kevent` that returns before the write still fails the test: before 40 ms at the same check, later at the `data == 1` check. ### Tested Windows 11 26200, Ryzen 5 5600X, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. - The race, on `main` `6685795f7`, 8 parallel copies of `guest_kqueue` plus 12 busy processes: 9 of 120 runs aborted (0xC0000409). With a local probe (not part of this PR) that printed the timings of `PipeRead`, every abort looked the same (4 of 4): the main thread took `start` 38.7 to 67.7 ms after creating the writer, the write came 28.5 to 39.6 ms after `start`, and `kevent` returned 0.3 to 1.4 ms after the write with `data` 1. So the pipe was never reported before the write. - `sleep_for(50 ms)`: 120 calls took 58.2 to 77.6 ms (median 62.1 ms). - With this change on `main` `48d61f53b`: `guest_kqueue` and `guest_socket_events` pass 3 of 3 under `ctest --repeat until-fail:3`. Run 15 times in a row with 24 busy threads, neither `main` nor this change failed, so this machine needs the parallel load above to show it. - `guest_socket_events` did not fail on `main` in our load runs; it is changed because its wait is measured the same way. - `python tools/check_conventions.py --base origin/main` with this title and body passes. ### 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-18 21:36:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T21:36:30Z.


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 6044 core/libs/tests/GuestKqueue.cpp
core/libs/tests/GuestSocketEvents.cpp
PR 7049 core/libs/tests/GuestKqueue.cpp
PR 7075 core/libs/tests/GuestKqueue.cpp
PR 7088 core/libs/tests/GuestKqueue.cpp
PR 7121 core/libs/tests/GuestKqueue.cpp
PR 7129 core/libs/tests/GuestKqueue.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6255443054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7670#issuecomment-6255443054). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-18T21:36:30Z. --- <!-- 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 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestKqueue.cpp`<br>`core/libs/tests/GuestSocketEvents.cpp` | | [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) | `core/libs/tests/GuestKqueue.cpp` | | [PR 7075](https://www.github.com/boykopovar/AnyPS5/pull/7075) | `core/libs/tests/GuestKqueue.cpp` | | [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `core/libs/tests/GuestKqueue.cpp` | | [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) | `core/libs/tests/GuestKqueue.cpp` | | [PR 7129](https://www.github.com/boykopovar/AnyPS5/pull/7129) | `core/libs/tests/GuestKqueue.cpp` |
karimibrahim07 commented 2026-10-19 04:32:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karimibrahim07 on 2026-10-19T04:32:57Z.

Review: COMMENTED


Windows check of head e0da1fb1c, merged onto main ccbd40240 together with #7669, #7709, #7717 and #7680 (each merge clean, built once). Windows 11 Pro (26300), Ryzen 5 3600 (6 cores, 12 threads), WinLibs GCC 15.2.0 r7, Ninja Release, BUILD_TESTING=ON.

  • guest_kqueue and guest_socket_events pass, --repeat until-fail:3.

  • Under load, with main's and this PR's test binaries built from the same tree, started directly:

    Load Test main this PR
    8 copies at once + 12 busy processes guest_kqueue 80 of 80 pass 80 of 80 pass
    16 copies at once + 24 busy processes guest_kqueue 159 of 160 pass, 1 abort 160 of 160 pass
    16 copies at once + 24 busy processes guest_socket_events 160 of 160 pass (2 runs) 160 of 160 pass (run first)

    In an earlier run that accidentally overlapped two of these batches, main's guest_kqueue aborted in 4 of 8 copies in one round. Require there calls abort() without a line, so I can't confirm these aborts are the GuestKqueue.cpp:246 check. They do only show up on main's binary.

  • A note so nobody chases it: in two batches where this PR's guest_socket_events ran right after main's, 2 of 160 copies hung past my 60 s limit. With the order swapped, this PR's binary passed 160 of 160 first. So that depends on what ran before (16 copies at once leave many sockets behind), not on this change. It was a hang, not the 40 ms check, and this PR only moves start.

Code: taking start before the thread is created makes the measured wait include the writer's whole 50 ms sleep, so the >= 40 ms bound can't fail because of a slow main thread. A kevent or sceKernelWaitEqueue that returns without the event still fails at the == 1 / data checks after it. The same order in GuestSocketEvents.cpp is right for the same reason. I have no requests.

AI-assisted: yes. I ran the build and the runs above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5566953186 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7670#pullrequestreview-5566953186). Originally posted by [karimibrahim07](https://github.com/karimibrahim07) on 2026-10-19T04:32:57Z. **Review:** COMMENTED --- Windows check of head `e0da1fb1c`, merged onto `main` `ccbd40240` together with #7669, #7709, #7717 and #7680 (each merge clean, built once). Windows 11 Pro (26300), Ryzen 5 3600 (6 cores, 12 threads), WinLibs GCC 15.2.0 r7, Ninja Release, `BUILD_TESTING=ON`. - `guest_kqueue` and `guest_socket_events` pass, `--repeat until-fail:3`. - **Under load,** with `main`'s and this PR's test binaries built from the same tree, started directly: | Load | Test | `main` | this PR | |---|---|---|---| | 8 copies at once + 12 busy processes | `guest_kqueue` | 80 of 80 pass | 80 of 80 pass | | 16 copies at once + 24 busy processes | `guest_kqueue` | 159 of 160 pass, 1 abort | 160 of 160 pass | | 16 copies at once + 24 busy processes | `guest_socket_events` | 160 of 160 pass (2 runs) | 160 of 160 pass (run first) | In an earlier run that accidentally overlapped two of these batches, `main`'s `guest_kqueue` aborted in 4 of 8 copies in one round. `Require` there calls `abort()` without a line, so I can't confirm these aborts are the `GuestKqueue.cpp:246` check. They do only show up on `main`'s binary. - A note so nobody chases it: in two batches where this PR's `guest_socket_events` ran right after `main`'s, 2 of 160 copies hung past my 60 s limit. With the order swapped, this PR's binary passed 160 of 160 first. So that depends on what ran before (16 copies at once leave many sockets behind), not on this change. It was a hang, not the 40 ms check, and this PR only moves `start`. **Code:** taking `start` before the thread is created makes the measured wait include the writer's whole 50 ms sleep, so the `>= 40 ms` bound can't fail because of a slow main thread. A `kevent` or `sceKernelWaitEqueue` that returns without the event still fails at the `== 1` / `data` checks after it. The same order in `GuestSocketEvents.cpp` is right for the same reason. I have no requests. AI-assisted: yes. I ran the build and the runs above.
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!7670
No description provided.