kernel: fix file/semaphore handle lifetime races #1328
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1328
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1328/head"
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?
@HamsterCL on GitHub · 2026-10-09 20:44 UTC · original
HamsterCL:fix/kernel-handle-lifetime→main· +351 −44 · 5 files · 4 commits · closed without mergingSplit out of #1313 (closed) as a smaller, reviewable change.
Fileobjects so a concurrentKernelClosecannot free a file another thread is reading/writing; close the host handle under the per-file mutex; keep the registry and mount points alive on shutdown while calls are in flight.KernelDeleteSemadefers destruction until in-flight Wait/Signal/Poll/Cancel return, and wake blocked waiters on deletion.Known gap: an operation that passed the
openedcheck but has not yet taken the file mutex can still see a closed handle.Built and ran all 53 ctest tests locally (Windows, clang-cl).
@HamsterCL on GitHub · 2026-10-09 20:45 UTC · original
@coderabbitai review
@coderabbitai[bot] on GitHub · 2026-10-09 20:53 UTC · original · edited 2026-10-10 14:58 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
9cc0ee25-66a3-4c11-93b7-33ee6ab8f134📥 Commits
Reviewing files that changed from the base of the PR and between
4bab4a5946andb2a8ee518f.📒 Files selected for processing (1)
src/kernel/fileSystem.cpp🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
📝 Walkthrough
📝 Walkthrough
Walkthrough
Linux and Windows memory-backed file seeks now clamp positions to the buffer size. File descriptor lookups pin files, and deletion and shutdown defer cleanup while references remain. Kernel file operations release pins and return
EBADFwhen a file is no longer open. Semaphore calls pin registered handles; deletion marks the semaphore, wakes waiters, and defers freeing until active calls release their pins.KernelSetPrtAperturerejects nonzero addresses abovePRT_APERTURE_END.Priority: ➖ Normal
Change: Bug fix
Merge Risk: 🟡 Moderate · up to
b2a8eConcurrent file close and shutdown can terminate the process. Resolve that race before merging; shutdown can also still outlive calls that have not yet pinned a file.
Pre-merge checks |
4 |
1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@ -579,1 +688,4 @@}file->opened = false;}@coderabbitai[bot] on GitHub · 2026-10-09 20:53 UTC · original · edited 2026-10-09 21:00 UTC
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3391
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 22476
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5658
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1223
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1389
Return
EBADFfor concurrent closes.Both calls can pin the same
Fileand pass the unlockedopenedcheck. After one call setsopenedtofalse, the other call can triggerEXIT_IF(!file->opened). If both calls pass that check before the first close completes, the second call can reachDeleteDescriptorafterpending_deleteis set and trigger itsEXIT_IF.In non-final builds, either assertion calls
DbgExit, which invokes_Exit(321). Final builds disable these assertions, but the losing close still does not returnEBADF.Return
KERNEL_ERROR_EBADFwhen the mutex-protected check finds that another call already closed the file. CallDeleteDescriptoronly from the call that changesopenedfromtruetofalse.Suggested fix
✅ Addressed in commit 3b2ca4a
@coderabbitai[bot] on GitHub · 2026-10-09 20:53 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
47baac6f-5a05-4bfa-993c-26a9814d2f19📥 Commits
Reviewing files that changed from the base of the PR and between
59183dbfa8and 42e3b957e1c17e73516a04efca2b00cb0765d572.📒 Files selected for processing (5)
src/common/platform/sysLinuxFileIO.cppsrc/common/platform/sysWindowsFileIO.cppsrc/kernel/fileSystem.cppsrc/kernel/memory.cppsrc/kernel/semaphore.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@coderabbitai[bot] on GitHub · 2026-10-09 20:56 UTC · original
⚠️ Action not completed
Already reviewed the last commit. Use
@coderabbitai full reviewto rerun a review of the entire changeset.@HamsterCL on GitHub · 2026-10-09 20:58 UTC · original
@coderabbitai review
@coderabbitai[bot] on GitHub · 2026-10-09 20:58 UTC · original
⚠️ Action not completed
Already reviewed the last commit. Use
@coderabbitai full reviewto rerun a review of the entire changeset.@HamsterCL on GitHub · 2026-10-09 21:00 UTC · original
@coderabbitai full review
@HamsterCL on GitHub · 2026-10-09 21:05 UTC · original
@coderabbitai review
@coderabbitai[bot] on GitHub · 2026-10-09 21:12 UTC · original · edited 2026-10-09 21:14 UTC
✅ Action performed
Review finished.
@ -633,1 +747,3 @@EXIT_IF(!file->opened);// A concurrent KernelClose() may already have closed this descriptor.if (!file->opened) {return KERNEL_ERROR_EBADF;@coderabbitai[bot] on GitHub · 2026-10-09 21:14 UTC · original · edited 2026-10-10 13:04 UTC
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Check
openedunder the file mutex before using the host handle.KernelClose()can close the host handle after this check but beforeKernelRead()acquiresfile->mutex.KernelRead()then callsTell(),Size(), andRead()on the closed handle and can returnEIOinstead ofEBADF. The same check-before-lock pattern affectsKernelWrite(),KernelPread(),KernelPwrite(),KernelLseek(), andKernelFstat(); the pinned vectored I/O and truncate paths also use the handle after an unlocked check. Checkopenedwhile holding the mutex, before each host-handle operation.✅ Addressed in commit
b2a8ee5@coderabbitai[bot] on GitHub · 2026-10-09 21:14 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
cbd9cf0a-d0bd-4db8-b0f3-13dcfb7ea6ea📥 Commits
Reviewing files that changed from the base of the PR and between 42e3b957e1c17e73516a04efca2b00cb0765d572 and 7c6534c33136a5a55ad0f740d273f80fe95809bb.
📒 Files selected for processing (1)
src/kernel/fileSystem.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 0 remain after this review.
@coderabbitai[bot] on GitHub · 2026-10-09 21:16 UTC · original
⚠️ Action not completed
Review rate limited.
Your included review limit is currently reached under our Fair Usage Limits Policy. This review may still proceed through usage-based billing if eligible. Your next included review will be available in 27 minutes.
@theantipopau on GitHub · 2026-10-10 01:46 UTC · original
Evidence from a Windows fork build (AMD Ryzen, clang-cl,
42e3b957+3b2ca4ac+7c6534c3cherry-picked onto current main): all three commits apply with only mechanical conflicts infileSystem.cpp(the fork carriesstd::shared_ptr<File>descriptor handles plus test bodies from the #1140-adjacent work; the shared_ptr registry already provides much of commit 1's File lifetime fix there, and commits 2-3 applied as intended).kernel_file_system,controller_settings,memory_tracker,page_manager,archive_file,windows_file_readall green (6/6) on Windows.CheckConcurrentCloseDuringReadcase and passes on this build.Lighting up
kernel_file_systemin the CI ctest subsets (.github/workflows/build.yml) would keep this race covered on every PR — happy to see that land.@HamsterCL on GitHub · 2026-10-10 02:28 UTC · original
Thanks for cherry-picking this and for the detailed evidence: the passing build, the six ctest suites, your concurrent close-during-read test and the 120-180s smoke runs on three games are all useful. Understood on the shared_ptr overlap. If your fork already holds descriptors that way, part of the first commit is likely redundant there. This PR mainly targets main, which still uses raw File* with a ref_count. On CI: a kernel_file_system ctest target already exists on main (kernel_file_system_tests), but it is not in the -R subsets in build.yml for Windows, macOS or Linux. Adding it would be a reasonable separate change, so I will keep it out of this PR.
@HamsterCL on GitHub · 2026-10-10 02:30 UTC · original
Rebased onto current main. @coderabbitai review
@coderabbitai[bot] on GitHub · 2026-10-10 02:30 UTC · original
@HamsterCLI’ll request a review of the rebased changes.⚠️ Action not completed
Already reviewed the last commit. Use
@coderabbitai full reviewto rerun a review of the entire changeset.@HamsterCL on GitHub · 2026-10-10 14:46 UTC · original
@theantipopau thanks for testing the series on a Windows fork build and for the CI suggestion.
I opened #1371 to run
kernel_file_systemin the ctest subsets. To make that possible I had to change the test itself, since it aborted on hosted runners without a Vulkan device:Memory::InvalidateMemoryreturns when no GPU resources are installed;EINVALfor unknown socket options, macOS reports the flag value forSO_BROADCAST).CI is green on Linux, Windows and macOS with
kernel_file_systemincluded. I also pushed a follow-up to this PR that re-checksopenedunder the file mutex in the read/write/seek/stat/truncate paths, per CodeRabbit.@HamsterCL on GitHub · 2026-10-10 14:58 UTC · original
@coderabbitai review
@coderabbitai[bot] on GitHub · 2026-10-10 14:58 UTC · original
⚠️ Action not completed
Already reviewed the last commit. Use
@coderabbitai full reviewto rerun a review of the entire changeset.Pull request closed