fix(libc): return flush failures from fflush instead of throwing #4904

Merged
EagleBearerYS merged 1 commit from upstream-pr/4904 into main 2026-10-14 06:15:55 +00:00
EagleBearerYS commented 2026-10-13 03:15:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-13T03:15:08Z.


What

fflush (libc/src/FileAndHeap.cpp) throws when the host flush fails, so an ordinary write error such as a full disk ends the title:

terminate called after throwing an instance of 'std::runtime_error'
  what():  fflush: flush failed

On a write error, C17 7.21.5.2 asks fflush to set the stream's error indicator and return EOF. FreeBSD's __sflush does exactly that (fflush.c L126-L137).

fflush was the one stream call left:

  • #4380 made fwrite, fread and fputs return their errors instead of throwing.
  • #3926 did the same for fclose.
  • #4380's description left fflush to #4372, which has since merged without changing the throw.

fflush now returns the host's result and syncs the guest stream's error flag, as those functions do. A stream that was already closed still throws, as the existing test expects. No title is claimed.

Tested

Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release, -DBUILD_TESTING=ON, on main at c13dc7e6b:

  • file_streams now writes a line into a buffered stream, then replaces the stream's descriptor with a read-only one, so the flush fails with EBADF on both hosts. fflush must return EOF, and ferror must report the error.
  • With main's FileAndHeap.cpp the test ends with the terminate above. With the change it passes 20 runs in a row.
  • 128 tests pass: the tests registered in core/libs/prx/libc/CMakeLists.txt and core/libs/prx/libkernel/CMakeLists.txt that build on Windows.
  • tools/check_conventions.py --base origin/main passes.
  • Linux: WSL2 (Ubuntu 24.04, GCC 13.3.0, Release with CI's configure flags), same commit. With main's FileAndHeap.cpp the test aborts the same way. With the change it passes 20 runs in a row.
  • Not tested: macOS.
  • Title: XPSemu (PPSA97358), on main 18c438b1 with #5061 and #5187 merged, stops with fflush: flush failed (0xC0000409) when its Mesa shader-cache write fails under the Windows file lock (#4958); with this change fflush returns the failure and the title reaches xemu's GPU probe, as babyhuey reported in https://github.com/boykopovar/AnyPS5/pull/4904#issuecomment-6152738233.

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:4837824745 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4904). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-13T03:15:08Z. --- ### What `fflush` (`libc/src/FileAndHeap.cpp`) throws when the host flush fails, so an ordinary write error such as a full disk ends the title: ``` terminate called after throwing an instance of 'std::runtime_error' what(): fflush: flush failed ``` On a write error, C17 7.21.5.2 asks `fflush` to set the stream's error indicator and return `EOF`. FreeBSD's `__sflush` does exactly that ([`fflush.c` L126-L137](https://github.com/freebsd/freebsd-src/blob/d106002967eff712894ab8b5337bbbff106ce5e0/lib/libc/stdio/fflush.c#L126-L137)). `fflush` was the one stream call left: - #4380 made `fwrite`, `fread` and `fputs` return their errors instead of throwing. - #3926 did the same for `fclose`. - #4380's description left `fflush` to #4372, which has since merged without changing the throw. `fflush` now returns the host's result and syncs the guest stream's error flag, as those functions do. A stream that was already closed still throws, as the existing test expects. No title is claimed. ### Tested Windows 11 (10.0.26300), WinLibs MinGW-w64 GCC 15.2.0 r7 (UCRT), Release, `-DBUILD_TESTING=ON`, on `main` at c13dc7e6b: - `file_streams` now writes a line into a buffered stream, then replaces the stream's descriptor with a read-only one, so the flush fails with `EBADF` on both hosts. `fflush` must return `EOF`, and `ferror` must report the error. - With `main`'s `FileAndHeap.cpp` the test ends with the `terminate` above. With the change it passes 20 runs in a row. - 128 tests pass: the tests registered in `core/libs/prx/libc/CMakeLists.txt` and `core/libs/prx/libkernel/CMakeLists.txt` that build on Windows. - `tools/check_conventions.py --base origin/main` passes. - Linux: WSL2 (Ubuntu 24.04, GCC 13.3.0, Release with CI's configure flags), same commit. With `main`'s `FileAndHeap.cpp` the test aborts the same way. With the change it passes 20 runs in a row. - Not tested: macOS. - Title: XPSemu (PPSA97358), on `main` 18c438b1 with #5061 and #5187 merged, stops with `fflush: flush failed` (0xC0000409) when its Mesa shader-cache write fails under the Windows file lock (#4958); with this change `fflush` returns the failure and the title reaches xemu's GPU probe, as babyhuey reported in https://github.com/boykopovar/AnyPS5/pull/4904#issuecomment-6152738233. ### 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] AI-assisted: yes
xXPBSXx commented 2026-10-13 06:15:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T06:15:55Z.


Windows test: head 3d550b3de merged onto current main ea2bbefb2 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter file_streams: file_streams passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6139511834 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4904#issuecomment-6139511834). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T06:15:55Z. --- Windows test: head `3d550b3de` merged onto current `main` `ea2bbefb2` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest with the filter `file_streams`: file_streams passed (1 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
babyhuey commented 2026-10-13 18:48:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-13T18:48:14Z.


Title evidence from Windows (Windows 11, RTX 3080, WinLibs GCC 15.2, Release): XPSemu (PPSA97358), on main 18c438b1 with #5061 and #5187 merged, relinked with --windows-fixed-base 0x400000. Its Mesa shader cache write fails with EACCES under the whole-file Windows flock (#4958), and main throws fflush: flush failed (exit 0xC0000409). With this PR (3d550b3d, clean merge, without #4958) there's no throw. fflush returns the failure and Mesa carries on, so the title reaches xemu's GPU probe. Mesa treats each failed part as bad, though, and moves on through part0 to part49 with nothing stored, so #4958 is what makes the cache work.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6152738233 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4904#issuecomment-6152738233). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-13T18:48:14Z. --- Title evidence from Windows (Windows 11, RTX 3080, WinLibs GCC 15.2, Release): XPSemu (PPSA97358), on `main` 18c438b1 with #5061 and #5187 merged, relinked with `--windows-fixed-base 0x400000`. Its Mesa shader cache write fails with `EACCES` under the whole-file Windows `flock` (#4958), and `main` throws `fflush: flush failed` (exit 0xC0000409). With this PR (3d550b3d, clean merge, without #4958) there's no throw. `fflush` returns the failure and Mesa carries on, so the title reaches xemu's GPU probe. Mesa treats each failed part as bad, though, and moves on through part0 to part49 with nothing stored, so #4958 is what makes the cache work.
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!4904
No description provided.