build_dstorage_history: write the history report atomically #1667

Open
github-bot wants to merge 1 commit from github-bot/gh-1667 into main AGit
Owner

@⁠xel1uz on GitHub · 2026-11-25 07:39 UTC · original
xel1uz:fix/build-dstorage-history-atomic-out → main · +170 −2 · 2 files · 1 commits · open

Item ID: BUILD-DSTORAGE-HISTORY-ATOMIC-OUT (new item; the coordination issue #14 is at GitHub's 2,500-comment cap, so the claim lives here)

Claimed as: BUILD-DSTORAGE-HISTORY-ATOMIC-OUT - a failed --out write truncates an existing file and leaves a partial one behind. Reproduced on 42537e7b.

What and why

The report was written with Path.write_text(json.dumps(...)), which truncates the destination before any serialization happens: a report that fails part-way leaves a partial JSON and the previous history is already gone, while the caller only sees the write error.

The JSON now goes to a temporary file in the destination directory and is moved into place with os.replace once it is written; a failure removes the temp file and leaves the existing report untouched.

Tested

  • pytest tests/test_build_dstorage_history.py -q with the tool at 42537e7b: 1 failed, 2 passed - the failure is test_failed_write_preserves_existing_report.

  • With this branch: 3 passed.

  • Repro with RLIMIT_FSIZE=128 and SIGXFSZ ignored: the build returns 1 (File too large), and the report went 30 -> 128 bytes on the original while staying 30 bytes here, with no temp file left.

  • git diff --check clean; python3 -I scripts/check_no_game_data.py clean on the changed files.

GPUs tested: n/a - CPU only, no Vulkan device involved.

AI-assisted: Yes.

Checklist

  • Based on current main (42537e7b) and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption; synthetic inputs only.
  • A new translator rule in the contract table of docs/design.md - not applicable, no translator change.
**@⁠xel1uz** on GitHub · 2026-11-25 07:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1667) `xel1uz:fix/build-dstorage-history-atomic-out` → `main` · +170 −2 · 2 files · 1 commits · open Item ID: `BUILD-DSTORAGE-HISTORY-ATOMIC-OUT` (new item; the coordination issue #14 is at GitHub's 2,500-comment cap, so the claim lives here) Claimed as: `BUILD-DSTORAGE-HISTORY-ATOMIC-OUT` - a failed `--out` write truncates an existing file and leaves a partial one behind. Reproduced on 42537e7b. ### What and why The report was written with `Path.write_text(json.dumps(...))`, which truncates the destination before any serialization happens: a report that fails part-way leaves a partial JSON and the previous history is already gone, while the caller only sees the write error. The JSON now goes to a temporary file in the destination directory and is moved into place with `os.replace` once it is written; a failure removes the temp file and leaves the existing report untouched. ### Tested - `pytest tests/test_build_dstorage_history.py -q` with the tool at 42537e7b: **1 failed, 2 passed** - the failure is `test_failed_write_preserves_existing_report`. - With this branch: **3 passed**. - Repro with `RLIMIT_FSIZE=128` and `SIGXFSZ` ignored: the build returns 1 (`File too large`), and the report went **30 -> 128 bytes** on the original while staying **30 bytes** here, with no temp file left. - `git diff --check` clean; `python3 -I scripts/check_no_game_data.py` clean on the changed files. GPUs tested: n/a - CPU only, no Vulkan device involved. AI-assisted: Yes. ### Checklist - [x] Based on current `main` (42537e7b) and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption; synthetic inputs only. - [ ] A new translator rule in the contract table of `docs/design.md` - not applicable, no translator change.
Write history JSON through a same-directory temporary file and replace it only after serialization completes. Clean up temporary files on failures and preserve the previous file mode.

Synthetic short-write reproduction (the subprocess test ignores SIGXFSZ and sets RLIMIT_FSIZE=128 after the first JSON write, then seeds the existing intermediate report with 30 bytes):

Command: /home/hampa/work/.venv/bin/python -m pytest tests/test_build_dstorage_history.py -q

Before: git checkout upstream/main -- tools/build_dstorage_history.py; pytest rc 1 (1 failed, 2 passed). Reproduction subprocess rc 1 (EFBIG); existing report 30 -> 128 bytes.

After: git checkout HEAD -- tools/build_dstorage_history.py; pytest rc 0 (3 passed). Reproduction subprocess rc 1 (EFBIG); existing report 30 -> 30 bytes, no .tmp file left.

Checks: git diff --check -> rc 0; python3 -I scripts/check_no_game_data.py tools/build_dstorage_history.py tests/test_build_dstorage_history.py -> rc 0.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:00:02 +00:00
Author
Owner

@⁠DeadlyChirp on GitHub · 2026-11-25 08:12 UTC · original

The new test file fails on native Windows before exercising the atomic write. I reproduced on f0239cff, Windows 11 and Python 3.12.10, with the synthetic fixtures:

python -I tests/test_build_dstorage_history.py -v

All three cases ran (2 failures, 1 error). The two failure-injection cases launch a child that imports the Unix-only resource module. It exits with ModuleNotFoundError before creating history.json or injecting the write failure. The success case reaches collect(), but CreateProcess rejects the extensionless Python shebang trainer with WinError 193.

Could the failure injection use a deterministic write error that also works on Windows, and the success fixture invoke the test trainer through the Python interpreter? That would let Windows exercise preservation, cleanup and success as intended. CPU-only, AI-assisted review.

**@⁠DeadlyChirp** on GitHub · 2026-11-25 08:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1667#issuecomment-7156745632) The new test file fails on native Windows before exercising the atomic write. I reproduced on f0239cff, Windows 11 and Python 3.12.10, with the synthetic fixtures: python -I tests/test_build_dstorage_history.py -v All three cases ran (2 failures, 1 error). The two failure-injection cases launch a child that imports the Unix-only resource module. It exits with ModuleNotFoundError before creating history.json or injecting the write failure. The success case reaches collect(), but CreateProcess rejects the extensionless Python shebang trainer with WinError 193. Could the failure injection use a deterministic write error that also works on Windows, and the success fixture invoke the test trainer through the Python interpreter? That would let Windows exercise preservation, cleanup and success as intended. CPU-only, AI-assisted review.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-25 09:11 UTC · original

Independent verification (Windows, Python 3.11.9): two issues.

  1. The fix protects nothing real: both changed writes target temp/"history.json" inside a fresh tempfile.TemporaryDirectory — no pre-existing file can exist there in production, and a mid-write failure already aborts before pack.write_profile consumes it. The real outputs are untouched: --out .bcph was already atomic via (temp/"history.bcph").replace(output), and --json-out still goes through the non-atomic _write_json_deterministic -> open(path,"w"). The claimed "failed --out write truncates an existing file" bug is not fixed for any real output; the test only demonstrates it by monkeypatching TemporaryDirectory to a fixed dir with a planted report. Suggest either applying the atomic write to the actual --json-out path or re-scoping.

  2. All 3 new tests fail on Windows: python -I tests/test_build_dstorage_history.py -> FAILED (2 failures, 1 error) — the synthetic trainer is a #!/usr/bin/env python3 shell script (WinError 193) and _BUILD_WITH_LIMIT does import resource (POSIX-only). Same Windows-CI failure as the sibling PR. Needs skipUnless(os.name == "posix") or platform-aware equivalents.

check_no_game_data clean; neighbor test_bcpack_history_calib.py passes. AI-assisted verification.

**@⁠whoalin1** on GitHub · 2026-11-25 09:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1667#issuecomment-7157995254) Independent verification (Windows, Python 3.11.9): two issues. 1. **The fix protects nothing real**: both changed writes target `temp/"history.json"` inside a fresh `tempfile.TemporaryDirectory` — no pre-existing file can exist there in production, and a mid-write failure already aborts before `pack.write_profile` consumes it. The real outputs are untouched: `--out` .bcph was already atomic via `(temp/"history.bcph").replace(output)`, and `--json-out` still goes through the non-atomic `_write_json_deterministic` -> `open(path,"w")`. The claimed "failed --out write truncates an existing file" bug is not fixed for any real output; the test only demonstrates it by monkeypatching TemporaryDirectory to a fixed dir with a planted report. Suggest either applying the atomic write to the actual `--json-out` path or re-scoping. 2. **All 3 new tests fail on Windows**: `python -I tests/test_build_dstorage_history.py` -> FAILED (2 failures, 1 error) — the synthetic trainer is a `#!/usr/bin/env python3` shell script (WinError 193) and `_BUILD_WITH_LIMIT` does `import resource` (POSIX-only). Same Windows-CI failure as the sibling PR. Needs `skipUnless(os.name == "posix")` or platform-aware equivalents. `check_no_game_data` clean; neighbor `test_bcpack_history_calib.py` passes. AI-assisted verification.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1667/head:github-bot/gh-1667'
git switch 'github-bot/gh-1667'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1667'
git switch 'github-bot/gh-1667'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1667'
git switch 'github-bot/gh-1667'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1667'
git switch 'main'
git merge --squash 'github-bot/gh-1667'
git switch 'main'
git merge --ff-only 'github-bot/gh-1667'
git switch 'main'
git merge 'github-bot/gh-1667'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.