Skip to content

First light: each prepare stages into its own folder so concurrent runs do not collide (BUILD-1053) - #3772

Open
Koro4e wants to merge 1 commit into
ItsNotRexed:mainfrom
Koro4e:build-1053
Open

Koro4e wants to merge 1 commit into
ItsNotRexed:mainfrom
Koro4e:build-1053

Conversation

@Koro4e

@Koro4e Koro4e commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Closes BUILD-1053 (finding @shdann). Claim: #3574 comment 7085952474.

translate() staged into the shared work/first_light/dxbc and work/first_light/spirv folders, so a second
prepare sharing --work with a different --out replaced the first run's staged index and translator results
while it was in flight: run A exited with "translator has no ps result for first" after B published its pair.

Every call now stages into a unique prepare-XXXX folder under work/first_light and returns it; main()
copies the .spv results from there and removes the folder once published. A failed run keeps its folder for
diagnosis. This also makes each run immune to another run's stale leftovers (adjacent to, and superseding part
of, the sequential empty-dirs fix in #3577 - happy to reconcile with @zzEmmazz if both move).

Regression test in OverlappingPrepareTests: run A starts, a full second prepare runs synchronously at A's
first tool call (after staging, before the unpack reads), then A resumes. On the old layout it fails with the
reported symptom; now both runs publish their own pair and the staging folders are cleaned up. The
shared-path assumption in ShaderPreparationTests now reads the staging folder from the tool's argv.

Tests: python -I tests/test_first_light.py 12 OK / 3 package-gated skips. Tool calls are stand-ins; staging,
orchestration and shader selection are real, per the finding. No game files, no GPU.
AI-assisted (Claude Code).

@ray3242

ray3242 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

This PR overlaps #3689 (@Sc4ros), which was opened earlier for the same BUILD-1053 item and is a draft that depends on #3577. In #3574, the coordinator asked @Koro4e to keep one of the two First Light items and leave BUILD-1053 to @Sc4ros. #3771 is already open for BUILD-1052.

The two PRs fix the shared --work race in different ways:

They conflict: merging this PR onto #3689's head (68773319) gives a content conflict in tools/first_light.py, so only one of them can land as it stands. If per-run staging is the better design, it may be worth agreeing on that with @Sc4ros in #3574 or on #3689, rather than keeping two PRs open. AI-assisted (Devin).

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested at the head (7a27946) on Windows 11 / Python 3.13.0: tests/test_first_light.py 12 run, 3 skipped; the area's 21 run, 3 skipped; compileall clean; merge clean. Negative control reproduces the exact symptom on main: translator has no vs result for first.

The staging fix is right: tempfile.mkdtemp(prefix="prepare-") (tools/first_light.py:97) gives each prepare its own folder, rmtree only removes that path (:561-562), and the output/manifest paths are unchanged.

Two notes, so the "concurrent runs do not collide" claim is accurate:

  • Cold-start carving is still shared: ensure_carve (:60-69) writes work/shaders/dxbc, so two first-time concurrent prepares can race, and shader_carve.py:115 writes .dxbc in place - a copy can read a partial file.
  • Cleanup contradicts the docstring: a publish failure deletes the staging folder (:561-562) while translate's docstring (:92-93) says failures keep it, and failures inside translate leak the folder permanently.

Approve with those documented or fixed. AI-assisted review, no game files, no GPU.

@Sc4ros

Sc4ros commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

A review note from the other BUILD-1053 PR, #3689. I'm closing it in favour of this one: yours came first and handles the reported case cleanly.

Per-run staging fixes the case shdann found: two prepares with separate --out folders no longer replace each other's staged pair. Two shared places are still open, and #3689 covered them with a lock. I'm putting them here so they don't get lost. Your call whether they belong in this PR or in a follow-up:

  1. The carve. ensure_carve() runs shader_carve.py into <work>/shaders/dxbc whenever index.json is missing. With a fresh --work, two prepares (or a prepare and a --list) can both carve into the same folder at once. The second one can also read a partly written folder.
  2. The output folder. Two runs with the same --out still overwrite each other's vs.spv, ps.spv, vs.dxbc, ps.dxbc, index.json, mesh.bin and manifest.json. That includes the default <work>/first_light/out when --out is left out, so two plain runs on one --work mix their outputs. A run without --obj can also delete the other run's mesh.bin.
  3. Rebase note. main now has first_light.py: each run starts with empty first_light/dxbc and first_light/spirv (BUILD-1011) #3577's reset_run_folders() (the PR was closed as already on main). It removes first_light/dxbc and first_light/spirv at the start of every run. With per-run staging nothing writes there any more, so the function can probably go, or clear only old leftovers. As it is, it would also delete those folders under a prepare that is still running.

What #3689 did for 1 and 2 (take any of it you like; same project, same licence):

  • prepare_lock(work): a non-blocking OS lock on one byte of <work>/first_light/prepare.lock (msvcrt.locking on Windows, fcntl.flock elsewhere). It is held from the carve to the last file written into --out, and --list takes it too. A second run stops at once with a message that names the lock and suggests its own --work. The lock belongs to the process, so a crashed run leaves nothing to delete.
  • A lighter option for 1: carve into a temporary folder under <work>/shaders and os.replace it into place, so index.json only appears once the carve is complete.
  • A lighter option for 2: lock per --out instead of per --work, so runs with different --out still run in parallel, which is the point of your PR.
  • Tests: an interleaving test (a second prepare that starts while the first holds the lock is refused and leaves --out alone) and a two-process check on Windows. They are on branch first-light-prepare-lock of my fork, commit 6877331.

