feature_scan: write --out atomically and keep the existing report on failure #1648

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

@⁠xel1uz on GitHub · 2026-11-25 07:00 UTC · original
xel1uz:fix/feature-scan-atomic-out → main · +84 −2 · 2 files · 1 commits · open

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

Claimed as: FEATURE-SCAN-ATOMIC-OUT - a failed --out write truncates an existing report and leaves a partial file. Reproduced on 42537e7b.

What and why

feature_scan wrote its report with open(a.out, "w") followed by json.dump, so the target was truncated the moment the file was opened, before anything had been serialized. A write that then fails (EFBIG, ENOSPC, an interrupt) leaves a partial JSON behind and the previous report gone - the failure path prints ERROR: cannot write ... and returns 2, but the damage is already done and invisible to the caller.

The write is now atomic: the JSON goes to a tempfile.mkstemp file in the same directory as the target, is flushed and closed, and is moved into place with os.replace; on any failure the temp file is removed and the existing report is left untouched. The error message, the exit code 2 and the makedirs behaviour for a missing parent folder are unchanged.

Reproduction (synthetic input, 32-byte existing report, ulimit -f 1 forcing a short write):

  • original: exit 2 and the report is now 1024 bytes - truncated/partial, previous content lost;
  • with this patch: exit 2, the report is still the original 32 bytes, and no temp file is left in the directory.

