[PR #2361] d3d12: one compile job per pipeline at a time, so a released PSO is not deleted under its other build (RT-COMPILE-OWNER-RACE) #2364

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2361
Author: @qhukz13
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: cont/compile-owner-race


📝 Commits (1)

  • 76c69bd d3d12: one compile job per pipeline at a time, so a released PSO is not deleted under its other build

📊 Changes

3 files changed (+367 additions, -4 deletions)

View changed files

📝 runtime/CMakeLists.txt (+9 -0)
📝 runtime/shims/d3d12_pipeline_compile.h (+11 -4)
➕ runtime/test/d3d12_compile_owner_test.cpp (+347 -0)

📄 Description

Claimed in the coordination issue as: RT-COMPILE-OWNER-RACE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7196188910)

Conflicts with: #2233. It changes the same next_locked; whichever lands second keeps the owner check in its single-worker branch.

What and why

PipelineCompiler (runtime/shims/d3d12_pipeline_compile.h) runs a general worker and, by default, a graphics-only worker. next_locked gives each worker a job without looking at Job::owner:

  • the general worker takes jobs_.begin(), graphics jobs included;
  • the graphics worker takes the first graphics job.

So two jobs of one PSO, for example two variants of one graphics pipeline, could be built at the same time, one on each worker.

If the title released that PSO meanwhile, PipelineState::last_release called after_running(owner, [this] { delete this; ... }). after_running hands last to the first running job of that owner and returns. That worker deleted the PSO when its own build returned, while the other worker was still in a build of the same object, or waiting on its variants_mutex_. That is a use-after-free.

The change

  • next_locked skips a queued job whose owner has a running entry, for both workers. A running entry still counts after its job has finished, while it runs what comes after the job (the PSO's deletion). So one job per PSO runs at a time.
  • A worker that finishes a job now also wakes the others (wake_.notify_all()), because a job held back by the first point can run now.
  • With one PSO's builds serialized, the PSO's other queued job is still queued when last_release runs, and after_running drops it. Its deletion then runs after the only build of it.

This is the rule #2233's pool already uses (next_locked there skips a job whose owner has a running job). This PR applies it to the current two-worker scheme on main.

Tested

Windows 10 Pro 19045, MSVC 14.44 RelWithDebInfo, on main 73516a290. No GPU needed, no game files.

  • New d3d12_compile_owner_test: CPU only, the real PipelineCompiler, four cases, every wait bounded.

    1. Two graphics jobs of one owner. The second does not start while the first is held. after_running runs once, after it, with no job of that owner running. The queued second job is dropped.
    2. Graphics jobs of two owners still run at the same time on the two workers.
    3. Two compute jobs of one owner run one after the other.
    4. A compute job held back behind its owner's graphics build on the graphics worker starts once that build returns.
  • Passes, 4 repeats, and with LEONIDA_VK_VALIDATION=1. d3d12_compile_teardown_test passes beside it.

  • It fails without the change:

    change undone result
    main's next_locked case 1 fails: the second job started at once on the other worker
    the new wake_.notify_all() case 4 times out
  • tests/test_ctest_registration.py and scripts/check_no_game_data.py pass on the changed files, and git diff --check is clean.

GPUs tested: none needed (CPU-only test); host GTX 1050 Ti, Windows 10.

AI-assisted: yes. Antigravity (Gemini 4 Neon) wrote the first version of the change and the test; Claude Code reviewed it, tightened the owner check and ran the tests and mutants.

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code


🔄 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/2361 **Author:** [@qhukz13](https://github.com/qhukz13) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `cont/compile-owner-race` --- ### 📝 Commits (1) - [`76c69bd`](https://github.com/ubiquitarianism/leonida/commit/76c69bd718fef3e723c462a1d4762bb7642b23cc) d3d12: one compile job per pipeline at a time, so a released PSO is not deleted under its other build ### 📊 Changes **3 files changed** (+367 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+9 -0) 📝 `runtime/shims/d3d12_pipeline_compile.h` (+11 -4) ➕ `runtime/test/d3d12_compile_owner_test.cpp` (+347 -0) </details> ### 📄 Description Claimed in the coordination issue as: RT-COMPILE-OWNER-RACE (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7196188910) Conflicts with: #2233. It changes the same `next_locked`; whichever lands second keeps the owner check in its single-worker branch. ### What and why `PipelineCompiler` (`runtime/shims/d3d12_pipeline_compile.h`) runs a general worker and, by default, a graphics-only worker. `next_locked` gives each worker a job without looking at `Job::owner`: - the general worker takes `jobs_.begin()`, graphics jobs included; - the graphics worker takes the first graphics job. So two jobs of one PSO, for example two variants of one graphics pipeline, could be built at the same time, one on each worker. If the title released that PSO meanwhile, `PipelineState::last_release` called `after_running(owner, [this] { delete this; ... })`. `after_running` hands `last` to the **first** running job of that owner and returns. That worker deleted the PSO when its own build returned, while the other worker was still in a build of the same object, or waiting on its `variants_mutex_`. That is a use-after-free. ### The change - `next_locked` skips a queued job whose owner has a running entry, for both workers. A running entry still counts after its job has finished, while it runs what comes after the job (the PSO's deletion). So one job per PSO runs at a time. - A worker that finishes a job now also wakes the others (`wake_.notify_all()`), because a job held back by the first point can run now. - With one PSO's builds serialized, the PSO's other queued job is still queued when `last_release` runs, and `after_running` drops it. Its deletion then runs after the only build of it. This is the rule #2233's pool already uses (`next_locked` there skips a job whose owner has a running job). This PR applies it to the current two-worker scheme on main. ### Tested Windows 10 Pro 19045, MSVC 14.44 RelWithDebInfo, on main `73516a290`. No GPU needed, no game files. - New `d3d12_compile_owner_test`: CPU only, the real `PipelineCompiler`, four cases, every wait bounded. 1. Two graphics jobs of one owner. The second does not start while the first is held. `after_running` runs once, after it, with no job of that owner running. The queued second job is dropped. 2. Graphics jobs of two owners still run at the same time on the two workers. 3. Two compute jobs of one owner run one after the other. 4. A compute job held back behind its owner's graphics build on the graphics worker starts once that build returns. - Passes, 4 repeats, and with `LEONIDA_VK_VALIDATION=1`. `d3d12_compile_teardown_test` passes beside it. - It fails without the change: | change undone | result | |---|---| | main's `next_locked` | case 1 fails: the second job started at once on the other worker | | the new `wake_.notify_all()` | case 4 times out | - `tests/test_ctest_registration.py` and `scripts/check_no_game_data.py` pass on the changed files, and `git diff --check` is clean. GPUs tested: none needed (CPU-only test); host GTX 1050 Ti, Windows 10. AI-assisted: yes. Antigravity (Gemini 4 Neon) wrote the first version of the change and the test; Claude Code reviewed it, tightened the owner check and ran the tests and mutants. ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <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#2364
No description provided.