[PR #1742] [MERGED] runtime: unlock resource_mutex while compiling unbuilt pipelines #1757

Closed
opened 2026-11-26 21:18:51 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1742
Author: @IlyassRS3
Created: 11/25/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142

Base: main ← Head: fix/pipeline-compile-unlock-resources


📝 Commits (2)

  • 3ab657b runtime: unlock resource_mutex while compiling unbuilt pipelines
  • d33ada4 runtime: fix test identifiers and preserve serial compile short-circuit

📊 Changes

5 files changed (+706 additions, -205 deletions)

View changed files

📝 runtime/CMakeLists.txt (+10 -0)
📝 runtime/src/vulkan_backend.h (+251 -188)
📝 runtime/src/vulkan_replay.h (+78 -17)
➕ runtime/test/replay_pipeline_compile_unlock_test.cpp (+291 -0)
📝 runtime/test/replay_pipeline_compiles_test.cpp (+76 -0)

📄 Description

Item ID: RT-PIPELINE-COMPILE-UNLOCK-RESOURCES
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7160038060
Addresses finding: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157762911 (render thread stalled on VulkanContext::remove_resource -> resource_mutex_ behind 2–5 s compute pipeline compiles at world start, tripping the title's hang watchdog)

Root cause

VulkanContext previously guarded both resources (resources_, buffer_vas_, query_pools_, add_resource() / remove_resource()) and registered pipelines (pipelines_, graphics_pipelines_, pipeline_workers_, device_fault_->pipeline_names) under one std::shared_mutex resource_mutex_.

VulkanQueue::execute_now() (runtime/src/vulkan_backend.h) held std::shared_lock(resource_mutex_) across prepare() and build_pipelines() to keep PipelineState objects alive during compilation. Whenever a queue compiled unbuilt world shaders (2–5 s each in vkCreateComputePipelines), VulkanContext::remove_resource() / add_resource() on the title's render thread blocked on std::unique_lock(resource_mutex_), creating a lock convoy that also stalled other queues and VulkanPresenter until the hang watchdog fired.

Changes

  • runtime/src/vulkan_backend.h:
    • Added VulkanContext::pipeline_mutex_ (std::shared_mutex) to guard pipelines_, graphics_pipelines_, pipeline_workers_, and device_fault_->pipeline_names independently of resource_mutex_, keeping a strict lock hierarchy: state_mutex_ -> pipeline_mutex_ -> resource_mutex_ -> tracker_mutex_ -> submit_mutex_.
    • Updated VulkanQueue::execute_now() to hold pipeline_lock across prepare() / compilation / emit(), always collect unbuilt pipelines via PipelineCompiles (both with pipeline_workers_ and in serial mode), and on pass 0 when !compiles.empty() retire uncommitted pass-0 helper state and release tracker_lock and resource_lock before running compiles.build(...) under pipeline_lock alone. Pass 1 then re-acquires resource_lock and re-prepares against current resources so no stale VkBuffer/VkImage handle can survive a concurrent resource update.
    • Zero extra passes on the warm path (compiles.empty()).
  • runtime/src/vulkan_replay.h:
    • Added PipelineCompiles::lookup() / contains_ticket(), optional prior parameter to PipelineCompiles::build() and build_pipelines(), and shared check_compiled_pipelines() so pass-1 re-preparation reuses prior outcomes (preventing double build() calls or duplicate unresolved_at logs on non-strict failed compute dispatches).
  • runtime/test/replay_pipeline_compiles_test.cpp, runtime/test/replay_pipeline_compile_unlock_test.cpp, runtime/CMakeLists.txt:
    • Extended replay_pipeline_compiles_test with prior_compiles_reuse covering pass-1 reuse of built and non-strict failed compute dispatches.
    • Added CPU mock concurrency test replay_pipeline_compile_unlock_test verifying (both with PipelineWorkers(2) and serial pipeline_workers_ == nullptr) that add_resource(), remove_resource(), and a second VulkanQueue::execute() complete while a slow compile is blocked in build(), that remove_pipeline() safely waits on pipeline_mutex_ until compilation ends, that concurrent resource replacement is picked up on pass 1 (vkCmdCopyBuffer binds the new VkBuffer), and that a non-strict failed compute dispatch invokes build() only once.

Verification

  • AI-assisted: yes
  • GPUs tested: none (developed on macOS without Vulkan::Headers installed)
  • Tested locally:
    • python3 -I scripts/check_no_game_data.py runtime/src/vulkan_replay.h runtime/src/vulkan_backend.h runtime/test/replay_pipeline_compiles_test.cpp runtime/test/replay_pipeline_compile_unlock_test.cpp runtime/CMakeLists.txt: PASS.
    • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
  • Not compiled/executed locally: runtime/src/vulkan_backend.h, runtime/src/vulkan_replay.h, runtime/test/replay_pipeline_compiles_test.cpp, and runtime/test/replay_pipeline_compile_unlock_test.cpp require Vulkan::Headers (not present on this macOS host) and rely on CI (replay_pipeline_compiles, replay_pipeline_compiles_strict, replay_pipeline_compile_unlock, and existing vk_queue_* / vk_presenter_sync tests).

🔄 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/1742 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `fix/pipeline-compile-unlock-resources` --- ### 📝 Commits (2) - [`3ab657b`](https://github.com/ubiquitarianism/leonida/commit/3ab657b058f1c130b4d554d5ecfcd5dc4d86a0cc) runtime: unlock resource_mutex while compiling unbuilt pipelines - [`d33ada4`](https://github.com/ubiquitarianism/leonida/commit/d33ada43446752d22a597f87f0fce8848662fce6) runtime: fix test identifiers and preserve serial compile short-circuit ### 📊 Changes **5 files changed** (+706 additions, -205 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+10 -0) 📝 `runtime/src/vulkan_backend.h` (+251 -188) 📝 `runtime/src/vulkan_replay.h` (+78 -17) ➕ `runtime/test/replay_pipeline_compile_unlock_test.cpp` (+291 -0) 📝 `runtime/test/replay_pipeline_compiles_test.cpp` (+76 -0) </details> ### 📄 Description Item ID: `RT-PIPELINE-COMPILE-UNLOCK-RESOURCES` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7160038060 Addresses finding: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7157762911 (render thread stalled on `VulkanContext::remove_resource` -> `resource_mutex_` behind 2–5 s compute pipeline compiles at world start, tripping the title's hang watchdog) ## Root cause `VulkanContext` previously guarded both resources (`resources_`, `buffer_vas_`, `query_pools_`, `add_resource()` / `remove_resource()`) and registered pipelines (`pipelines_`, `graphics_pipelines_`, `pipeline_workers_`, `device_fault_->pipeline_names`) under one `std::shared_mutex resource_mutex_`. `VulkanQueue::execute_now()` (`runtime/src/vulkan_backend.h`) held `std::shared_lock(resource_mutex_)` across `prepare()` and `build_pipelines()` to keep `PipelineState` objects alive during compilation. Whenever a queue compiled unbuilt world shaders (2–5 s each in `vkCreateComputePipelines`), `VulkanContext::remove_resource()` / `add_resource()` on the title's render thread blocked on `std::unique_lock(resource_mutex_)`, creating a lock convoy that also stalled other queues and `VulkanPresenter` until the hang watchdog fired. ## Changes - **`runtime/src/vulkan_backend.h`**: - Added `VulkanContext::pipeline_mutex_` (`std::shared_mutex`) to guard `pipelines_`, `graphics_pipelines_`, `pipeline_workers_`, and `device_fault_->pipeline_names` independently of `resource_mutex_`, keeping a strict lock hierarchy: `state_mutex_` -> `pipeline_mutex_` -> `resource_mutex_` -> `tracker_mutex_` -> `submit_mutex_`. - Updated `VulkanQueue::execute_now()` to hold `pipeline_lock` across `prepare()` / compilation / `emit()`, always collect unbuilt pipelines via `PipelineCompiles` (both with `pipeline_workers_` and in serial mode), and on pass 0 when `!compiles.empty()` retire uncommitted pass-0 helper state and release `tracker_lock` and `resource_lock` before running `compiles.build(...)` under `pipeline_lock` alone. Pass 1 then re-acquires `resource_lock` and re-prepares against current resources so no stale `VkBuffer`/`VkImage` handle can survive a concurrent resource update. - Zero extra passes on the warm path (`compiles.empty()`). - **`runtime/src/vulkan_replay.h`**: - Added `PipelineCompiles::lookup()` / `contains_ticket()`, optional `prior` parameter to `PipelineCompiles::build()` and `build_pipelines()`, and shared `check_compiled_pipelines()` so pass-1 re-preparation reuses prior outcomes (preventing double `build()` calls or duplicate `unresolved_at` logs on non-strict failed compute dispatches). - **`runtime/test/replay_pipeline_compiles_test.cpp`**, **`runtime/test/replay_pipeline_compile_unlock_test.cpp`**, **`runtime/CMakeLists.txt`**: - Extended `replay_pipeline_compiles_test` with `prior_compiles_reuse` covering pass-1 reuse of built and non-strict failed compute dispatches. - Added CPU mock concurrency test `replay_pipeline_compile_unlock_test` verifying (both with `PipelineWorkers(2)` and serial `pipeline_workers_ == nullptr`) that `add_resource()`, `remove_resource()`, and a second `VulkanQueue::execute()` complete while a slow compile is blocked in `build()`, that `remove_pipeline()` safely waits on `pipeline_mutex_` until compilation ends, that concurrent resource replacement is picked up on pass 1 (`vkCmdCopyBuffer` binds the new `VkBuffer`), and that a non-strict failed compute dispatch invokes `build()` only once. ## Verification - **AI-assisted**: yes - **GPUs tested**: none (developed on macOS without `Vulkan::Headers` installed) - **Tested locally**: - `python3 -I scripts/check_no_game_data.py runtime/src/vulkan_replay.h runtime/src/vulkan_backend.h runtime/test/replay_pipeline_compiles_test.cpp runtime/test/replay_pipeline_compile_unlock_test.cpp runtime/CMakeLists.txt`: **PASS**. - `python3 -I tests/test_text_write_line_endings.py`: **3/3 OK**. - **Not compiled/executed locally**: `runtime/src/vulkan_backend.h`, `runtime/src/vulkan_replay.h`, `runtime/test/replay_pipeline_compiles_test.cpp`, and `runtime/test/replay_pipeline_compile_unlock_test.cpp` require `Vulkan::Headers` (not present on this macOS host) and rely on CI (`replay_pipeline_compiles`, `replay_pipeline_compiles_strict`, `replay_pipeline_compile_unlock`, and existing `vk_queue_*` / `vk_presenter_sync` tests). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 21:18:51 +01:00
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#1757
No description provided.