Tested

  • pytest tests/test_feature_scan.py -q with the tool at 42537e7b: 1 failed, 15 passed - the failure is FeatureScanAtomicOutputTests::test_failed_write_keeps_existing_report (assertEqual(stream.read(), original)).
  • With this branch: 16 passed.
  • git diff --check clean; python3 -I scripts/check_no_game_data.py tools/feature_scan.py tests/test_feature_scan.py clean.

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:00 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1648) `xel1uz:fix/feature-scan-atomic-out` → `main` · +84 −2 · 2 files · 1 commits · open Item ID: `FEATURE-SCAN-ATOMIC-OUT` (new item; the coordination issue #14 is at GitHub's 2,500-comment cap, so the claim lives here) Claimed as: `FEATURE-SCAN-ATOMIC-OUT` - a failed `--out` write truncates an existing report and leaves a partial file. Reproduced on 42537e7b. ### What and why `feature_scan` wrote its report with `open(a.out, "w")` followed by `json.dump`, so the target was truncated the moment the file was opened, before anything had been serialized. A write that then fails (EFBIG, ENOSPC, an interrupt) leaves a partial JSON behind and the previous report gone - the failure path prints `ERROR: cannot write ...` and returns 2, but the damage is already done and invisible to the caller. The write is now atomic: the JSON goes to a `tempfile.mkstemp` file in the *same* directory as the target, is flushed and closed, and is moved into place with `os.replace`; on any failure the temp file is removed and the existing report is left untouched. The error message, the exit code 2 and the `makedirs` behaviour for a missing parent folder are unchanged. Reproduction (synthetic input, 32-byte existing report, `ulimit -f 1` forcing a short write): - original: exit 2 and the report is now **1024 bytes** - truncated/partial, previous content lost; - with this patch: exit 2, the report is still the original **32 bytes**, and no temp file is left in the directory. ### Tested - `pytest tests/test_feature_scan.py -q` with the tool at 42537e7b: **1 failed, 15 passed** - the failure is `FeatureScanAtomicOutputTests::test_failed_write_keeps_existing_report` (`assertEqual(stream.read(), original)`). - With this branch: **16 passed**. - `git diff --check` clean; `python3 -I scripts/check_no_game_data.py tools/feature_scan.py tests/test_feature_scan.py` clean. 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 JSON to a same-directory temporary file and replace the report only after serialization succeeds. On write failure, keep the existing report and remove the temporary file; preserve the current error message and exit code.

Evidence (run from /home/hampa/work/leonida-feature-scan):

Tests:
- Original: git checkout upstream/main -- tools/feature_scan.py && /home/hampa/work/.venv/bin/python -m pytest tests/test_feature_scan.py -q -> rc 1; 1 failed, 15 passed. The existing report was empty instead of the 23-byte sentinel.
- Fixed: git checkout HEAD -- tools/feature_scan.py && /home/hampa/work/.venv/bin/python -m pytest tests/test_feature_scan.py -q -> rc 0; 16 passed.

File-size-limit reproduction (synthetic shaders only):
- Setup: mkdir -p /home/hampa/.hermes/profiles/mikuling/cache/scratch/feature-scan-ulimit1.hlUt9B/shaders/dxbc /home/hampa/.hermes/profiles/mikuling/cache/scratch/feature-scan-ulimit1.hlUt9B/out; python3 -c 'import json,sys; json.dump([{"stage":"synthetic_stage_"+str(i)+"_"+("x"*80)} for i in range(40)], open(sys.argv[1],"w"))' /home/hampa/.hermes/profiles/mikuling/cache/scratch/feature-scan-ulimit1.hlUt9B/shaders/dxbc/index.json; printf '{"previous": "sentinel report"}\\n' > /home/hampa/.hermes/profiles/mikuling/cache/scratch/feature-scan-ulimit1.hlUt9B/out/report.json
- Before: bash -c 'ulimit -f 1; python3 -I -B tools/feature_scan.py "$1" --out "$2" >/dev/null' _ /home/hampa/.hermes/profiles/mikuling/cache/scratch/feature-scan-ulimit1.hlUt9B/shaders /home/hampa/.hermes/profiles/mikuling/cache/scratch/feature-scan-ulimit1.hlUt9B/out/report.json -> rc 2; report 32 bytes before, 1024 bytes after (old contents replaced by partial JSON).
- After: bash -c 'trap "" XFSZ; ulimit -f 1; python3 -I -B tools/feature_scan.py "$1" --out "$2" >/dev/null' _ /home/hampa/.hermes/profiles/mikuling/cache/scratch/feature-scan-ulimit1.hlUt9B/shaders /home/hampa/.hermes/profiles/mikuling/cache/scratch/feature-scan-ulimit1.hlUt9B/out/report.json -> rc 2; report 32 bytes before and after, with no temp file remaining.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:59:24 +00:00
Author
Owner

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

Independent verification (Windows, Python 3.11.9): tool change is correct and Windows-safe — mkstemp in target dir + os.replace is atomic, failure leaves the existing report untouched (verified live: --out pointing at a directory -> exit 2, directory preserved, no stray temp). Two test problems though:

  1. The new FeatureScanAtomicOutputTests failure-injection tests are POSIX-only (import resource, signal.SIGXFSZ, preexec_fn) — under pytest on Windows: 2 failed / 14 passed.
  2. Worse, they are invisible to CI: the file's __main__ runner only collects module-level test_* functions, so python -I tests/test_feature_scan.py (what the windows-latest check job runs) passes vacuously without running the new class at all.

Suggest: skip POSIX-only failure injection on Windows AND make the tests collectible (module-level functions or pytest marker/unittest.main()). Minor nits: mkstemp files are mode 0600 on POSIX (no published_mode chmod like sibling tools); os.replace replaces a symlink itself rather than writing through it — behavior change vs old open(). AI-assisted verification.

**@⁠whoalin1** on GitHub · 2026-11-25 10:02 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1648#issuecomment-7159134833) Independent verification (Windows, Python 3.11.9): **tool change is correct and Windows-safe** — mkstemp in target dir + os.replace is atomic, failure leaves the existing report untouched (verified live: `--out` pointing at a directory -> exit 2, directory preserved, no stray temp). Two test problems though: 1. The new `FeatureScanAtomicOutputTests` failure-injection tests are POSIX-only (`import resource`, `signal.SIGXFSZ`, `preexec_fn`) — under pytest on Windows: 2 failed / 14 passed. 2. Worse, they are **invisible to CI**: the file's `__main__` runner only collects module-level `test_*` functions, so `python -I tests/test_feature_scan.py` (what the windows-latest check job runs) passes vacuously without running the new class at all. Suggest: skip POSIX-only failure injection on Windows AND make the tests collectible (module-level functions or pytest marker/`unittest.main()`). Minor nits: mkstemp files are mode 0600 on POSIX (no published_mode chmod like sibling tools); os.replace replaces a symlink itself rather than writing through it — behavior change vs old open(). 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/1648/head:github-bot/gh-1648'
git switch 'github-bot/gh-1648'

Merge

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