fix(libc): drop the kernel state of a descriptor that fclose or freopen closes #10755
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10755"
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 Ragbie on 2026-10-24T22:13:17Z.
What
fcloseandfreopenclose the stream's descriptor through the host C library and leave libkernel's state for that descriptor number behind: its status flags, close-on-exec and pipe flags, the random-device set, archived files, JIT shared memory, equeue registrations and, on Windows, directory descriptors and file locks.sceKernelCloseandclosedrop all of it. The next descriptor that gets the same number, usually from the nextfopen, inherits it:sceKernelOpen(O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC),fdopen(fd, "a"),fclose: the nextfopen(path, "r+")gets the same number,fcntl(F_GETFL)returnsO_RDWR | O_APPENDandfcntl(F_GETFD)returnsFD_CLOEXEC. On Windows,sceKernelPwriteandsceKernelWriteon it append instead of writing at the offset.sceKernelOpen("/dev/urandom"),fdopen(fd, "r"),fclose:sceKernelReadon the next file with that number returns random bytes instead of the file's contents.freopen(path, "r+", stream)on such a stream behaves the same way.FreeBSD 11.4 closes the descriptor in both:
fclosecallscleanfile, which calls the stream's close function,__sclose, that is_close(fp->_file).freopenreplaces the old descriptor withdup2onto its number, or closes it. Either way the number comes back with the new file's flags andFD_CLOEXECclear.The change:
File::ForgetClosedDescriptorholds the drops thatsceKernelCloseandclosemade inline. Both call it, in the same order, and on Windows still under the positioned-transfer lock.LibcRegisterClosedDescriptors_nid_no_patch, asLibcRegisterDirectoryStreams_nid_no_patchdoes for directory streams). Begin drops the equeue registrations, takes the positioned-transfer lock on Windows and callsFile::ForgetClosedDescriptor; end releases the lock.FileStream::CloseandFileStream::Reopenhold it throughClosingDescriptor, so the host close runs under the same lock as insceKernelClose, and a concurrentpread/pwriteeither finishes before the close or sees the closed number.freopenreleases it right after closing the old stream, before it opens the new file: UCRT's stream allocation locks the free streams it scans, so holding the old stream's lock into the open can deadlock against another thread'sfopen. Without libkernel nothing is registered and there is no state to drop.ClosingDescriptortakes the stream lock before the positioned-transfer lock, the order a guestflockfilefollowed bylseekuses.File::LockPositionedTransferreturns a lock that co-owns its mutex, so adupthat reuses the number while the lock is held cannot free the mutex.FileStream::Closeclears the stream only after the hostfclosereturns, so a thread that holds the stream withflockfilecan stillfunlockfileit whilefclosewaits.Tested
Windows 11, WinLibs GCC 15.2.0:
guest_stream_close:main's libc and libkernel it fails on the status flags, close-on-exec, the positioned and plain writes, the random-device read and thefreopenstatus flags, close-on-exec and write.sceKernelPwriteon another thread while the main thread callsfclose, orfreopenthenfclose, 16 rounds; the write must complete or fail withEBADF, and nothing may land in the reopened file. When the lock is released beforefclose, it fails 3 of 3 runs:pwritegetsESPIPEbecause its handle is closed between_get_osfhandleandGetFileType.flockfileby a thread that then callslseekwhile another threadfcloses it. With the positioned-transfer lock taken before the stream lock it hangs until the ctest timeout.freopen500 times while another threadfopens andfcloses a second file. With the stream lock held into the open it hangs until the ctest timeout in 3 of 3 runs.--repeat until-fail:10(longest run 0.40 s).ctestafter rebasing ontomain8a29e0883:99% tests passed, 5 tests failed out of 2379.agc_write_tracking_metadata_commit,agc_host_import_cut_lifetime,agc_driver_depth_plane_partial_storeandagc_driver_image_plane_address_on_linefail onmainon this machine too, andguest_wide_formatting(wcsftime length matches strftime) fails the same way onmainplus an unrelated socket change.check_skips.py: all skips expected.public_headersand its compile batches pass: the test now includesprx/libkernel/include/Api.hpp,prx/libkernel/File/include/File.hpp,prx/libc/include/Api.hppandprx/libc/include/FileStream.hppinstead of declaring the 13 exports itself.mainbf77a9e9c, after #8520:File::ForgetClosedDescriptornow drops the directory-descriptor entry on every host, assceKernelCloseandclosedo since #8520, sofcloseandfreopendrop it too.guest_stream_closeadds a host directory closed throughfdopenandfclosewhose number the next file reuses (Windows tracks every directory). Full build, then the 134 tests matchingstream_close|public_headers|kernel_fcntl|append_writes|file_lock|freopen|fclose|stdio|stream|app_archive|directorypass, andguest_stream_closeandguest_kernel_app_archive_archivepass with--repeat until-fail:10. Fullctestwas not rerun for this rebase.Linux (Ubuntu 24.04 container, GCC 13.3.0):
guest_stream_closefails onmainand passes with the change, with--repeat until-fail:10. After the rebase,guest_stream_closeandpublic_headerspass with--repeat until-fail:10, and fullctestgives99% tests passed, 24 tests failed out of 2286; the same 24 fail onmainin this container (22agc_*on its lavapipe and kernel withoutUFFD_FEATURE_WP_ASYNC,pr_overlap_encodingandguest_system_load_exec_standard_error). On POSIX only app0.zar directories are tracked, soguest_kernel_app_archive_archiveadds one closed throughfdopenandfclose: with the call still under_WIN32it fails at the read of the file that reuses the number (EISDIR), and with this head it passes, withguest_stream_closeand the other twoguest_kernel_app_archive_*tests, with--repeat until-fail:3. Full Linuxctestwas not rerun after #8520.Overlaps:
FileStream::Close(archive detach); the two changes are independent.File::ForgetDirectoryDescriptorout of the_WIN32block insceKernelCloseandclose;File::ForgetClosedDescriptornow does the same.FileStreamclose and reopen lifecycle; agreed order: this PR first, #11831 rebases after it.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 MarcZX10 on 2026-10-24T22:22:06Z.
Review: CHANGES_REQUESTED
The descriptor-state cleanup is needed, and the regression test covers the stale flags and reused descriptor cases. I found a Windows synchronization gap in the close path:
FileStream::Close()callsForgetClosedDescriptor_nid_no_patch()and then callsstd::fclose()(core/libs/prx/libc/include/FileStream.hpp:306-310). The registered callback acquiresFile::LockPositionedTransfer(descriptor)atcore/libs/prx/libkernel/File/src/Open.cpp:275-280, but that lock is destroyed when the callback returns, beforefcloseactually closes the CRT descriptor. The WindowssceKernelClose/closepaths keep the same lock alive across both state cleanup and the native close (Open.cpp:330-341,Stdio.cpp:728-749). Thus a concurrent positioned read/write can acquire the descriptor lock in the gap and race the stream close or descriptor reuse; the cleanup callback currently does not provide the same serialization guarantee.Could the libc/libkernel handoff keep the positioned-transfer lock held until the underlying close/replacement has completed (for example, through a close wrapper/callback whose scope includes
fclose/freopen), and add a Windows regression that coordinates a positioned transfer withfclose/freopen? The current sequential test does not exercise this interleaving. I have not run a local Windows concurrency reproduction, so this is based on the lock scopes and the existing close path.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T23:05:23Z.
Review: APPROVED
Reviewed exact head
512967154daeaa0a830cf55f8c9e6a661deee46b. The begin/end close handoff now keeps the Windows positioned-transfer lock alive through the nativefclose/replacement, and the stream lock is acquired before it, matching theflockfilethenlseekorder. The added regression exercises the close/write race, stream-lock/lseek interaction, descriptor reuse andfreopenconcurrent with stream creation. The author reports the focused test passing 10/10 on Windows and Linux; the applicable hosted relinker, ASan, register-context and write-watch checks are green. Productlinuxandwindowsjobs are skipped by policy, so I did not independently verify the platform runtime here.The known #8520 integration is recorded in the PR description and discussed with the author: if POSIX directory-descriptor tracking lands first, the shared cleanup must be moved out of the Windows-only guard and the stream-close test extended. The current head is otherwise clean against
main. I approve this exact head, with that follow-up integration point retained for the eventual combined branch.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T23:33:06Z.
Review: APPROVED
Reviewed the descriptor-state cleanup path, the libc/libkernel callback registration, and the close/reopen locking changes. Cleanup is performed before the host descriptor is closed, while the Windows positioned-transfer lock remains held; the new tests cover descriptor reuse, append and close-on-exec state, random-device reuse, freopen, and close races. Hosted relinker and write-watch suites pass on Linux, Windows, and macOS, along with conventions and intake.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T23:05:12Z.
Review: APPROVED
Re-checked — new head, looks good
The new POSIX directory-descriptor regression is present on head
92f4276f28e9643fe7e336e1cc40ee02f30277da, and the reported cross-platform focused tests cover it.What I ran
Reviewed head
92f4276f28e9643fe7e336e1cc40ee02f30277daagainst PR base3ead2d72517499985692df803ab72ff92ac82ecc; the merge preview against currentmaindc38224141e0b6096007b876b4c42fc9144d11b8is clean. I inspected the eight PR files, including the portable descriptor cleanup and new POSIX test. Local checks ran in Linux; no compiler or build was invoked.Results
maincce419e41f5934a32bb710eaad8a9ad4989b7579Unedited local check output
Findings
Blocking
None found in this re-check. The added POSIX case closes an app archive directory through
fdopen/fclose, verifies descriptor reuse by opening a regular file, and reads that file successfully.Non-blocking
The focused tests are author-reported; I did not build or run them locally. Hosted product Linux and Windows jobs were skipped.
Sources
Not verified
No local build or tests, console behavior, or full
ctestrun was performed. The author states the full test suite was not rerun for this rebase.