libc: setvbuf with _IOLBF on Windows does not flush at newlines, so line-buffered output stays hidden until the buffer fills; fix branch ready #5391

Closed
opened 2026-10-13 18:37:09 +00:00 by husseinkad · 1 comment
husseinkad commented 2026-10-13 18:37:09 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T18:37:09Z.


On Windows, setvbuf(stream, buf, _IOLBF, size) does not give line buffering. The host C runtime treats _IOLBF as _IOFBF, so a stream set to line buffering keeps its output until the buffer fills or the stream closes. FreeBSD flushes at each newline. Writing "ab\ncd" to a line-buffered file leaves 0 bytes visible to a second handle here, where FreeBSD shows "ab\n". A guest that uses line buffering so a log file or a pipe reader sees complete lines as they are written gets nothing until much later.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-setvbuf-line-buffering (four commits on a recent main: the change and three follow-ups from review; libc/src/StandardStreams.cpp, the stream write path in FileStream, tests/GuestStandardStreams.cpp, and a debt entry in docs/dev/debt/libc.md). The host stream stays fully buffered. A per-stream flag, set only when setvbuf succeeds with _IOLBF, flushes through the last newline of each write (fputc, putc_unlocked, fputs, fwrite, vfprintf, fputwc/fputws, printf and wprintf on stdout, and puts). Reads are untouched. The write now holds the stream's recursive lock for the whole call, so two threads writing to one stream keep each write intact, and the flag is cleared by Reopen, Close and a later setvbuf(_IOFBF/_IONBF).

Checks:

  • The new tests (visibility after "ab\ncd" is "ab\n", the flag cleared by freopen, and a two-thread test of 2×20000 fputs) fail on main or with the fix removed and pass with it; guest_standard_streams and file_streams pass.
  • Four review rounds. The first version mapped _IOLBF to unbuffered output and was 56 times slower (1M fputc took 3,665 ms against 65 ms) and made reads 25 times slower; the final design costs nothing without newlines and one flush per line otherwise. Timings for 1M fputc (final head): _IOFBF 43 to 111 ms, _IOLBF with no newlines 44 to 115 ms, and with a newline every 80 characters 81 to 210 ms (one flush per line), over several runs on a loaded machine.
  • Concurrency: 2, 4 and 6 threads mixing fputc, fputs, fwrite, fprintf and putc_unlocked kept every 9-byte unit intact (an _IOFBF stream was already atomic), with and without flockfile held (no deadlock); the lock is released on every return path, including a write to a read-only stream and a flush failure. Recycling a FileStream across fclose/fopen and freopen (also a failed one) leaves no stale flag. _IOLBF with size 1 now returns 0 (main failed with EINVAL); sizes 0 and 1 use the CRT's default-size buffer, and a guest buffer of 16 or 4096 bytes holds the tail.
  • check_conventions.py is clean and pr_overlap.py found no overlap.

Not covered: _IOFBF with size 1 still fails with EINVAL (the CRT rejects it; FreeBSD accepts it); with a size-2 buffer the tail leaks because the buffer is full. Open PRs #4627 (the putc_unlocked line) and #4804 (puts returning '\n') edit the same lines, so a textual rebase is needed; they do not conflict semantically, but the puts resolution must flush first and then return '\n'. Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5835711616 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5391). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T18:37:09Z. --- On Windows, `setvbuf(stream, buf, _IOLBF, size)` does not give line buffering. The host C runtime treats `_IOLBF` as `_IOFBF`, so a stream set to line buffering keeps its output until the buffer fills or the stream closes. FreeBSD flushes at each newline. Writing "ab\ncd" to a line-buffered file leaves 0 bytes visible to a second handle here, where FreeBSD shows "ab\n". A guest that uses line buffering so a log file or a pipe reader sees complete lines as they are written gets nothing until much later. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-setvbuf-line-buffering (four commits on a recent main: the change and three follow-ups from review; `libc/src/StandardStreams.cpp`, the stream write path in `FileStream`, `tests/GuestStandardStreams.cpp`, and a debt entry in `docs/dev/debt/libc.md`). The host stream stays fully buffered. A per-stream flag, set only when `setvbuf` succeeds with `_IOLBF`, flushes through the last newline of each write (`fputc`, `putc_unlocked`, `fputs`, `fwrite`, `vfprintf`, `fputwc`/`fputws`, `printf` and `wprintf` on stdout, and `puts`). Reads are untouched. The write now holds the stream's recursive lock for the whole call, so two threads writing to one stream keep each write intact, and the flag is cleared by `Reopen`, `Close` and a later `setvbuf(_IOFBF/_IONBF)`. Checks: - The new tests (visibility after "ab\ncd" is "ab\n", the flag cleared by `freopen`, and a two-thread test of 2×20000 `fputs`) fail on main or with the fix removed and pass with it; `guest_standard_streams` and `file_streams` pass. - Four review rounds. The first version mapped `_IOLBF` to unbuffered output and was 56 times slower (1M `fputc` took 3,665 ms against 65 ms) and made reads 25 times slower; the final design costs nothing without newlines and one flush per line otherwise. Timings for 1M `fputc` (final head): `_IOFBF` 43 to 111 ms, `_IOLBF` with no newlines 44 to 115 ms, and with a newline every 80 characters 81 to 210 ms (one flush per line), over several runs on a loaded machine. - Concurrency: 2, 4 and 6 threads mixing `fputc`, `fputs`, `fwrite`, `fprintf` and `putc_unlocked` kept every 9-byte unit intact (an `_IOFBF` stream was already atomic), with and without `flockfile` held (no deadlock); the lock is released on every return path, including a write to a read-only stream and a flush failure. Recycling a `FileStream` across `fclose`/`fopen` and `freopen` (also a failed one) leaves no stale flag. `_IOLBF` with size 1 now returns 0 (main failed with `EINVAL`); sizes 0 and 1 use the CRT's default-size buffer, and a guest buffer of 16 or 4096 bytes holds the tail. - `check_conventions.py` is clean and `pr_overlap.py` found no overlap. Not covered: `_IOFBF` with size 1 still fails with `EINVAL` (the CRT rejects it; FreeBSD accepts it); with a size-2 buffer the tail leaks because the buffer is full. Open PRs #4627 (the `putc_unlocked` line) and #4804 (`puts` returning `'\n'`) edit the same lines, so a textual rebase is needed; they do not conflict semantically, but the `puts` resolution must flush first and then return `'\n'`. Only Windows was built and run. AI-assisted: yes
oneandonlydean commented 2026-10-14 06:55:07 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:55:07Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. #5394 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162722947 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5391#issuecomment-6162722947). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:55:07Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. #5394 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
Sign in to join this conversation.
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#5391
No description provided.