fix(libkernel): append O_APPEND writes atomically on Windows #5831
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5831"
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-14T12:50:30Z.
What
On Windows,
NativeOpenhands the file to the C runtime with_O_APPEND. The runtime appends by seeking to the end and then writing, two separate steps. When two descriptors append to the same file at once, both seek to the same end and the second write overwrites the first. FreeBSD'svn_writesetsIO_APPENDfor anO_APPENDfile and writes at the end under the vnode lock, so appends never collide there.A standalone Windows program shows the size of the loss: 4 threads each append 2,000 lines through their own descriptor. With the C runtime's
_O_APPEND, 6,999 of the 8,000 lines arrive. WithWriteFileat offset0xFFFFFFFFFFFFFFFF, all 8,000 arrive. That offset is Windows' atomic write to end of file, "functionally equivalent" toFILE_APPEND_DATA, and it leaves the file pointer after the data, as FreeBSD leaves the offset at the end.The change:
sceKernelOpenalready records the guest open flags withFile::RememberDescriptor, and those flags are shared acrossdup,dup2andF_DUPFD. The newFile::Appends(fd)reads itsO_APPENDbit.NativeWrite(sceKernelWrite,write,_write) andNativeTransfer(writev,sceKernelWritev) send writes on such a descriptor through the newFile::WriteAtEnd. It callsWriteFileat the end-of-file offset, only for disk files. Pipes and devices keep the existing path.writevon anO_APPENDdescriptor gathers its buffers into one write, because FreeBSD appends the whole vector at once. Until now, each buffer was written separately, so other writers could land between them. This is also in the Windows code: Linux calls the host'swritev, which already appends the whole vector at once.pwriteis unchanged.guest_append_writescovers four concurrent writers on one file:sceKernelWriteon its own descriptor;writeon another descriptor;sceKernelWriteon adupof the first descriptor;writev.Each writes 1,500 fixed-length lines. The test checks that the file holds exactly 6,000 intact lines, each exactly once. It then checks that a write after
lseek(0, SEEK_SET)lands at the end and leaves the offset after it. Onmainit fails at the size check in 10 of 10 runs; with the change it passes.Overlap with #5833
#5833 holds the descriptor's position lock across a whole vectored transfer and splits
NativeTransferinto a lock-freeNativeTransferBufferand a locking wrapper. The two changes don't overlap in purpose: #5833 keeps the aliases of one descriptor from interleaving, and this PR makesO_APPENDwrites atomic across independent opens. Agreed with @Ondra97 on #5833: #5833 lands first, and this PR is then rebased onto it with two adjustments:File::Appendsbranch moves intoNativeTransferBuffer;writevgathering callsNativeTransferBuffer, because with #5833TransferIovecsalready holds the lock, and callingNativeTransferthere would lock the samestd::mutextwice.With
main84b01f9c, #5833 and this PR resolved that way,guest_append_writes,guest_file_vector(with #5833's concurrent records) andguest_kernel_fcntlpass 5 of 5 runs.Tested
Windows 11 Pro (26200), WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON:main9a3e2ac4, the full build and fullctestpass: 1172 tests, 10 of them skipped by their own environment checks.libkernel.prxsucceeds.main8ffbb492, whose new commits don't touch these files,guest_append_writes,guest_kernel_fcntl,guest_file_vectorandguest_system_configurationpass (3 runs).Java 25 LAN / DNS (PPSA99869, #5501) was run with #5563, #5643 and #5666 stacked, which take it past its remaining missing imports (#5502 and #5187 are merged), and relinked with
--trap-sdk-syscall-stubs. It keeps a trace in/download0/java25-vm-trace.log, which its threads append to through separateO_APPENDdescriptors. During the DNS stage (two threads), without this change 86 lines lose their start and 99 lines lose their newline and run into the next. With it, no line is cut and no newline is lost. The 174 lines that still join are each followed by an empty line, because the title writes a line and its newline separately.Linux not tested; nothing changes there.
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-15T06:42:42Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libkernel/File/src/Stdio.cppL1088Some 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/Open.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/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Stdio.cppImported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-15T08:24:25Z.
Windows cross-check of head
17d69f14a, merged ontomaind92a8dad3: clean merge, no conflicts.Windows 11 Pro 26220, Ryzen 7 5700X, RTX 4070 (driver 596.21), WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release.
libspass. No warnings in the files this PR touches; the only warnings are the onesmainalso has.guest_append_writes,--repeat until-fail:3: 3 of 3 pass.guest_append_writes_tests.exewithmain'slibkernel.prx/libc.prxfirst onPATHfails 11 of 13 runs withappend check failed at line 88(the size check). The file from the last failing run holds 154,690 of 192,000 bytes and 4,067 of 6,000 intact lines. With this PR's libraries it passes 13 of 13.guest_kernel_fcntl,guest_file_vector,guest_positioned_race,guest_dup,guest_aio*,guest_sendfile,guest_file_lock_*,guest_host_streams,file_streams, and others),--repeat until-fail:3: 31 pass 3 of 3.guest_file_lock_processis flaky on this host onmaintoo: it gives each of its 24 child processes 3 s, andmain's own test binary withmain's libraries also exits0xC00004093 of 3 here. It never writes.ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip': 807/815 under load. Six of the eight failures pass when rerun alone.guest_file_lock_processis the flaky test above, andhardware_oracle_compare(python, 20 s limit) needs 39.9 s on this loaded host.tools/check_conventions.py --base origin/mainandgit diff --check: pass.Titles, 60 s each, relinked once with
main's relinker and run withmain's or this PR's libraries:mainmain+ #5831/data/prosperolight/logs/prosperolight-launcher.log, openedO_WRONLY | O_APPEND)ProsperoLight | FPS: 8.63 (357), log 2,194 lines, intactProsperoLight | FPS: 6.50 (353), log 2,176 lines, intactHOME)PS5MC (Kodi 22) | FPS: 9.77 (439),kodi.log314 lines, intactPS5MC (Kodi 22) | FPS: 12.10 (574),kodi.log314 lines, intactOpenNOW PS5 - Prototype | FPS: 9.08 (656)OpenNOW PS5 - Prototype | FPS: 16.91 (839)There is no stop line in any run. FPS varied with other load on the machine.
One observation outside this PR's path: PS5MC's
download0/ps5-opengl.log(its stdout/stderr) has lines whose start was overwritten on both builds (5 onmain, 4 with the PR), for example[debug:0] [kodi[s2c/Open.cpp:215 sceKernelOpen] ...followed by026-10-15 10:12:51.555 T:100001 .... That file is not opened throughsceKernelOpen. libc'sfopen/freopen/fdopenappend streams still append through the C runtime (FileAndHeap.cpp), so streams opened that way keep the old behaviour. A possible follow-up, not a reason to widen this PR.Verdict: confirmed on Windows. Concurrent appends through separate descriptors no longer lose data, and there are no test or title regressions.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by ahmad3a4 on 2026-10-15T08:34:31Z.
Thanks @screamm for the cross-check, and for the negative control with main's libraries.
On the overlap with #6228 flagged above: it's textual only. Both PRs change neighbouring lines in
TransferIovecs. #6228 replaces theDirectoryDescriptorPathcheck withIsDirectoryDescriptor, and this PR adds the O_APPEND gathering right below it, so whichever lands second keeps both. #6228 doesn't touch the open-flag registry thatFile::Appendsreads. I merged this head onto #6228 (head as of today) with that resolution. It builds, and these pass with--repeat until-fail:3: guest_append_writes, guest_kernel_sys, guest_kernel_fcntl, guest_file_vector, guest_directory, guest_directory_buffer, guest_directory_rewind, guest_directory_close, guest_kernel_rename, guest_positioned_race, guest_dup, guest_host_streams, file_streams, guest_working_directory and guest_file_lock_duplicate (15 of 15). @Ondra97 nothing needed on your side. The agreed order with #5833 stays.On the libc streams: agreed, that's a separate path.
fopen,freopenandfdopenin an append mode give the C runtime a descriptor with_O_APPEND, so stream flushes still seek to the end and then write. I'll take it as a follow-up PR.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-15T20:33:19Z.
Review: COMMENTED
Linux run (Ubuntu 26.04, Intel i7-8700, GCC 15.2, full build, non-GPU tests), head
17d69f14amerged ontomaince991f1ec. The description says Linux was not tested.guest_append_writes, which is registered on every platform, passes 30 of 30 repeated runs.guest_file_vector,guest_kernel_fcntl,guest_filesystemandguest_file_timestampspass too.main(the PR's test kept),guest_append_writesalso passes 30 of 30. That matches the description: the hostwrite/writevon Linux already append atomically, so the new test is safe on the Linux CI and only guards the Windows path.Non-GPU suite (
ctest -E "^agc"): same result asmain; onlyfile_writer_binaryandfile_writer_textfail, and they fail onmaintoo on this machine (Ubuntu 26.04 coreutils).Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T18:28:06Z.
Rebased onto
mainafter #5833: theFile::Appendsbranch now lives inNativeTransferBuffer, and thewritevgathering callsNativeTransferBuffer(TransferIovecsalready holds the position lock, soNativeTransferwould lock it twice). Scoped build and ctest on Linux: 176/176 pass.WriteAtEndis Windows-only; the negative control is @screamm's (main's libraries failguest_append_writes11 of 13 runs, this head passes), and the Windows job of the full Build covers the rebase before merge.