fix(libc): append stream writes atomically on Windows #6287
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6287"
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 ahmad3a4 on 2026-10-15T09:03:19Z.
What
On Windows,
fopenandfreopenin an append mode (a,ab,a+, ...) give the C runtime a descriptor with_O_APPEND. The runtime appends by seeking to the end and then writing, two separate steps. When two streams on separate descriptors flush at once, both seek to the same end and the later write overwrites the earlier one. FreeBSD'sfopenandfreopenopen these modes withO_APPEND, andvn_writewrites eachwrite(2)at the end of the file under the vnode lock, so flushes never overwrite each other. This is the stream side of #5831, which covers descriptors opened withsceKernelOpen. @screamm pointed it out there.The change:
fopenopens the append modes withFILE_GENERIC_WRITEminusFILE_WRITE_DATA, plusGENERIC_READfora+. Windows documents that withFILE_APPEND_DATAand withoutFILE_WRITE_DATA, "write operations will not overwrite existing data": every write on such a handle goes to the end of the file, wherever the file pointer is, and the pointer ends up after the data.freopenin an append mode moves an append-only duplicate of the handle onto the stream's descriptor, the same wayfdopenalready moves an_O_APPENDduplicate (now one sharedMoveAppendingHandle). For descriptors 0 to 2 it keeps the host standard handle, asFileStream::Reopendoes.fdopenis unchanged. FreeBSD'sfdopendoes not setO_APPENDeither; it only seeks before each write (__SAPP), which is what the existing code does.FILE_WRITE_DATAcannot truncate, and without read access it cannot take a byte-range lock. FreeBSD allows both on these descriptors.ftruncateon an append-only handle now truncates throughReOpenFile(GENERIC_WRITE)(asfsyncalready reopens), andflocklocks through a reopened handle kept in the lock state, which closes when the last descriptor sharing that state is forgotten. Without this,flockon anastream would throw, becauseERROR_ACCESS_DENIEDis not a lock conflict. All other descriptors take the existing paths. File-backedmmapis not supported on Windows, so it is not affected.guest_append_streams: four threads append 1,500 fixed-length lines each to one file throughfopen("a"),fopen("a+"),freopen("a")andfreopen("ab"), flushing after each line. The file must hold exactly 6,000 intact lines, each once. Then:a+stream reads from the start, and a write afterfseek(0, SEEK_SET)lands at the end withftellafter it;ftruncateon anastream's descriptor truncates, and the next write appends after the new end;flockon anastream's descriptor excludes a second open of the file, andfclosereleases it.With main's libc and libkernel it fails at the size check in 10 of 10 runs; with the change it passes 10 of 10.
Tested
Windows 11 Pro (26200), i5-13400, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON, on main721db55:ctest -j8 --timeout 120: 1,255 of 1,255 pass (15 skipped by their own environment checks).tools/check_conventions.py --base origin/mainandgit diff --check: pass.HOME. Its stdout and stderr, and AnyPS5's own trace lines, all go to/download0/ps5-opengl.logthrough separate append descriptors. I counted the places where one writer's output landed inside another's line:d92a8da[debug:0] [kodi] 2026-10-15 [src/Open.1pp:215 sceKernelOpen] ...followed by0 T:100001 ...[debug:0] [kodi] [0s5-cache] clflushopt=1 ...With the change, a line can still be split around another writer's line, for example
[debug:0] [kodi] 2026-10-1+[ps5-cache] clflushopt=1 cacheline=64+5 11:57:03.480 T:100002 .... That happens because the title writes one log line in more than onewrite, and FreeBSD splits it the same way. No bytes are lost.GENERIC_WRITEhandle and_O_APPEND, 3,124 and 3,179 of 8,000 lines arrive; with the append-only handle, 8,000 of 8,000. The same program showsfsyncworks on such a handle,ftellis at the end after each write, and_chsize_sandLockFileExfail on it, which is whyftruncateandflockreopen.Linux not tested. Nothing changes in the Linux build; the new test uses only guest functions, so it runs there too.
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 github-actions[bot] on 2026-10-15T09:27:08Z.
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.
core/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libc/src/FileAndHeap.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppImported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T12:58:39Z.
Cross-validation on Windows 11 Pro 26220, NVIDIA RTX 4070 (596.21), MinGW GCC 15.2, PR head
a431a12a4merged on main821c69e50:libs: pass, no warnings in the files this PR touches.guest_append_streams,--repeat until-fail:3: 3 of 3 pass.guest_append_streams_tests.exewith the libraries of a pre-PRmainbuild first onPATHfails 9 of 10 runs at the size check (append stream check failed at line 85); with this PR's libraries it passes every run. (Library swap, not a source revert.)fopen(path, "a")stream, 3000 variable-length lines each (83 bytes on average, up to about 160), once with a flush after every line and once with default buffering. With the PR all 6000 distinct lines are intact and the size is exact in 3 of 3 runs of each variant; withmain's libraries 3 of 3 flushed runs lose lines (4043, 5492 and 4078 of 6000 intact) and 2 of 3 unflushed runs do.guest_file_lock_*,guest_kernel_fcntl,guest_dup,guest_host_streams,file_position,guest_kernel_sync,guest_terminal, ...),--repeat until-fail:3: 18 pass 3 of 3.guest_file_lock_processaborts on this loaded host with and without the PR (5 alternating runs each: PR 0 of 5, pre-PRmain0 of 5; it starts 24 child processes with a 3 s limit each) and passed underctestin a calmer moment (11.45 s).ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip': 807 of 818 pass, 2 skipped, 9 fail (8 timeouts under load and theguest_file_lock_processabort above); all 9 pass when rerun serially.tools/check_conventions.py --base origin/mainandgit diff --check: pass.ENVIRONMENT_MODIFICATIONfor thelibkernelandlibcdirectories.No concerns. Concurrent
fopen/freopenappend streams no longer lose or overwrite lines, andftruncateandflockkeep working on them.Written with Claude Code assistance.
Imported from boykopovar/AnyPS5. Originally posted by ahmad3a4 on 2026-10-18T12:24:16Z.
Rebased onto main
3e3e8b9. The only conflict was inNativeFtruncate:0948f77(fix(libkernel): reject invalid ftruncate descriptors safely) now returnsEBADFfor an invalid handle and runs_chsize_swithout the invalid-parameter handler. The append-only branch now sits after those two checks, and the rest is main's version.Windows 11 Pro, WinLibs GCC 15.2.0 r7, Release: full build passes. Full
ctest -j8 --timeout 120: 1,479 of 1,479 pass; 5 of them timed out while another build was running on this machine and passed when rerun alone.guest_append_streamsand 10 related file and lock tests pass 5 of 5.tools/check_conventions.pypasses.Thanks @screamm for the cross-validation, and for the extra variable-length and unflushed runs.