Skip to content

rdna2spirv: isolate and clean up index staging files (SHADER-INDEX-ATOMIC) - #558

Merged
QemmHD merged 3 commits into
ProjectLeonida:mainfrom
JiyouProt:codex/shader-index-atomic
Nov 24, 2026
Merged

QemmHD merged 3 commits into
ProjectLeonida:mainfrom
JiyouProt:codex/shader-index-atomic

Conversation

@JiyouProt

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-INDEX-ATOMIC — #14 (comment)

What and why

write_index used the fixed sibling <index>.tmp. An existing file at that name was overwritten, a directory at that name blocked a valid write, and concurrent writers could replace or remove each other's staging file. Failed writes also left partial staging files behind.

Use a unique temporary file in the destination directory, close it before publishing through the existing Windows-aware replace_retry, and remove it if serialization, fsync or replacement fails (including interruption). Keep fsync, sorted JSON, LF output and atomic replacement. Temporary names do not match the shard-merge glob. No shader translation rule changes.

Six synthetic regression tests reproduce the old behavior and cover an unrelated .tmp file/directory, serialization/fsync/replace failures, interruption and overlapping writers. The overlap test also checks that the shard glob sees only complete indexes.

Tested

Windows, Python 3.14.3, based on main ece4ba5 (current when this task started).

  • All six new tests fail against the original writer and pass with this change.
  • python -I tests/test_rdna2spirv_index_write.py: 6 passed.
  • python -I tests/test_rdna2spirv_merge.py: 30 passed.
  • python -I tests/test_text_write_line_endings.py: 3 passed.
  • Full semantic suite with SPIRV-Tools v2026.4 available: 465 files, 6,103 tests, 0 files failed (532 s, 8 jobs).
  • compileall, make_game.py --help, no-game-data scan of both changed files and git diff --check: passed.

GPUs tested: n/a — index file publication only; fixtures are synthetic dictionaries. No game files needed or used.

AI-assisted: yes, with Codex.

Checklist

  • Based on current main at task start; GitHub mergeability checked before opening.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption, including in logs or reports.
  • No translator rule added, so no contract table change needed.

@JiyouProt

Copy link
Copy Markdown
Contributor Author

Portability follow-up in 62c061e: the unique temporary file must not impose its private 0600 permissions on the published index. On POSIX, use the existing file_mode.published_mode helper before replacement; this preserves an existing destination mode or the directory/umask default for a new file, without changing the process umask. Permission lookup/chmod failures retain the old index and clean up staging. Windows retains its existing behavior.

Validation: index writer 9 tests (8 passed, 1 POSIX-only skip on this Windows host); shard merge 30 passed; LF writers 3 passed; compile and no-game-data checks passed. Both new portable permission tests fail without this addition. The full 6,103-test run reported above was before this mode-only addition; no translation rules changed.

The same portability fix is included in #344 at 6164357: verifier 65 tests (62 passed, 3 platform skips) and shared file_mode tests 5 (3 passed, 2 POSIX-only skips). Both PRs remain ready for review. AI-assisted.

@Dezmonius

Copy link
Copy Markdown
Contributor

Independent Windows check at 62c061e against 1c93c86, CPython 3.14.7: 8 index-writer tests passed (1 POSIX-only skip), 30 shard-merge tests passed, and 3 LF-output tests passed.

I also checked publication with two separate Python processes writing different synthetic JSON payloads to the same destination. A barrier held both processes immediately before the real rename. On main, both staged to the same path: the first publisher wrote the second process's payload, and the second publisher failed with FileNotFoundError. On the PR, the staging paths were distinct and outside index.part*.json; readers saw the existing complete index until the first release, then each complete new payload as its process published. Both processes exited 0.

With a real Win32 destination handle that omitted FILE_SHARE_DELETE, publication succeeded after the handle was released. Holding the handle past a shortened retry budget produced WinError 5: the original index stayed intact in both versions, and only the PR removed the staging file. These checks use the actual filesystem and replace path, with a barrier and shorter retry budget in the probe only.

CPU-only synthetic JSON. AI-assisted (Codex).

@sodove

sodove commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

At 62c061e, the POSIX permission case passed on Ubuntu under WSL, Python 3.14.4. All 9 index writer tests passed there, including the real permission bits for an existing index and a new file.

I also confirmed the Windows behavior above with three independent processes on Python 3.12. Main used one staging path. The first process published the third process's content and the other two failed. With the PR, all three processes published their own complete indexes in release order. The native sharing conflict and retry-expiry cleanup checks also agreed with the report above. Synthetic JSON. AI-assisted.

@QemmHD

