fix(libc): discard a failed write's bytes on an unbuffered stream on macOS #21721
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/21721"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-16T16:48:23Z.
What
On macOS,
file_streamsfails in its write-error recovery check. After a write to an unbuffered stream fails withEAGAINon 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, thenfputc('A'):Even unbuffered, macOS stdio writes through a one-byte buffer and keeps the byte when the write fails. With
fpurgeafter the failure, the reader getsAand nothing else. glibc and the UCRT drop the failed byte, so Linux and Windows pass.FileStream::DropFailedUnbufferedWritepurges the pending output of an unbuffered stream on macOS. On an unbuffered stream that is only the failed write's bytes.fputc(andputcand__swbufthrough it),putc_unlocked,fwriteandfputscall 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_streamsalso needs #21720 (stream float scans of malformed prefixes) to pass on macOS.Tested
-DBUILD_TESTING=ON, x86_64 under Rosettafile_streamspasses. 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 4run and pass alone.python3 tools/check_conventions.py --base origin/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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:
mainata72186d3cf, 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:file_streams, identical in all 3 runs of each build:Stream scan changed a matching failure to input failureFile stream check failedSo on macOS this PR can't be confirmed on its own. The scan failure stops
file_streamsbefore it reaches the unbuffered write check. With #21720 merged, the test stops atFile 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_ctypeaborts andguest_libc_internal_standard_stream_initis Not Run on every build,mainincluded.-j4, and passed in every other run:guest_system_service_date_time_formaton this PR alone andcode_data_scanningon both PRs together. Neither test is touched by these PRs.I didn't test this on Linux or Windows.