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

Merged
leonida/mandy
xel1uz merged 1 commit from fix/feature-scan-atomic-out into main 2026-11-29 18:28:18 +00:00

Pull request successfully merged and closed

xel1uz commented 2026-11-25 07:00:08 +00:00

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.
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.
whoalin1 commented 2026-11-25 10:02:10 +00:00

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.

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.
MoonEyes commented 2026-11-28 03:04:01 +00:00

Hi @xel1uz, thanks for this pull request.

CONTRIBUTING.md asks contributors to claim their work in the coordination issue ("Coordinating who works on what", currently #1608) before opening a pull request: post claiming: <ID> there, or a one-line description if the item is not listed yet, and put the ID in the pull request. I could not find a claim from you for this one.

The claim matters because "it is claimed in the coordination issue by the pull request's author" is the first item on the maintainers' checklist for a mergeable pull request, and because it lets others see that the work is taken before they write it a second time.

Could you post the claim in #1608 and put its ID (or a link to it) on the pull request's "Claimed in the coordination issue as:" line? If you already claimed it in another form, a link to your comment is enough.

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

Hi @xel1uz, thanks for this pull request. CONTRIBUTING.md asks contributors to claim their work in the coordination issue ("Coordinating who works on what", currently #1608) before opening a pull request: post `claiming: <ID>` there, or a one-line description if the item is not listed yet, and put the ID in the pull request. I could not find a claim from you for this one. The claim matters because "it is claimed in the coordination issue by the pull request's author" is the first item on the maintainers' checklist for a mergeable pull request, and because it lets others see that the work is taken before they write it a second time. Could you post the claim in #1608 and put its ID (or a link to it) on the pull request's "Claimed in the coordination issue as:" line? If you already claimed it in another form, a link to your comment is enough. AI-assisted (Claude Code, Claude Opus 5.5).
TheGeogeo commented 2026-11-29 00:16:54 +00:00

Tested 362b85400 merged (no commit) with main 829530dbd: Python 3.12.4, Windows 11 10.0.26300, CPU only. check_no_game_data.py is clean.

Blocker: the new tests do not run the way the file is run, and fail on Windows when they do.

  • python -I tests/test_feature_scan.py (the file's own __main__ runner, line 633) runs only the existing checks. The new FeatureScanAtomicOutputTests class is never called, so the file passes with and without the fix.
  • Under python -m unittest discover -s tests -p test_feature_scan.py, 2 of its 3 tests error on Windows: ValueError: preexec_fn is not supported on Windows platforms (RLIMIT_FSIZE through resource).

Hooking the class into the file's runner and skipping the size-limit cases where resource is missing would cover both points.

AI-assisted (Claude Code).

Tested `362b85400` merged (no commit) with `main` `829530dbd`: Python 3.12.4, Windows 11 10.0.26300, CPU only. `check_no_game_data.py` is clean. **Blocker: the new tests do not run the way the file is run, and fail on Windows when they do.** - `python -I tests/test_feature_scan.py` (the file's own `__main__` runner, line 633) runs only the existing checks. The new `FeatureScanAtomicOutputTests` class is never called, so the file passes with and without the fix. - Under `python -m unittest discover -s tests -p test_feature_scan.py`, 2 of its 3 tests error on Windows: `ValueError: preexec_fn is not supported on Windows platforms` (`RLIMIT_FSIZE` through `resource`). Hooking the class into the file's runner and skipping the size-limit cases where `resource` is missing would cover both points. AI-assisted (Claude Code).
zmasi commented 2026-11-29 00:22:23 +00:00

Native Windows 11 Verification (362b854007)

Diff-audited tools/feature_scan.py (+16/-2) and tests/test_feature_scan.py (+68/-0) and verified merged onto origin/main (48bae6cad) on Windows 11 (10.0.26200, NTFS, Python 3.12.10).

Live NTFS Read-Only Probe

  • Marked an existing report.json read-only (stat.S_IREAD) on NTFS and ran python -I tools/feature_scan.py <shaders> --out <report.json>: exited 2 (ERROR: cannot write ...: Access is denied), preserved report.json byte-for-byte, and unlinked the temporary file (0 leftover temp files).

Three Issues Found in tests/test_feature_scan.py & tools/feature_scan.py

  1. FeatureScanAtomicOutputTests never runs under python -I tests/test_feature_scan.py (CI's stdlib test loop):
    • tests/test_feature_scan.py's __main__ block only collects module-level def test_* functions via run_tests(...), so class FeatureScanAtomicOutputTests(unittest.TestCase): is silently skipped (0/3 tests executed) when run as a script!
  2. Windows ValueError: preexec_fn is not supported on Windows platforms under unittest:
    • Running python -m unittest tests/test_feature_scan.py on Windows 11 fails 2/3 tests (FAILED (errors=2)):
      ERROR: test_failed_write_keeps_existing_report (...FeatureScanAtomicOutputTests...)
      ...
      ValueError: preexec_fn is not supported on Windows platforms
      
      (resource.RLIMIT_FSIZE and signal.SIGXFSZ inside fail_file_writes are also Unix-only.) Converting the 3 new tests to module-level def test_* functions (or running unittest.TextTestRunner from __main__) and triggering write failure portably — e.g. mocking json.dump to raise OSError mid-write in-process, or pointing --out at a read-only destination — makes all 3 tests run and pass on both Linux and Windows.
  3. Double os.close(fd) in tools/feature_scan.py when os.replace raises:
    • Once with os.fdopen(fd, "w", ...) as f: wraps fd, exiting the with block closes fd. When os.replace(temporary, a.out) subsequently raises PermissionError (as on a read-only NTFS target), except BaseException: calls os.close(fd) a second time on an already-closed integer FD. Clearing a local fd_owned = False flag once os.fdopen succeeds avoids closing a potentially reused descriptor.

AI-assisted.

### Native Windows 11 Verification (`362b854007`) Diff-audited `tools/feature_scan.py` (`+16/-2`) and `tests/test_feature_scan.py` (`+68/-0`) and verified merged onto `origin/main` (`48bae6cad`) on Windows 11 (`10.0.26200`, NTFS, Python `3.12.10`). #### Live NTFS Read-Only Probe - Marked an existing `report.json` read-only (`stat.S_IREAD`) on NTFS and ran `python -I tools/feature_scan.py <shaders> --out <report.json>`: exited `2` (`ERROR: cannot write ...: Access is denied`), preserved `report.json` byte-for-byte, and unlinked the temporary file (`0` leftover temp files). #### Three Issues Found in `tests/test_feature_scan.py` & `tools/feature_scan.py` 1. **`FeatureScanAtomicOutputTests` never runs under `python -I tests/test_feature_scan.py` (CI's stdlib test loop)**: - `tests/test_feature_scan.py`'s `__main__` block only collects module-level `def test_*` functions via `run_tests(...)`, so `class FeatureScanAtomicOutputTests(unittest.TestCase):` is silently skipped (`0/3` tests executed) when run as a script! 2. **Windows `ValueError: preexec_fn is not supported on Windows platforms` under `unittest`**: - Running `python -m unittest tests/test_feature_scan.py` on Windows 11 fails `2/3` tests (`FAILED (errors=2)`): ```text ERROR: test_failed_write_keeps_existing_report (...FeatureScanAtomicOutputTests...) ... ValueError: preexec_fn is not supported on Windows platforms ``` (`resource.RLIMIT_FSIZE` and `signal.SIGXFSZ` inside `fail_file_writes` are also Unix-only.) Converting the 3 new tests to module-level `def test_*` functions (or running `unittest.TextTestRunner` from `__main__`) and triggering write failure portably — e.g. mocking `json.dump` to raise `OSError` mid-write in-process, or pointing `--out` at a read-only destination — makes all 3 tests run and pass on both Linux and Windows. 3. **Double `os.close(fd)` in `tools/feature_scan.py` when `os.replace` raises**: - Once `with os.fdopen(fd, "w", ...) as f:` wraps `fd`, exiting the `with` block closes `fd`. When `os.replace(temporary, a.out)` subsequently raises `PermissionError` (as on a read-only NTFS target), `except BaseException:` calls `os.close(fd)` a second time on an already-closed integer FD. Clearing a local `fd_owned = False` flag once `os.fdopen` succeeds avoids closing a potentially reused descriptor. AI-assisted.
TheGeogeo commented 2026-11-29 11:51:49 +00:00

Before a merge: the blocker in my comment above still stands (the new test class never runs from the file's own runner, and under unittest 2 of its 3 tests error on Windows with preexec_fn). Your claim in the coordination issue #2570 is missing too.

AI-assisted (Claude Code).

Before a merge: the blocker in my comment above still stands (the new test class never runs from the file's own runner, and under unittest 2 of its 3 tests error on Windows with `preexec_fn`). Your claim in the coordination issue #2570 is missing too. AI-assisted (Claude Code).
OreoFR commented 2026-11-29 17:11:17 +00:00

Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, main 4364a4fa6, PR head 362b85400 : red 1 failure / green 16 OK

  • Merge: clean (git merge-tree). Reviews: none. No game data in the diff. Skipped on the merge: none.
  • (MEASURED n=3) each red failure reruns alone 3/3.
  • main changed tools/feature_scan.py (2 lines) since the PR base; the merge is clean and the test file is unchanged on main.
  • Red (pytest): 16 tests, 1 failure: FeatureScanAtomicOutputTests::test_failed_write_keeps_existing_report.
  • Green: 16/16 OK. Neighbours (test_feature_scan_* x5, test_inner_coverage_metadata, test_json_bom_inputs): 21 passed on main and merge.
  • Diff, minor: mkstemp creates the file as 0600, so the report loses its usual 0644 mode (#1668 keeps the mode); if os.replace fails, the except calls os.close(fd) on an fd already closed by the with (theoretical double close).

Verdict: READY (two nits above)

AI-assisted: Claude Code

Verified by OreoFR on macOS 27.2.1 (26B131), Apple M5, APFS (case-insensitive), Python 3.12.16, main 4364a4fa6, PR head 362b85400 : red 1 failure / green 16 OK - Merge: clean (`git merge-tree`). Reviews: none. No game data in the diff. Skipped on the merge: none. - (MEASURED n=3) each red failure reruns alone 3/3. - main changed `tools/feature_scan.py` (2 lines) since the PR base; the merge is clean and the test file is unchanged on main. - **Red** (pytest): 16 tests, 1 failure: `FeatureScanAtomicOutputTests::test_failed_write_keeps_existing_report`. - **Green**: 16/16 OK. Neighbours (`test_feature_scan_*` x5, `test_inner_coverage_metadata`, `test_json_bom_inputs`): 21 passed on main and merge. - Diff, minor: `mkstemp` creates the file as 0600, so the report loses its usual 0644 mode (#1668 keeps the mode); if `os.replace` fails, the `except` calls `os.close(fd)` on an fd already closed by the `with` (theoretical double close). **Verdict: READY** (two nits above) AI-assisted: Claude Code
DadaCap commented 2026-11-29 18:28:21 +00:00

Thank you! Merged into main with a merge commit. It was verified ready on its current head (362b85400) by OreoFR's Mac agent (macOS, red/green on main + PR); see the verification comment above. (AI-assisted, posted for DadaCap.)

Thank you! Merged into `main` with a merge commit. It was verified ready on its current head (362b85400) by OreoFR's Mac agent (macOS, red/green on main + PR); see the verification comment above. (AI-assisted, posted for DadaCap.)
Sign in to join this conversation.
No description provided.