Skip to content

fix(libc): leave write mode when an unbuffered update stream switches to reading on Windows - #12036

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
Ragbie:fix/unbuffered-read-after-write
Nov 9, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
Ragbie:fix/unbuffered-read-after-write

Conversation

@Ragbie

@Ragbie Ragbie commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What

On Windows a read that follows a write on an unbuffered update stream fails. With 0123456789 opened r+b and setvbuf(_IONBF), fputc('A') and fputc('B') then fgetc return EOF and set the error indicator, where FreeBSD returns '2'. fread, getc_unlocked, ungetc followed by fgetc, and an fflush before the read fail the same way. Writes that reach the UCRT through fputc, putc_unlocked or fputwc leave the stream in write mode; fwrite and fputs of more than one byte bypass it and already work.

FreeBSD 11.4's __srefill flushes a stream that is writing, clears __SWR and sets __SRD before it reads, and __ungetc does the same. FileStream::SwitchToReadingUnlocked (from #8226) flushes and adds _IOREAD, but leaves clearing _IOWRITE to the UCRT's _fflush_nolock. That only happens when the flush writes buffered bytes: a stream without a buffer returns 0 at once with _IOWRITE still set, and _filbuf then refuses to read.

The change clears _IOWRITE after 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_streams gets CheckUnbufferedStreamReadsAfterWrites. On an unbuffered r+b stream over 0123456789, each of fwrite, fputs, fputc, putc_unlocked, fprintf, fputwc and fputc with fflush writes AB. Then fgetc, getc_unlocked, a one-byte fread, or ungetc('Z') with two fgetc must read '2' without the error indicator, ftell must give 3, and the file must hold AB23456789.
  • Without the fix (main's sources): file_streams fails with fgetc after a write on an unbuffered r+ stream. The fwrite and fputs rounds before it pass.
  • With the fix:
    • file_streams and the guest_standard_streams tests pass with --repeat until-fail:5.
    • The 97 tests matching stream|stdio|printf|wide|format|fopen|freopen|fclose|fwrite|fread|putw|getw|ungetc|gets|scanf|puts|libc|random|fwide|setvbuf pass with --repeat until-fail:3, except guest_wide_formatting. That test fails on main here too: %Z gives the host's non-ASCII zone name, which fix(libc): convert Windows time zone names to UTF-8 #10987 addresses.
    • Full ctest on main c547deaac: 99% tests passed, 3 tests failed out of 2669.
      • guest_wide_formatting, as above.
      • agc_driver_image_plane_address_on_line also fails on main on this machine.
      • agc_driver_mesh_barycentric_emulation fails every time on this machine's NVIDIA GPU with the pixel shader did not emulate barycentrics. feat(recompiler): read I/J from barycentric built-ins on MoltenVK #9193 changed that test today. It does not use libc streams.
    • check_skips.py reports 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:

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: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libc/include/FileStream.hpp
PR 7386 core/libs/tests/FileStreams.cpp
PR 7447 core/libs/prx/libc/include/FileStream.hpp
PR 8150 core/libs/tests/FileStreams.cpp
PR 12682 core/libs/prx/libc/include/FileStream.hpp
PR 12688 core/libs/prx/libc/include/FileStream.hpp
PR 12700 core/libs/prx/libc/include/FileStream.hpp
PR 12965 core/libs/prx/libc/include/FileStream.hpp
PR 17691 core/libs/prx/libc/include/FileStream.hpp
core/libs/tests/FileStreams.cpp

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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's main, so this is an integration-order issue even though the test behaviors are distinct. CONTRIBUTING.md requires agreement with the overlapping PR author and a Depends on entry for this case. Please agree with #8150's author which test change lands first and record the order in this checklist; it currently says Depends on: none. The other shared-path hunks I checked (#5021, #7386, #7447 and #10324) do not conflict in the changed SwitchToReadingUnlocked behavior.

I am requesting changes for that coordination requirement, not for a defect in the stream fix.

@github-actions github-actions Bot added the libc label Oct 27, 2026
@B-Howell

Copy link
Copy Markdown
Contributor

Independent Windows (AMD) and Linux results.

Head 9f16485c4188efc23cae31f13be4f6012fd4ce59 merged onto main 951399570d4a43e714ee10ed1f9d3ee8c796e749 (clean), alone in the tree.

Windows: Windows 11 Pro 26300, AMD Ryzen 7 7800X3D, WinLibs GCC 15.2.0 r7, Ninja Release, BUILD_TESTING=ON, libs rebuilt.

  • ctest -R '^file_streams$' --repeat until-fail:10: passes all 10 runs (0.15 to 0.3 s).
  • Control: core/libs/prx/libc/include/FileStream.hpp restored from main, tests kept: file_streams aborts with 0xc0000409 on the first run. I did not capture which check failed.

Linux: Ubuntu 22.04 under WSL2, GCC 11.4.0, Release. ctest -R '^file_streams$|^guest_standard_streams' --repeat until-fail:5: file_streams and the four guest_standard_streams* tests pass all 5 runs. The new CheckUnbufferedStreamReadsAfterWrites cases are in the shared test file. They also pass on glibc. No Linux control, since the fix is in the Windows UCRT path.

Not tested: the wider stream and stdio set from the description, a tree with both #12036 and #12057 (both edit FileStream.hpp), macOS, any title.

AI-assisted (Claude Code).

@adriadam10 adriadam10 self-assigned this Oct 28, 2026

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix and the test look right: clearing UcrtWriting after a successful flush in SwitchToReadingUnlocked (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 main of core/libs/tests/FileStreams.cpp and conflicts with this PR there (it already conflicts with main). Please agree with its author which goes first and record it under Depends on (this PR looks like the natural first one, Depends on: none stays if so and #8150 lists #12036). Once that is in the description this can go to the merge queue.

@Ragbie

Ragbie commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 @adriadam10 Thanks. @Dellz36, #8150 and this PR conflict in main of core/libs/tests/FileStreams.cpp, where both add a call; the tested behaviours are separate.

Proposed order: this PR first, since it merges cleanly into main and #8150 needs a rebase onto main either way (git merge-tree reports conflicts in StandardStreams.cpp, FileStreams.cpp and docs/dev/debt/libc.md). On that rebase #8150 keeps this PR's call next to its own and lists #12036 under Depends on. If you prefer #8150 to go first, I will rebase this PR after it and set Depends on: #8150. The description now states this order.

AI-assisted: yes (Claude Code)

@MarcZX10

Copy link
Copy Markdown
Contributor

Re-checked — changes still requested

I agree that #12036 should land before #8150 because it merges cleanly to current main and #8150 needs a broader rebase; the other author still needs to confirm that sequence before the overlap rule is satisfied.

What I checked

Results

Item Result
#12036 checklist
Raw valueDepends on: none
#8150 status
Raw valuestate=OPEN; checklist says no merge order has been agreed
#8150 author confirmation of the proposed order Not present in the current thread

Findings

Blocking

The suggested #12036-first sequence is reasonable, but it is still a proposal. Please get #8150's author's confirmation and have #8150 record Depends on: #12036 and the rebase plan; this PR can remain Depends on: none if both authors agree it lands first. The current changes-requested review remains in place until the order is recorded and agreed.

Sources

Not verified

I did not rerun the test matrix; this reply addresses only the requested integration order, and no code head changed.

To get this to a good state

Confirm the order with #8150's author and record it in both PR descriptions before either is merged.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@Ragbie reviewed at 9f16485: PASS. On Windows, SwitchToReadingUnlocked now clears _IOWRITE after a successful flush and before setting _IOREAD, as FreeBSD's __srefill does and matching SwitchToWritingUnlocked. A failed flush and buffered streams are unchanged. The new CheckUnbufferedStreamReadsAfterWrites cases (7 writes x 4 reads) are in the registered file_streams test.

@adriadam10 exact-head CI is green (run 40570192797); we'll merge this in the next train unless you'd rather merge it yourself.

@MarcZX10

Copy link
Copy Markdown
Contributor

The implementation/test PASS is useful, but the overlap blocker I raised remains: #8150 is still open and its author has not confirmed the proposed #12036-first order or recorded Depends on: #12036 on that PR. Your proposal to land this PR first is reasonable; please keep the merge order recorded and agreed on both sides before it enters the merge queue.

@oneandonlydean oneandonlydean self-assigned this Oct 29, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@MarcZX10 Agreed, the order should be recorded on both sides before this merges. We'll keep this PR out of the merge train until #8150's author confirms #12036 first and records Depends on: #12036 there, or says they would rather go first, in which case this PR rebases after #8150 lands. The implementation and test review at 9f16485 stands either way.

@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 9f16485c41, merged onto main 990481cdbf (2b130cfd7e, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • file_streams passes in 3 of 3 runs, none skipped.

AI-assisted: yes.

@oneandonlydean
oneandonlydean dismissed stale reviews from MarcZX10 and adriadam10 November 9, 2026 09:54

Met: the PR body now records the ordering with #8150 (line 29) and keeps Depends on: none.

@oneandonlydean
oneandonlydean merged commit 8551751 into boykopovar:main Nov 9, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants