Vulkan external fd handling when OS revokes file descriptors
0 reputation · 06 May 2022, 05:09 UTC
0 reputation · 06 May 2022, 05:09 UTC
Vulkan allows importing external memory and semaphores via OS file descriptors through VK_EXT_external_memory_fd and VK_EXT_external_semaphore_fd. The specification states that behavior is implementation-defined when an imported handle becomes invalid after the application closes it or the OS revokes it.
Applications must maintain valid descriptors until Vulkan no longer references them, but no Vulkan query or callback exists to detect revocation. Drivers may return VK_ERROR_INVALID_EXTERNAL_HANDLE, produce undefined behavior, or silently corrupt submissions. This creates a gap between least-privilege fd management (closing descriptors early) and safe Vulkan usage.
Determine whether a portable pattern exists for detecting or preventing use-after-revoke without relying on vendor-specific behavior.
Does the validation layer surface any diagnostic when a submitted command buffer references a stale fd? Can an application safely duplicate the descriptor with FD_CLOEXEC and close the original without triggering undefined behavior across all conformant drivers? Is there a recommended synchronization point where the driver guarantees it has consumed the fd so the application may close it?
33775 reputation · 06 May 2022, 16:02 UTC
Validation layer diagnostics: No. Validation layers validate Vulkan API usage, not kernel object lifetimes. Once an external fd has been imported into a VkDeviceMemory or VkSemaphore, the layer cannot track whether the underlying descriptor is later closed or revoked, so it will not warn when a submitted command buffer references a stale handle.
Duplicating with FD_CLOEXEC and closing the original: No. The specification does not require the implementation to duplicate the fd at import time. A conformant driver may retain the original fd number for later waits, mapping, or submission operations. Closing the original after duplication is therefore not portable and may cause undefined behavior, VK_ERROR_INVALID_EXTERNAL_HANDLE, or silent corruption.
Guaranteed synchronization point: No. Queue submission, fence signaling, and semaphore waits do not provide a portable guarantee that the driver has finished consuming the descriptor. There is no Vulkan query or callback that reports fd consumption.
The Vulkan specification explicitly leaves behavior implementation-defined when an imported external handle becomes invalid after the application closes it or the OS revokes it. From the driver's perspective, an application close and an OS-level revocation (for example, FD_CLOEXEC on exec or container teardown) are indistinguishable. Because drivers may defer descriptor use until submission or mapping, the API cannot expose a safe early-close point without breaking that flexibility.
The only portable rule is to retain the original fd for the entire lifetime of the imported Vulkan object:
vkFreeMemory or vkDestroySemaphore call completes.Because the specification does not mandate a duplication strategy, you can verify your driver's behavior experimentally:
strace or driver debug logs to observe whether the implementation issues dup, ioctl, or close on the imported fd during import and subsequent queue operations.These checks are diagnostic only; they do not create a portable guarantee across other conformant drivers.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.