runtime: use shared_mutex, destroy duplicates unlocked, and harden GraphicsShaderModules (RT-GRAPHICS-SHADER-MODULES-LOCK-AND-GUARDS) #1932

Open
github-bot wants to merge 1 commit from github-bot/gh-1932 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 16:06 UTC · original
IlyassRS3:fix/rt-graphics-shader-modules-lock-and-guards → main · +272 −13 · 3 files · 1 commits · open

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 by DeviceState::graphics_modules when compiling graphics PSOs across parallel pipeline workers):

  1. vkDestroyShaderModule invoked while holding mutex_: on a concurrent duplicate creation race, acquire() did std::lock_guard lock(mutex_); if (auto found = find_locked(hash, words)) return found;. Returning found destroyed the local result (~Module() -> vkDestroyShaderModule) before lock was released, running driver destruction under the global module cache lock.
  2. Exclusive lock on read-only cache hits: std::mutex serialized parallel PipelineWorkers threads even on pure cache hits when multiple graphics PSOs share a vertex or pixel shader.
  3. Cross-device handle aliasing: find_locked() matched only hash and SPIR-V words without checking live->device == device.
  4. Missing guards and dead bucket buildup:
    • Null create or destroy function pointers were not rejected (leading to null dereference on create or in ~Module()).
    • If create() returned VK_SUCCESS with VK_NULL_HANDLE, a null handle was cached and returned.
    • Expired weak_ptr entries in a hot hash bucket were not pruned until every 1024th creation.

Fix

  • runtime/src/vulkan_graphics_shader_modules.h:
    • Switch mutex_ to std::shared_mutex, taking std::shared_lock on the read fast path and std::unique_lock only on miss/insert.
    • Scope the std::unique_lock before returning an existing Reference so any losing duplicate Module runs vkDestroyShaderModule completely outside the cache lock.
    • Check live->device == device in find_locked().
    • Reject empty words, null create/destroy, and VK_SUCCESS with VK_NULL_HANDLE; guard ~Module() against null destroy.
    • Prune expired entries in the target hash bucket on insert (in addition to periodic full sweeps), and keep a newly created valid Module even if weak-pointer map insertion throws std::bad_alloc.
  • runtime/test/vulkan_graphics_shader_modules_test.cpp & runtime/CMakeLists.txt:
    • Add portable CPU-only CTest vulkan_graphics_shader_modules covering null/error guards, deduplication, cross-device isolation, expiration/re-creation, a concurrency test proving vkDestroyShaderModule on a losing duplicate runs outside the cache lock, and a 4-thread TSan stress test.

Verification

  • AI-assisted: yes
  • GPUs tested: none (macOS host, CPU-only unit + concurrency tests against Khronos Vulkan-Headers)
  • Game files used: none
  • Commands run:
    • clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=address,undefined runtime/test/vulkan_graphics_shader_modules_test.cpp -> PASS
    • clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=thread runtime/test/vulkan_graphics_shader_modules_test.cpp -> PASS
    • clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/vulkan_graphics_shader_modules_test.cpp (x3) -> PASS
    • Negative mutation check running the new test against unpatched main:runtime/src/vulkan_graphics_shader_modules.h -> FAILED as expected
    • python3 -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 -> PASS
    • python3 -I tests/test_text_write_line_endings.py -> PASS (3/3)
**@⁠IlyassRS3** on GitHub · 2026-11-25 16:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1932) `IlyassRS3:fix/rt-graphics-shader-modules-lock-and-guards` → `main` · +272 −13 · 3 files · 1 commits · open 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 by `DeviceState::graphics_modules` when compiling graphics PSOs across parallel pipeline workers): 1. **`vkDestroyShaderModule` invoked while holding `mutex_`**: on a concurrent duplicate creation race, `acquire()` did `std::lock_guard lock(mutex_); if (auto found = find_locked(hash, words)) return found;`. Returning `found` destroyed the local `result` (`~Module()` -> `vkDestroyShaderModule`) before `lock` was released, running driver destruction under the global module cache lock. 2. **Exclusive lock on read-only cache hits**: `std::mutex` serialized parallel `PipelineWorkers` threads even on pure cache hits when multiple graphics PSOs share a vertex or pixel shader. 3. **Cross-device handle aliasing**: `find_locked()` matched only `hash` and SPIR-V `words` without checking `live->device == device`. 4. **Missing guards and dead bucket buildup**: - Null `create` or `destroy` function pointers were not rejected (leading to null dereference on create or in `~Module()`). - If `create()` returned `VK_SUCCESS` with `VK_NULL_HANDLE`, a null handle was cached and returned. - Expired `weak_ptr` entries in a hot hash bucket were not pruned until every 1024th creation. ## Fix - **`runtime/src/vulkan_graphics_shader_modules.h`**: - Switch `mutex_` to `std::shared_mutex`, taking `std::shared_lock` on the read fast path and `std::unique_lock` only on miss/insert. - Scope the `std::unique_lock` before returning an existing `Reference` so any losing duplicate `Module` runs `vkDestroyShaderModule` completely outside the cache lock. - Check `live->device == device` in `find_locked()`. - Reject empty `words`, null `create`/`destroy`, and `VK_SUCCESS` with `VK_NULL_HANDLE`; guard `~Module()` against null `destroy`. - Prune expired entries in the target hash bucket on insert (in addition to periodic full sweeps), and keep a newly created valid `Module` even if weak-pointer map insertion throws `std::bad_alloc`. - **`runtime/test/vulkan_graphics_shader_modules_test.cpp`** & **`runtime/CMakeLists.txt`**: - Add portable CPU-only CTest `vulkan_graphics_shader_modules` covering null/error guards, deduplication, cross-device isolation, expiration/re-creation, a concurrency test proving `vkDestroyShaderModule` on a losing duplicate runs outside the cache lock, and a 4-thread TSan stress test. ## Verification - **AI-assisted**: yes - **GPUs tested**: none (macOS host, CPU-only unit + concurrency tests against Khronos Vulkan-Headers) - **Game files used**: none - Commands run: - `clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=address,undefined runtime/test/vulkan_graphics_shader_modules_test.cpp` -> PASS - `clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=thread runtime/test/vulkan_graphics_shader_modules_test.cpp` -> PASS - `clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/vulkan_graphics_shader_modules_test.cpp` (x3) -> PASS - Negative mutation check running the new test against unpatched `main:runtime/src/vulkan_graphics_shader_modules.h` -> FAILED as expected - `python3 -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` -> PASS - `python3 -I tests/test_text_write_line_endings.py` -> PASS (3/3)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:11:42 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 16:21 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 16:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1932#pullrequestreview-6061051203) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1932/head:github-bot/gh-1932'
git switch 'github-bot/gh-1932'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1932'
git switch 'github-bot/gh-1932'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1932'
git switch 'github-bot/gh-1932'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1932'
git switch 'main'
git merge --squash 'github-bot/gh-1932'
git switch 'main'
git merge --ff-only 'github-bot/gh-1932'
git switch 'main'
git merge 'github-bot/gh-1932'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.