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

Open
opened 2026-11-26 21:19:28 +01:00 by 4ld0 · 0 comments
Owner

📋 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)

  • 64572cf runtime: 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 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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1932 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-graphics-shader-modules-lock-and-guards` --- ### 📝 Commits (1) - [`64572cf`](https://github.com/ubiquitarianism/leonida/commit/64572cfd9e684a9a0807f5bac5ddbf105c890419) runtime: use shared_mutex, destroy duplicates unlocked, and harden GraphicsShaderModules (RT-GRAPHICS-SHADER-MODULES-LOCK-AND-GUARDS) ### 📊 Changes **3 files changed** (+272 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+6 -0) 📝 `runtime/src/vulkan_graphics_shader_modules.h` (+39 -13) ➕ `runtime/test/vulkan_graphics_shader_modules_test.cpp` (+227 -0) </details> ### 📄 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 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1940
No description provided.