vulkan: keep MoltenVK device-address buffers alive through queued work #721
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!721
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/721/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?
@carbonimax on GitHub · 2026-09-19 20:23 UTC · original
carbonimax:codex/moltenvk-bda-lifetime→main· +475 −147 · 6 files · 3 commits · closed without mergingMoltenVK can lose the device with Metal
Invalid Resource(code 9) when Kyty retires a device-address buffer after its last actual use. MoltenVK declares all live addressable buffers when encoding BDA shaders, so later queued command buffers can still reference the retired Metal resource even when their shaders never access that buffer.On MoltenVK only, wait for the latest submitted scheduler tick in the deferred buffer-deletion callback before destroying the cached buffer. Other drivers retain the existing retirement behavior. This is a conservative driver workaround: it can drain queued work during deletion batches and reduce CPU/GPU overlap.
Scope
main(f100f78); adapted from local commit6b31300.Validation
On the isolated PR sources: compiler syntax checks for the modified translation units and successful compilation of the standalone reproducer. The safe GPU mode was rerun on Apple M4 Pro with the local MoltenVK 1.4.3 fork.
Previously reviewed evidence from the live checkout:
VK_ERROR_DEVICE_LOSTand Metal code 9.Game trials stopped at the first PerVertex draw, beyond the two known early crash sites. These are bounded stability checks on a checkout with other WIP, not visual or gameplay validation of this isolated PR. The black screen remains unresolved. The observed queue-drain cost is a known tradeoff; retaining implicitly referenced resources in MoltenVK would be preferable.
Reproducer
The harness loads MoltenVK with
dlopenand is not part of default CI. In hazard mode, exit 0 requires exactly device loss after successful completion of the buffer's sole user; a failed initial wait is inconclusive, not a successful reproduction.Completion boundary
This workaround waits for Vulkan timeline visibility. It addresses MoltenVK keeping implicit references to otherwise unused live device-address buffers in later queued work; it does not prove Metal command-buffer completion. A separately reproduced Metal API validation window between timeline visibility and command-buffer completion remains outside this PR. No submission-fence protocol change is included here.
Main update validation (2026-10-01)
Updated against main
44798085. The GPU reproducer is Apple-only and checks portability enumeration and device-enumeration failures. Local build and host checks are recorded separately from the historical GPU evidence; no new GPU run was performed during this update.@coderabbitai[bot] on GitHub · 2026-09-19 20:32 UTC · original · edited 2026-10-13 22:44 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
a438dcde-8ba6-43c7-8aca-a88c0fc5087d📥 Commits
Reviewing files that changed from the base of the PR and between ae2a9df10e3d46c2be95c1eae8d1b5f960535bc2 and cc9ffcd1f6570b2dc52bb40f7f0ef94f6fb9df47.
📒 Files selected for processing (3)
src/graphics/host_gpu/graphicContext.hsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/presentation/window/vulkanWindow.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; 5 remain after this review.
📝 Walkthrough
Walkthrough
The change detects MoltenVK and records whether device-address buffer destruction must wait for queued work. When that policy is enabled, deferred buffer deletion waits through the preceding scheduler tick. An Apple-only, excluded GPU test target exercises normal and expected-hazard outcomes.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: 🟡 Moderate · up to
cc9ffMoltenVK can still release a buffer before Metal has finished using it, leaving a reported validation window that can disrupt GPU work. Resolve this lifetime gap before merging.
Security Architecture Review
Security architecture risk: 🔵 Low · up to
0dcfbThe workaround conservatively extends GPU buffer lifetimes on MoltenVK. No new guest-facing capability or privilege expansion was identified in the inspected paths. Remaining uncertainty concerns concurrent access and failure cleanup, rather than a verified security vulnerability.
Retained concerns
No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
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.@coderabbitai[bot] on GitHub · 2026-09-19 20:32 UTC · original · edited 2026-10-01 17:29 UTC
On
CMakeLists.txtline 384 (outdated):🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 20535
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 23147
Restrict the target to Apple platforms.
The project supports Windows, but this target is unguarded. Its source includes POSIX-only
<dlfcn.h>and callsdlopenanddlsym. WhenSDL_VULKAN_LIBRARYis unset, it also attempts to loadlibMoltenVK.dylib. An explicit Windows build can therefore fail. No portable loader is used by this target.Proposed CMake change
📝 Committable suggestion
✅ Confirmed as addressed by @carbonimax
@ -0,0 +194,4 @@Check(vkEnumeratePhysicalDevices(instance, &n, devices.data()), "vkEnumeratePhysicalDevices");if (n == 0 || devices[0] == VK_NULL_HANDLE) { std::printf("No Vulkan physical device; inconclusive\n"); return 2; }VkPhysicalDevice phys = devices[0];vkGetPhysicalDeviceMemoryProperties(phys, &g_mp);@coderabbitai[bot] on GitHub · 2026-09-19 20:32 UTC · original · edited 2026-10-01 17:32 UTC
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 19201
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 31878
Enable portability enumeration and validate the physical-device result.
On supported MoltenVK runs, enable
VK_KHR_portability_enumerationandVK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR, using the same extension-availability condition as production. Without them, some MoltenVK versions can report zero physical devices. The current code ignores the enumeration result and can pass an unwrittenphystovkGetPhysicalDeviceMemoryProperties. Initializephys, check the result, and reject zero devices before using the handle.✅ Confirmed as addressed by @carbonimax
@ -0,0 +281,4 @@return early ? 2 : 1;}vkResetCommandPool(g_dev, pool, 0);}@coderabbitai[bot] on GitHub · 2026-09-19 20:32 UTC · original · edited 2026-10-01 17:31 UTC
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 10832
Make every hazard-mode submission use
x.address. The current source selection makes onlycbs[0]accessx. Hazard mode waits forfirst, which is the signal value forcbs[0], before destroyingx. The remaining submissions usesrc.address, so no submitted command buffer can accessxafter destruction. Usex.addressfor every recorded command buffer and remove the now-unusedsrcallocation.Suggested fix
📝 Committable suggestion
@coderabbitai[bot] on GitHub · 2026-09-19 20:32 UTC · original · reviewed
Actionable comments posted: 3
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
aed55a9a-95ce-471a-b559-5e3f5ece0142📥 Commits
Reviewing files that changed from the base of the PR and between
f100f785daand b7399078130b602e160efd9760b7685112876f35.📒 Files selected for processing (6)
CMakeLists.txtsrc/graphics/host_gpu/graphicContext.hsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/vulkanCommon.hsrc/graphics/presentation/window/vulkanWindow.cpptests/DeviceAddressLifetimeGpuTests.cppIncluded review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@carbonimax on GitHub · 2026-10-01 17:16 UTC · original
On
CMakeLists.txtline 384 (outdated):Fixed in the updated branch: device_address_lifetime_gpu_tests is now created only inside if(APPLE). The production buffer-retirement policy remains selected by the Vulkan driver ID.
@ -0,0 +281,4 @@return early ? 2 : 1;}vkResetCommandPool(g_dev, pool, 0);}@carbonimax on GitHub · 2026-10-01 17:17 UTC · original
The later submissions intentionally use src.address rather than x.address. The only explicit shader use of x is submission 0, whose timeline value is waited before destruction. The regression reproduces MoltenVK declaring an otherwise unused live BDA buffer in later command buffers, so those commands must not explicitly use x: doing that would instead create an application use-after-free and invalidate the driver-hazard reproducer. Each later command has a distinct destination region to avoid an unrelated write race. The test also retains the default queue-retirement variant. This PR waits for timeline visibility and does not claim to close the separate Metal API validation window before command-buffer completion.
@ -0,0 +194,4 @@Check(vkEnumeratePhysicalDevices(instance, &n, devices.data()), "vkEnumeratePhysicalDevices");if (n == 0 || devices[0] == VK_NULL_HANDLE) { std::printf("No Vulkan physical device; inconclusive\n"); return 2; }VkPhysicalDevice phys = devices[0];vkGetPhysicalDeviceMemoryProperties(phys, &g_mp);@carbonimax on GitHub · 2026-10-01 17:17 UTC · original
Fixed in the updated branch: the test enumerates instance extensions and enables VK_KHR_portability_enumeration plus its instance flag only when available. Both physical-device enumeration calls are checked; the handles are initialized to VK_NULL_HANDLE, and an empty device list exits as inconclusive rather than reading an uninitialized handle.
@coderabbitai[bot] on GitHub · 2026-10-01 17:29 UTC · original
On
CMakeLists.txtline 384 (outdated):@carbonimax, thanks for the fix and clarification. The suppliedCMakeLists.txtplacesdevice_address_lifetime_gpu_testsinsideif(APPLE), which addresses this finding. The production buffer-retirement policy remains selected by the Vulkan driver ID.✏️ Learnings added
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@ -0,0 +281,4 @@return early ? 2 : 1;}vkResetCommandPool(g_dev, pool, 0);}@coderabbitai[bot] on GitHub · 2026-10-01 17:31 UTC · original
@carbonimax, thanks for the clarification. My suggested change was incorrect, and I withdraw the finding.Only submission 0 should explicitly read
x.address. Waiting for its timeline value before destroyingxisolates the MoltenVK hazard: later command buffers declare the live BDA buffer without explicitly accessing it. Making every submission readx.addresswould introduce an application use-after-free and invalidate the reproducer.The distinct destination regions avoid an unrelated write race. The default mode retains the queue-retirement variant. Neither mode establishes that timeline visibility closes the separate Metal API validation window before command-buffer completion.
✏️ Learnings added
✅ Review thread resolved.
You are interacting with an AI system.
@ -0,0 +194,4 @@Check(vkEnumeratePhysicalDevices(instance, &n, devices.data()), "vkEnumeratePhysicalDevices");if (n == 0 || devices[0] == VK_NULL_HANDLE) { std::printf("No Vulkan physical device; inconclusive\n"); return 2; }VkPhysicalDevice phys = devices[0];vkGetPhysicalDeviceMemoryProperties(phys, &g_mp);@coderabbitai[bot] on GitHub · 2026-10-01 17:32 UTC · original
🧩 Analysis chain
🏁 Script executed:
Length of output: 5733
@carbonimax, verified in the updated code. The test enables the portability extension and instance flag only when available. Both physical-device enumeration calls are checked. Device handles start asVK_NULL_HANDLE, and empty or null results exit with code 2 before use. This addresses the finding.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@ -155,0 +158,4 @@m_scheduler.GetMasterSemaphore().Wait(m_scheduler.CurrentTick() - 1);}m_slot_buffers.erase(id);});@coderabbitai[bot] on GitHub · 2026-10-09 09:42 UTC · original
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1748
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 24495
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 24302
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1234
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 20447
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11229
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 12282
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5029
Wait for Metal command-buffer completion before erasing the buffer.
On MoltenVK, the callback waits only for the Vulkan timeline value. That value can become visible while the submitted Metal command buffer still references the buffer.
m_slot_buffers.erase(id)then invokesBuffer::~Buffer, which callsvmaDestroyBuffer. MoltenVK can abort the queued command buffer withInvalid Resource.Use a completion-level fence or queue-completion wait before destroying the buffer. Do not use timeline visibility as the destruction guarantee.
@coderabbitai[bot] on GitHub · 2026-10-09 09:42 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
25b7b3f2-14aa-4b21-aa8f-1cc219df4c23📥 Commits
Reviewing files that changed from the base of the PR and between 0dcfb500a313fca904578b57ae945990c126eb74 and ae2a9df10e3d46c2be95c1eae8d1b5f960535bc2.
📒 Files selected for processing (5)
CMakeLists.txtsrc/graphics/host_gpu/graphicContext.hsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/vulkanCommon.hsrc/graphics/presentation/window/vulkanWindow.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; 6 remain after this review.
Pull request closed