fix(libc): leave write mode when an unbuffered update stream switches to reading on Windows #12036
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12036"
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 Ragbie on 2026-10-27T17:19:03Z.
What
On Windows a read that follows a write on an unbuffered update stream fails. With
0123456789openedr+bandsetvbuf(_IONBF),fputc('A')andfputc('B')thenfgetcreturnEOFand set the error indicator, where FreeBSD returns'2'.fread,getc_unlocked,ungetcfollowed byfgetc, and anfflushbefore the read fail the same way. Writes that reach the UCRT throughfputc,putc_unlockedorfputwcleave the stream in write mode;fwriteandfputsof more than one byte bypass it and already work.FreeBSD 11.4's
__srefillflushes a stream that is writing, clears__SWRand sets__SRDbefore it reads, and__ungetcdoes the same.FileStream::SwitchToReadingUnlocked(from #8226) flushes and adds_IOREAD, but leaves clearing_IOWRITEto the UCRT's_fflush_nolock. That only happens when the flush writes buffered bytes: a stream without a buffer returns 0 at once with_IOWRITEstill set, and_filbufthen refuses to read.The change clears
_IOWRITEafter a successful flush, before setting_IOREAD. A failed flush leaves the flags as they were, as before. On a buffered stream that the flush wrote, the UCRT has already cleared the flag, so nothing changes there. Linux and macOS are not touched. The libc debt entry already says reads directly after writes follow__srefill, which is now also true for unbuffered streams.Tested
Windows 11 Pro, Intel Core i5-14600KF, WinLibs GCC 15.2.0, full Release build:
file_streamsgetsCheckUnbufferedStreamReadsAfterWrites. On an unbufferedr+bstream over0123456789, each offwrite,fputs,fputc,putc_unlocked,fprintf,fputwcandfputcwithfflushwritesAB. Thenfgetc,getc_unlocked, a one-bytefread, orungetc('Z')with twofgetcmust read'2'without the error indicator,ftellmust give 3, and the file must holdAB23456789.main's sources):file_streamsfails withfgetc after a write on an unbuffered r+ stream. Thefwriteandfputsrounds before it pass.file_streamsand theguest_standard_streamstests pass with--repeat until-fail:5.stream|stdio|printf|wide|format|fopen|freopen|fclose|fwrite|fread|putw|getw|ungetc|gets|scanf|puts|libc|random|fwide|setvbufpass with--repeat until-fail:3, exceptguest_wide_formatting. That test fails onmainhere too:%Zgives the host's non-ASCII zone name, which #10987 addresses.ctestonmainc547deaac:99% tests passed, 3 tests failed out of 2669.guest_wide_formatting, as above.agc_driver_image_plane_address_on_linealso fails onmainon this machine.agc_driver_mesh_barycentric_emulationfails every time on this machine's NVIDIA GPU withthe pixel shader did not emulate barycentrics. #9193 changed that test today. It does not use libc streams.check_skips.pyreports one skip,guest_fopen_errors. It skips itself on this machine because creating symbolic links needs Developer Mode.Linux (Ubuntu 24.04 container, GCC 13.3.0), where the change does nothing: the 70 tests matching the same pattern pass with
--repeat until-fail:3, including the new cases.macOS: not tested.
Overlaps:
mainofcore/libs/tests/FileStreams.cpp, so the two conflict there; the tested behaviours are separate. #8150 already conflicts withmaininStandardStreams.cpp,FileStreams.cppanddocs/dev/debt/libc.md. Proposed order, asked of #8150's author in this PR's conversation: this PR first; #8150 then keeps both calls when it rebases and lists #12036 underDepends on. If #8150 goes first instead, this PR rebases after it andDepends onbecomes #8150.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 MarcZX10 on 2026-10-27T19:45:39Z.
Review: DISMISSED
Reviewed exact head
9f16485c4188efc23cae31f13be4f6012fd4ce59. The Windows-specific flag update is narrowly placed after a successful flush, and the regression matrix exercises seven write APIs crossed with four read paths. The author reports a main control failing before the change and passing after it, plus repeated stream tests; hosted Windows and the Linux/macOS relinker and supporting checks pass. I did not independently reproduce the Windows runtime test.One repository overlap item remains: the exact pairwise merge preview with open #8150 reports a content conflict in
core/libs/tests/FileStreams.cpp. Both PRs add calls in that test executable'smain, so this is an integration-order issue even though the test behaviors are distinct.CONTRIBUTING.mdrequires agreement with the overlapping PR author and aDepends onentry for this case. Please agree with #8150's author which test change lands first and record the order in this checklist; it currently saysDepends on: none. The other shared-path hunks I checked (#5021, #7386, #7447 and #10324) do not conflict in the changedSwitchToReadingUnlockedbehavior.I am requesting changes for that coordination requirement, not for a defect in the stream fix.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-28T09:59:26Z.
Review: DISMISSED
The fix and the test look right: clearing
UcrtWritingafter a successful flush inSwitchToReadingUnlocked(FileStream.hpp:285) matches FreeBSD's__srefill, and the 7 writes x 4 reads matrix pins the position and file contents.One thing left, the one @MarcZX10 requested: #8150 also adds a call in
mainofcore/libs/tests/FileStreams.cppand conflicts with this PR there (it already conflicts withmain). Please agree with its author which goes first and record it underDepends on(this PR looks like the natural first one,Depends on: nonestays if so and #8150 lists #12036). Once that is in the description this can go to the merge queue.