fix(audioin): never return a no-device block before its deadline #532

Merged
adriadam10 merged 1 commit from upstream-pr/532 into main 2026-10-04 18:37:04 +00:00
adriadam10 commented 2026-10-04 15:28:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T15:28:05Z.


What

Fixes the intermittent Windows crash of guest_audio_in_no_device (0xc0000409) reported on #243.

The crash is the test's Require aborting on elapsed >= 4 * 16 ms, because on Windows the no-device sceAudioInInput could return before its block deadline:

  • libstdc++ this_thread::sleep_until on a steady clock makes a single sleep_for(atime - now) call and doesn't check the clock again (bits/this_thread_sleep.h, GCC 15.2, the CI toolchain).
  • On the winlibs posix-threads build, sleep_for ends in winpthreads nanosleep, which truncates the request to whole milliseconds (tv_sec * 1000 + tv_nsec / 1000000, winpthreads/src/nanosleep.c, mingw-w64 14). So it sleeps up to 1 ms short, and Sleep itself may also wake early. If less than 1 ms is left, it doesn't sleep at all.

So the fifth block can come back just under 64 ms. glibc's nanosleep never returns early, which is why it only shows up on Windows. sceAudioInInput now sleeps until the clock actually reaches the deadline. That is also the pacing a guest relies on: one block per grain, never early. The SDL capture path already polls Clock::now() against its deadline and is unchanged.

Tested

Linux: Release build, full ctest (only agc_driver_pixel_helper_lanes fails, which also fails on main with local RADV), and guest_audio_in_* with --repeat until-fail:20. I have no Windows machine, so I couldn't reproduce the early wake locally: the fix comes from the two sources above. A few Windows CI reruns on a fork would confirm it.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4735474965 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/532). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T15:28:05Z. --- ### What Fixes the intermittent Windows crash of `guest_audio_in_no_device` (`0xc0000409`) reported on #243. The crash is the test's `Require` aborting on `elapsed >= 4 * 16 ms`, because on Windows the no-device `sceAudioInInput` could return before its block deadline: - libstdc++ `this_thread::sleep_until` on a steady clock makes a single `sleep_for(atime - now)` call and doesn't check the clock again (`bits/this_thread_sleep.h`, GCC 15.2, the CI toolchain). - On the winlibs posix-threads build, `sleep_for` ends in winpthreads `nanosleep`, which truncates the request to whole milliseconds (`tv_sec * 1000 + tv_nsec / 1000000`, `winpthreads/src/nanosleep.c`, mingw-w64 14). So it sleeps up to 1 ms short, and `Sleep` itself may also wake early. If less than 1 ms is left, it doesn't sleep at all. So the fifth block can come back just under 64 ms. glibc's `nanosleep` never returns early, which is why it only shows up on Windows. `sceAudioInInput` now sleeps until the clock actually reaches the deadline. That is also the pacing a guest relies on: one block per grain, never early. The SDL capture path already polls `Clock::now()` against its deadline and is unchanged. ### Tested Linux: Release build, full ctest (only `agc_driver_pixel_helper_lanes` fails, which also fails on `main` with local RADV), and `guest_audio_in_*` with `--repeat until-fail:20`. I have no Windows machine, so I couldn't reproduce the early wake locally: the fix comes from the two sources above. A few Windows CI reruns on a fork would confirm it. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
oneandonlydean commented 2026-10-04 18:36:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T18:36:59Z.


Thanks, this matches what I found: on the Windows CI toolchain sleep_until can wake about 1 ms early. With this exact change my fork's Windows CI ran the test 1200 times with 0 failures, against 19 in 300 on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5983133666 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/532#issuecomment-5983133666). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T18:36:59Z. --- Thanks, this matches what I found: on the Windows CI toolchain `sleep_until` can wake about 1 ms early. With this exact change my fork's Windows CI ran the test 1200 times with 0 failures, against 19 in 300 on main.
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!532
No description provided.