QemmHD commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks, the unique staging file is a good fix, and its tests pass on Windows (index_write 2 passed / 10 failed on main -> 8 passed). One thing before it can merge: rdna2spirv.py now imports tools/file_mode.py, so that file also needs listing in runtime/cmake/translator_deps.cmake, in make_game.py's translation sources, and in the tools paths of .github/workflows/runtime.yml. Without those, test_cmake_translator_deps, test_shader_stage_resume and test_runtime_paths fail on the merge (565 -> 562 passed, 3 failed). Happy to re-test after a push.

AI-assisted (Claude Code, Claude Opus 5.5).

Address review of ProjectLeonida#558: include file_mode.py in CMake shader dependencies,
make_game translation source digests, and both Runtime workflow path filters.
AI-assisted with Codex.
@JiyouProt

Copy link
Copy Markdown
Contributor Author

@QemmHD Thanks for catching the missing dependency registration. Fixed in e4dc13b: tools/file_mode.py is now in runtime/cmake/translator_deps.cmake, make_game.py TRANSLATION_SOURCES, and both push/pull_request tools path filters in .github/workflows/runtime.yml.

I reproduced all three failures on 62c061e before the change. After it, test_cmake_translator_deps (5), test_shader_stage_resume (46) and test_runtime_paths (5) all pass: 56/56. I also applied the complete PR cleanly to current main d7576d5 in a separate checkout; the same 56 tests pass there, along with the index writer tests (8 passed, 1 POSIX-only skip on Windows).

On the PR branch, index writer: 8 passed/1 skipped; shard merge: 30 passed; file_mode: 3 passed/2 POSIX-only skips; LF writers: 3 passed. No-game-data and diff whitespace checks pass. Windows/Python 3.14.3, CMake available, synthetic inputs only. No translation behavior change in this follow-up. AI-assisted with Codex.

@QemmHD

QemmHD commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Merging. Why merged: each rdna2spirv index write now uses its own staging file and cleans it up, so parallel translations can't clobber each other's index; and, after review, the new tools/file_mode.py is registered as a translator input (translator_deps.cmake, make_game.py's translation sources, runtime.yml paths).
Why it's good: re-tested at e4dc13b on main d7576d5 (Windows 11, RTX 4070 SUPER): the new index-write tests go from 2 passed / 10 failed on main to 8 passed; the translator-input tests stay 56/56; the full repo-consistency set is 565 passed / 0 failed; compileall clean. Thanks @JiyouProt for the quick fix.

AI-assisted (Claude Code, Claude Opus 5.5).

@QemmHD
QemmHD merged commit 21625bc into ProjectLeonida:main Nov 24, 2026
demoz53 added a commit to demoz53/leonida that referenced this pull request Nov 24, 2026
Brings 58 PRs integration did not have (among them ProjectLeonida#359 ProjectLeonida#360 ProjectLeonida#402 ProjectLeonida#497 ProjectLeonida#557 ProjectLeonida#558
ProjectLeonida#560 ProjectLeonida#561 ProjectLeonida#572 ProjectLeonida#573 ProjectLeonida#574 ProjectLeonida#577) and the newer heads of ProjectLeonida#401 and ProjectLeonida#404. 15 files
conflicted; resolved by hand:

- frame_pacer.h, d3d12_xs_device.h/.cpp: ProjectLeonida#402's pacer (per-event ticks,
  issue_token_locked) now issues from ProjectLeonida#414's shared FrameTokenSource, so a token
  from the device-side WaitFrameEventX is one the pacer accepts (towade's ProjectLeonida#14
  finding: without it every PresentX is refused). The dead frame_next_token and
  issued_ counters are dropped. Queue slot 32 stays ProjectLeonida#414's PresentX; ProjectLeonida#402's
  presentation-handler routes serve slot 30 or LEONIDA_XS_PRESENT_X_SLOT.
  ProjectLeonida#402's GetFrameStatisticsX route moves into ProjectLeonida#385's route_xbox_device_slot.
- acphal.cpp/acphal_test.cpp: upstream's reworked SHAPE allocation and release;
  the release writes nothing through its first argument, as ProjectLeonida#405 requires; both
  sides' tests kept. Upstream's ProjectLeonida#401/ProjectLeonida#404 heads remove lifecycle.cpp and the C++
  xma2_decoder.cpp (replaced by runtime/xma_decoder), so build_runtime.ps1's
  cl.exe list drops them and keeps ProjectLeonida#549's and ProjectLeonida#400's files.
- vulkan_backend.h, statistics_sum.comp, its SPIR-V header, d3d12_mesh_test,
  replay_query_edges_test, d3d12_vulkan_device.cpp, docs: ProjectLeonida#577's clipping
  counter on top of ProjectLeonida#540, which both sides carry; the Timestamps sentence keeps
  integration's corrected wording (c33ce87).
- xs_proxy_mock_test.cpp: both sides' cases; pr_conflict_label.py keeps the
  guarded reconfigure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

4 participants