mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 23:30:53 +01:00
[PR #1932] runtime: use shared_mutex, destroy duplicates unlocked, and harden GraphicsShaderModules (RT-GRAPHICS-SHADER-MODULES-LOCK-AND-GUARDS) #1940
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#1940
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
📋 Pull Request Information
Original PR: https://github.com/ubiquitarianism/leonida/pull/1932
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open
Base:
main← Head:fix/rt-graphics-shader-modules-lock-and-guards📝 Commits (1)
64572cfruntime: use shared_mutex, destroy duplicates unlocked, and harden GraphicsShaderModules (RT-GRAPHICS-SHADER-MODULES-LOCK-AND-GUARDS)📊 Changes
3 files changed (+272 additions, -13 deletions)
View changed files
📝
runtime/CMakeLists.txt(+6 -0)📝
runtime/src/vulkan_graphics_shader_modules.h(+39 -13)➕
runtime/test/vulkan_graphics_shader_modules_test.cpp(+227 -0)📄 Description
Item ID:
RT-GRAPHICS-SHADER-MODULES-LOCK-AND-GUARDS(claim: #1608, comment 7167546476)Problem
In
leonida::GraphicsShaderModules(runtime/src/vulkan_graphics_shader_modules.h, used byDeviceState::graphics_moduleswhen compiling graphics PSOs across parallel pipeline workers):vkDestroyShaderModuleinvoked while holdingmutex_: on a concurrent duplicate creation race,acquire()didstd::lock_guard lock(mutex_); if (auto found = find_locked(hash, words)) return found;. Returningfounddestroyed the localresult(~Module()->vkDestroyShaderModule) beforelockwas released, running driver destruction under the global module cache lock.std::mutexserialized parallelPipelineWorkersthreads even on pure cache hits when multiple graphics PSOs share a vertex or pixel shader.find_locked()matched onlyhashand SPIR-Vwordswithout checkinglive->device == device.createordestroyfunction pointers were not rejected (leading to null dereference on create or in~Module()).create()returnedVK_SUCCESSwithVK_NULL_HANDLE, a null handle was cached and returned.weak_ptrentries in a hot hash bucket were not pruned until every 1024th creation.Fix
runtime/src/vulkan_graphics_shader_modules.h:mutex_tostd::shared_mutex, takingstd::shared_lockon the read fast path andstd::unique_lockonly on miss/insert.std::unique_lockbefore returning an existingReferenceso any losing duplicateModulerunsvkDestroyShaderModulecompletely outside the cache lock.live->device == deviceinfind_locked().words, nullcreate/destroy, andVK_SUCCESSwithVK_NULL_HANDLE; guard~Module()against nulldestroy.Moduleeven if weak-pointer map insertion throwsstd::bad_alloc.runtime/test/vulkan_graphics_shader_modules_test.cpp&runtime/CMakeLists.txt:vulkan_graphics_shader_modulescovering null/error guards, deduplication, cross-device isolation, expiration/re-creation, a concurrency test provingvkDestroyShaderModuleon a losing duplicate runs outside the cache lock, and a 4-thread TSan stress test.Verification
clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=address,undefined runtime/test/vulkan_graphics_shader_modules_test.cpp-> PASSclang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=thread runtime/test/vulkan_graphics_shader_modules_test.cpp-> PASSclang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/vulkan_graphics_shader_modules_test.cpp(x3) -> PASSmain:runtime/src/vulkan_graphics_shader_modules.h-> FAILED as expectedpython3 -I scripts/check_no_game_data.py runtime/src/vulkan_graphics_shader_modules.h runtime/test/vulkan_graphics_shader_modules_test.cpp runtime/CMakeLists.txt-> PASSpython3 -I tests/test_text_write_line_endings.py-> PASS (3/3)🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.