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

Closed
github-bot wants to merge 1 commit from github-bot/gh-2361 into main AGit
Owner

@⁠qhukz13 on GitHub · 2026-11-26 15:23 UTC · original
qhukz13:cont/compile-owner-race → main · +367 −4 · 3 files · 1 commits · closed without merging

Claimed in the coordination issue as: RT-COMPILE-OWNER-RACE (#1608)

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

**@⁠qhukz13** on GitHub · 2026-11-26 15:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2361) `qhukz13:cont/compile-owner-race` → `main` · +367 −4 · 3 files · 1 commits · closed without merging Claimed in the coordination issue as: RT-COMPILE-OWNER-RACE (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) 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)
PipelineCompiler runs a general worker and a graphics-only worker, and next_locked previously ignored Job::owner, allowing two jobs for the same graphics PSO to run concurrently. PipelineState::last_release calls after_running(owner, ...) to defer deletion, which attaches to the first Running entry of that owner; when that worker finishes, it can delete the PSO while the other worker is still building another variant on the same object.

Skip queued jobs in next_locked when their owner already has an unfinished Running entry (for both workers), and call wake_.notify_all() in run() after erasing a finished job from running_ so held-back jobs can start. Add standalone CPU test d3d12_compile_owner_test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 15:26:59 +00:00
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 15:35 UTC · original · reviewed

MSVC/Windows 11: d3d12_compile_owner passed 10 consecutive runs. The suite covers same-owner graphics and compute serialization, pending work waking up, and different graphics owners still running concurrently. This checks the exact PR head. The open #2233 stack was outside these runs.

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-26 15:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2361#pullrequestreview-6076848986) · **reviewed** MSVC/Windows 11: `d3d12_compile_owner` passed 10 consecutive runs. The suite covers same-owner graphics and compute serialization, pending work waking up, and different graphics owners still running concurrently. This checks the exact PR head. The open #2233 stack was outside these runs. AI-assisted.
Author
Owner

@⁠Tyrannys on GitHub · 2026-11-27 11:53 UTC · original

@⁠qhukz13 thank you for finding this race and for fixing it on main. I missed your RT-COMPILE-OWNER-RACE claim when I fixed the same thing in #2233 yesterday, sorry for the overlap. Credit for the finding and the fix is yours.

#2233's change for this (9b8760d32) is the same owner check in the single-worker branch of next_locked (the pool branch already had one). I am aligning #2233's single-worker branch with your code (the same find_if with the none_of over running_, and your comment), so whichever of the two lands second has a trivial merge. If #2361 lands first, #2233 takes your version and drops its own copy. #2233 already wakes the idle workers after each job, as yours does.

AI-assisted (Claude Code)

**@⁠Tyrannys** on GitHub · 2026-11-27 11:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2361#issuecomment-7217593290) @⁠qhukz13 thank you for finding this race and for fixing it on `main`. I missed your RT-COMPILE-OWNER-RACE claim when I fixed the same thing in #2233 yesterday, sorry for the overlap. Credit for the finding and the fix is yours. #2233's change for this (9b8760d32) is the same owner check in the single-worker branch of `next_locked` (the pool branch already had one). I am aligning #2233's single-worker branch with your code (the same `find_if` with the `none_of` over `running_`, and your comment), so whichever of the two lands second has a trivial merge. If #2361 lands first, #2233 takes your version and drops its own copy. #2233 already wakes the idle workers after each job, as yours does. AI-assisted (Claude Code)
github-bot closed this pull request 2026-11-27 13:28:11 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-27 13:26 UTC · original · edited 2026-11-27 13:28 UTC

Closing as superseded by #2233, which landed on main (f5c3ea2e0) with this exact single-worker next_locked owner check (crediting @⁠qhukz13 / #2361 in both runtime/shims/d3d12_pipeline_compile.h and docs/d3d12_vulkan.md), wake_.notify_all(), and the LEONIDA_COMPILE_THREADS=1 regression test (test_one_worker_and_the_graphics_worker_build_one_job_of_a_pso in d3d12_compile_pool_test.cpp).

Thank you for catching the race and writing the fix and reproducer. If you want to add Case 4 (a non-graphics job waking after a same-owner graphics job on the graphics-only worker under LEONIDA_COMPILE_THREADS=1) to d3d12_compile_pool_test.cpp, a small test-only follow-up is welcome.

AI-assisted review (Antigravity, Gemini 4 Neon).

**@⁠aslan2142** on GitHub · 2026-11-27 13:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2361#issuecomment-7219477088) · edited 2026-11-27 13:28 UTC Closing as superseded by #2233, which landed on `main` (`f5c3ea2e0`) with this exact single-worker `next_locked` owner check (crediting @⁠qhukz13 / #2361 in both `runtime/shims/d3d12_pipeline_compile.h` and `docs/d3d12_vulkan.md`), `wake_.notify_all()`, and the `LEONIDA_COMPILE_THREADS=1` regression test (`test_one_worker_and_the_graphics_worker_build_one_job_of_a_pso` in `d3d12_compile_pool_test.cpp`). Thank you for catching the race and writing the fix and reproducer. If you want to add Case 4 (a non-graphics job waking after a same-owner graphics job on the graphics-only worker under `LEONIDA_COMPILE_THREADS=1`) to `d3d12_compile_pool_test.cpp`, a small test-only follow-up is welcome. AI-assisted review (Antigravity, Gemini 4 Neon).

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.