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
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 husseinkad on 2026-10-13T18:37:09Z.
On Windows,
setvbuf(stream, buf, _IOLBF, size)does not give line buffering. The host C runtime treats_IOLBFas_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 inFileStream,tests/GuestStandardStreams.cpp, and a debt entry indocs/dev/debt/libc.md). The host stream stays fully buffered. A per-stream flag, set only whensetvbufsucceeds with_IOLBF, flushes through the last newline of each write (fputc,putc_unlocked,fputs,fwrite,vfprintf,fputwc/fputws,printfandwprintfon stdout, andputs). 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 byReopen,Closeand a latersetvbuf(_IOFBF/_IONBF).Checks:
freopen, and a two-thread test of 2×20000fputs) fail on main or with the fix removed and pass with it;guest_standard_streamsandfile_streamspass._IOLBFto unbuffered output and was 56 times slower (1Mfputctook 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 1Mfputc(final head):_IOFBF43 to 111 ms,_IOLBFwith 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.fputc,fputs,fwrite,fprintfandputc_unlockedkept every 9-byte unit intact (an_IOFBFstream was already atomic), with and withoutflockfileheld (no deadlock); the lock is released on every return path, including a write to a read-only stream and a flush failure. Recycling aFileStreamacrossfclose/fopenandfreopen(also a failed one) leaves no stale flag._IOLBFwith size 1 now returns 0 (main failed withEINVAL); 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.pyis clean andpr_overlap.pyfound no overlap.Not covered:
_IOFBFwith size 1 still fails withEINVAL(the CRT rejects it; FreeBSD accepts it); with a size-2 buffer the tail leaks because the buffer is full. Open PRs #4627 (theputc_unlockedline) and #4804 (putsreturning'\n') edit the same lines, so a textual rebase is needed; they do not conflict semantically, but theputsresolution must flush first and then return'\n'. Only Windows was built and run.AI-assisted: yes
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.