fix(libkernel): make close wait for a positioned transfer on the same descriptor #5938
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5938"
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 xXPBSXx on 2026-10-14T16:25:17Z.
What
Cat Quest III (PPSA15065) aborts about 15 s after start with:
(
PPSA15065_rN2/run_175915.errline 10; the bracketed fields come from a local debug build that extends the message, not from this change.) The read itself succeeded (readOk=1) and the restore failed withrestoreError=6(ERROR_INVALID_HANDLE), so the handle was released while the transfer was in flight.On Windows
sceKernelPread/sceKernelPwriterun asSetFilePointerEx+ReadFile/WriteFile+SetFilePointerExback under the per-descriptor positioned lock (File::LockPositionedTransfer).sceKernelCloseandclose_nid_postfixdid not take that lock, so a close on another thread could free the handle in the middle of a transfer.NativePositionedalso fetched the handle before it took the lock, so a transfer that began after a finished close used a stale handle and failed with ESPIPE instead of EBADF.sceKernelCloseandclose_nid_postfixtake the descriptor's positioned lock before they release anything (Windows only), so a transfer in flight finishes and the close follows.NativePositionedtakes the lock before_get_osfhandle, so a transfer after a close sees EBADF.guest_positioned_close(Windows): 60 rounds of a 32 MiBsceKernelPreadin one thread andsceKernelClosefrom the main thread about 300 us later; it requires no exception,close == 0, and the read either complete or EBADF, and at least one round must overlap.Title evidence: a trace run on the same build (
PPSA15065_rN2/run_180943.err, 120 s, no crash) has 539 lines[fd0trace] close fd=0from 7 distinct thread ids (callerapp.exe+0x1401327) and 138[fd0trace] positioned enter fd=0lines (counted with Python); the[fd0trace]lines come from a local trace build, not from this change. So the title closes descriptor 0 repeatedly while it reads from it.Tested
Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on
maine34c47921+ this commit:guest_positioned_closefails without the fix in 4 of 4 runs (3 xwhat(): NativePositioned: cannot restore the file offset, 1 x result -2147352547 = ESPIPE,check failed at line 67) and passes with the fix in 5 of 5 runs.python tools/check_conventions.py --base upstream/mainwith this title and body passes.maina453108cfwith a local debug build); the effect on the title is therefore not shown by a run with the fix.Overlaps: #5892 (Cat Quest III, open) refuses to close the standard descriptors (#4909), which removes the fd 0 reuse that produced this crash; with it the title may no longer reach this path. This change is independent of it (it fixes close against a running transfer for any descriptor) and conflicts textually with #5892 in
Open.cpp(merge-tree). #3295 (open) keeps the host standard streams open on close and edits the same close functions; merge-tree conflicts only indocs/dev/TechnicalDebt.md. How the console behaves when a close races a positioned read is not verified; this change assumes the transfer completes first.Checklist
main; overlaps with #5892 and #3295 are listed aboveNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repositoryAI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-15T04:13:30Z.
Title run with this change (Cat Quest III, PPSA15065), update to the "not repeated with this change" line in the description.
Tree:
upstream/maina077a88df5bc4ec26a3049aa721ac37ec8f02305+ this headabe6ba82b(merge commit74d0c56281ecbf3e56892f1bee52d7f1cc4374b8; without #5892 and without #3295). Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, RTX 4080 SUPER; freshlibs+ relinker of that tree (build 912 steps), title relinked with it,run_title.ps1 -Seconds 600, Enter/Space posted to the game window until +240 s.Result (
PPSA15065_rP1/run_060108.err, 13 lines, counted with Python): nowhat(), noFATAL, noNativePositioned: cannot restore the file offset, noVulkan result -4; the app ran 06:01:30 to 06:11:30 (the 600 s limit); 20 samples: CPU 8.8 s to 925.5 s, 150 threads, working set 3.2 GB, window responding.For comparison, without this change on
maina453108cfthe title aborted after about 15 s in 2 of 2 runs (run_175633.err,run_175915.err), while a 120 s trace run on the same build (run_180943.err) did not abort, so the abort is a race. One successful 600 s run with the change is therefore a hint, not a proof. Not tested: Linux, other GPUs, a second run, a picture.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T06:42:50Z.
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/Open.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Open.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppImported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-15T17:14:16Z.
@adriadam10 a short request for review of this PR. Head
abe6ba82bisMERGEABLE, the Conventions, Eligibility and Intake checks are green, and the description names the title (Cat Quest III, PPSA15065), the log line and the test that fails without the change. If a review or a full Build run is possible, I will answer any finding the same day.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T18:55:19Z.
Thanks for the fix. One thing added on top of your commit:
NativePositionedTransfer(Stdio.cpp) no longer takesLockPositionedTransferitself. Since65e95f7139its callers (NativePositioned_nid_no_patchand the vectored transfer) already hold that non-recursive per-descriptor mutex, so the extra lock would deadlock every positioned transfer once merged onto currentmain. The wrapper takes the lock before_get_osfhandle, so a transfer after a close still sees EBADF.Checked: rebase onto current
main, libkernel build and ctest on Linux (175 tests green).guest_positioned_closeis Windows only, so the full Windows Build must pass before merging.