If you'd rather keep this PR to the staging change, I can open a small follow-up on top of it once it lands (claimed on the coordination issue first). Not tested here: I read the diff but did not run this branch, because it conflicts with current main.

AI-assisted (Claude Code).

@Koro4e

Koro4e commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Status after the overnight reviews: the rebased change is ready locally (5151bf3a on current main 3faac55): translate() stages each prepare into prepare-XXXX and no longer calls reset_run_folders (that would race two concurrent runs), keeps the symlink/junction refusal, main() removes the folder in a finally block. RunFolderTests now checks old shared leftovers are ignored, and the refusal test goes through main(); OverlappingPrepareTests still fails on main with 'translator has no vs result for first'. 29 tests run / 4 skips, all pass. Thanks @Sc4ros for closing #3689 in favour of this one and for the carve + same---out notes; those remain shared by design and I agree they belong in a separate claimed follow-up. Publication hit the same fork-sync problem as #3771 (fork 4,597 commits behind, push blocked, sync API needs workflow scope), so the branch still has an old parent and GitHub shows a conflict; the two-file content is up to date. — @Koro4e

@tunczzz

tunczzz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 5cbeee40 (merge base f847748c) on Windows 11 10.0.26300 with an NVIDIA GeForce RTX 5070 Ti (Blackwell, subgroup 32..32), driver 616.56. Result: pass.

  • Runtime built from this head: MSVC 19.44 (BuildTools 2022), Ninja, RelWithDebInfo, -DLEONIDA_VULKAN_HEADERS_FETCH=ON (no Vulkan SDK; the loader comes from the NVIDIA driver). glslangValidator and llvm-mc 21 came from WSL Ubuntu. Build exit 0.
  • ctest -j 3: 140/140 passed.
  • scripts/run_gpu_tests.py --build <this build>: 153/199 passed, 46 skipped, 0 failed.
  • The merge base f847748c is older than main 3faac557, so main's newer tests aren't in this tree (it is 2,236 commits behind; 37 ctests and 18 GPU tests are main-only). Every test that exists in both gives the same result as on main 3faac557 (ctest 177/177, GPU 170/217).
  • This base predates the runner bug that drops cs_tbuffer_store, so that test runs here and passes.
  • I didn't run a first-light prepare, so the concurrent-staging change wasn't tested end to end here. A real prepare needs game files.

Not run: other GPUs/vendors, Linux, the Vulkan validation layer (not installed). No game files.

AI-assisted (Claude Code).

@Koro4e

Koro4e commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (8575d36) - the conflicts with the round-44 merge were resolved locally, tests pass locally (package-gated skips unchanged). Head updated on the fork.

@hcampbell98

Copy link
Copy Markdown

Tested 0630d81f4 merged with main 86a7807dd on Linux (WSL2, CPython 3.12.13 and 3.14.4, CPU-only, synthetic fixtures): fail (sibling tests break due to translate() signature change).

  • Failure in sibling First Light tests (pass on origin/main 86a7807dd): translate(work, picked) now returns tuple[dict, str] (return result, staging) instead of dict, and main() unpacks meta, staging = translate(work, picked). Both tests/test_first_light_mesh_draw.py and tests/test_first_light_stale_mesh.py stub first_light.translate returning {"vs": {}, "ps": {}}, so meta, staging unpacks "vs" and "ps" and shutil.copy(os.path.join(staging, "spirv", ...)) raises FileNotFoundError:
    • python3 -I tests/test_first_light_mesh_draw.py: 2 errors in 3 tests (FileNotFoundError: ... 'ps/spirv/vs.spv').
    • python3 -I tests/test_first_light_stale_mesh.py: 1 error in 1 test (FileNotFoundError: ... 'ps/spirv/a.spv').
    • pytest -q across all 5 tests/test_first_light*.py files: 3 failed, 58 passed, 4 skipped, 90 subtests passed.
  • Suites that pass at 0630d81f4: tests/test_first_light.py (48 run: 44 OK, 4 skipped for LEONIDA_FIRST_LIGHT_DIR / vk_harness) and tests/test_first_light_bad_json.py (9 OK) on both CPython 3.12.13 and 3.14.4 (-I); tests/test_first_light_harness_paths.py (4 OK).
  • Negative control (origin/main -- tools/first_light.py): tests/test_first_light.py errors on OverlappingPrepareTests (SystemExit: translator has no vs result for first) and RunFolderTests.test_a_second_run_holds_only_its_own_pair (FileExistsError), confirming the regression test catches the race.
  • Additional checks: translate's docstring now accurately documents failure cleanup; trial merge on top of #3771 (dd2dc15a1) is clean (test_first_light.py 49 run / 4 skipped, test_first_light_bad_json.py 9 OK); git diff --check, scripts/check_no_game_data.py, and compileall -q are clean.

Not run: vk_harness GPU tests or real ensure_carve (no GPU / no game files).

AI-assisted.

@boesbo

boesbo commented Nov 23, 2026

Copy link
Copy Markdown

Independent macOS arm64 check of 0630d81, CPython 3.13.18. I ran the overlapping-prepare regression against unmodified upstream 48ff424 as a negative control. With a shared work folder and separate outputs it failed because the first prepare lost its selected vertex shader. On this PR head the same regression passed 10 consecutive runs, including both published manifests and staging-folder cleanup.

The first-light suite passed 44 tests. Three prepared-package tests and one optional harness test were skipped. The concurrency check used the synthetic tool stand-ins. AI-assisted with Codex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants