fix(libc): discard a failed write's bytes on an unbuffered stream on macOS #21721

Merged
XIIYAZ merged 1 commit from upstream-pr/21721 into main 2026-11-19 17:05:17 +00:00
XIIYAZ commented 2026-11-16 16:48:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-16T16:48:23Z.


What

On macOS, file_streams fails in its write-error recovery check. After a write to an unbuffered stream fails with EAGAIN on a full nonblocking pipe and the pipe is drained, the next write's byte must be the one the reader gets. On macOS the reader gets the byte of the failed write first.

A standalone program on macOS (no AnyPS5) shows it: an unbuffered stream on a full nonblocking pipe, fputc('1') fails, the pipe is drained, then fputc('A'):

fail fputc=-1 errno=35 ferror=1
recover fputc=65 read=1 byte=1
next read=1 byte=A

Even unbuffered, macOS stdio writes through a one-byte buffer and keeps the byte when the write fails. With fpurge after the failure, the reader gets A and nothing else. glibc and the UCRT drop the failed byte, so Linux and Windows pass.

FileStream::DropFailedUnbufferedWrite purges the pending output of an unbuffered stream on macOS. On an unbuffered stream that is only the failed write's bytes. fputc (and putc and __swbuf through it), putc_unlocked, fwrite and fputs call it when the host write reports a failure. The error indicator stays set, as the test checks. Buffered streams keep their pending output as before, and Linux and Windows are unchanged.

file_streams also needs #21720 (stream float scans of malformed prefixes) to pass on macOS.

Tested

  • Apple M4, macOS 26.7.3, Apple clang 21.0.0, full Release build with -DBUILD_TESTING=ON, x86_64 under Rosetta
  • With this change and #21720, file_streams passes. Without this change, it fails at the read after the recovering write for every writer case.
  • ctest -R 'file_streams|scan|format|printf|stdio|wide|fwrite|fputs|stream' (99 tests) passes. Two audio tests that the pattern also matched timed out in that -j 4 run and pass alone.
  • python3 tools/check_conventions.py --base origin/main 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: none (no GPU work)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5341145383 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21721). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-16T16:48:23Z. --- ### What On macOS, `file_streams` fails in its write-error recovery check. After a write to an unbuffered stream fails with `EAGAIN` on a full nonblocking pipe and the pipe is drained, the next write's byte must be the one the reader gets. On macOS the reader gets the byte of the failed write first. A standalone program on macOS (no AnyPS5) shows it: an unbuffered stream on a full nonblocking pipe, `fputc('1')` fails, the pipe is drained, then `fputc('A')`: ``` fail fputc=-1 errno=35 ferror=1 recover fputc=65 read=1 byte=1 next read=1 byte=A ``` Even unbuffered, macOS stdio writes through a one-byte buffer and keeps the byte when the write fails. With `fpurge` after the failure, the reader gets `A` and nothing else. glibc and the UCRT drop the failed byte, so Linux and Windows pass. `FileStream::DropFailedUnbufferedWrite` purges the pending output of an unbuffered stream on macOS. On an unbuffered stream that is only the failed write's bytes. `fputc` (and `putc` and `__swbuf` through it), `putc_unlocked`, `fwrite` and `fputs` call it when the host write reports a failure. The error indicator stays set, as the test checks. Buffered streams keep their pending output as before, and Linux and Windows are unchanged. `file_streams` also needs #21720 (stream float scans of malformed prefixes) to pass on macOS. ### Tested - Apple M4, macOS 26.7.3, Apple clang 21.0.0, full Release build with `-DBUILD_TESTING=ON`, x86_64 under Rosetta - With this change and #21720, `file_streams` passes. Without this change, it fails at the read after the recovering write for every writer case. - `ctest -R 'file_streams|scan|format|printf|stdio|wide|fwrite|fputs|stream'` (99 tests) passes. Two audio tests that the pattern also matched timed out in that `-j 4` run and pass alone. - `python3 tools/check_conventions.py --base origin/main` 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: none (no GPU work) - [x] AI-assisted: yes
kurexw commented 2026-11-17 06:14:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-17T06:14:28Z.

Review: COMMENTED


Tested on macOS 27.2 on an Apple M4. This is an x86-64 build under Rosetta with Apple clang 21.0.0.

Method. I compared four builds: main at a72186d3cf, this PR merged onto it, #21720 merged onto it, and both PRs merged onto it. All the merges were clean. On each build I ran the same 103 stream, scan and format tests three times:

ctest --test-dir build -R '(file_streams|scan|format|printf|stdio|wide|fwrite|fputs|stream)' -j4 --timeout 300 --output-on-failure

file_streams, identical in all 3 runs of each build:

Build Result
main Fails: Stream scan changed a matching failure to input failure
this PR alone Fails at the same scan check
#21720 alone Gets past the scan check, then fails: File stream check failed
#21720 + this PR Passes

So on macOS this PR can't be confirmed on its own. The scan failure stops file_streams before it reaches the unbuffered write check. With #21720 merged, the test stops at File stream check failed. Adding this PR makes it pass, which is consistent with this PR fixing that check. As the description says, it needs #21720 to show on macOS.

Other tests in the set. There were no regressions.

  • guest_libc_internal_wide_ctype aborts and guest_libc_internal_standard_stream_init is Not Run on every build, main included.
  • Two tests timed out once each, at ctest's 20 s per-test limit under -j4, and passed in every other run: guest_system_service_date_time_format on this PR alone and code_data_scanning on both PRs together. Neither test is touched by these PRs.

I didn't test this on Linux or Windows.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5933865089 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/21721#pullrequestreview-5933865089). Originally posted by [kurexw](https://github.com/kurexw) on 2026-11-17T06:14:28Z. **Review:** COMMENTED --- Tested on macOS 27.2 on an Apple M4. This is an x86-64 build under Rosetta with Apple clang 21.0.0. **Method.** I compared four builds: `main` at a72186d3cf, this PR merged onto it, #21720 merged onto it, and both PRs merged onto it. All the merges were clean. On each build I ran the same 103 stream, scan and format tests three times: ``` ctest --test-dir build -R '(file_streams|scan|format|printf|stdio|wide|fwrite|fputs|stream)' -j4 --timeout 300 --output-on-failure ``` **`file_streams`, identical in all 3 runs of each build:** | Build | Result | | --- | --- | | main | Fails: `Stream scan changed a matching failure to input failure` | | this PR alone | Fails at the same scan check | | #21720 alone | Gets past the scan check, then fails: `File stream check failed` | | #21720 + this PR | Passes | So on macOS this PR can't be confirmed on its own. The scan failure stops `file_streams` before it reaches the unbuffered write check. With #21720 merged, the test stops at `File stream check failed`. Adding this PR makes it pass, which is consistent with this PR fixing that check. As the description says, it needs #21720 to show on macOS. **Other tests in the set.** There were no regressions. - `guest_libc_internal_wide_ctype` aborts and `guest_libc_internal_standard_stream_init` is Not Run on every build, `main` included. - Two tests timed out once each, at ctest's 20 s per-test limit under `-j4`, and passed in every other run: `guest_system_service_date_time_format` on this PR alone and `code_data_scanning` on both PRs together. Neither test is touched by these PRs. I didn't test this on Linux or Windows.
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!21721
No description provided.