[PR #1648] feature_scan: write --out atomically and keep the existing report on failure #1662

Open
opened 2026-11-26 20:22:01 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1648
Author: @xel1uz
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/feature-scan-atomic-out


📝 Commits (1)

  • 362b854 feature_scan: write --out atomically and keep the old report on failure

📊 Changes

2 files changed (+84 additions, -2 deletions)

View changed files

📝 tests/test_feature_scan.py (+68 -0)
📝 tools/feature_scan.py (+16 -2)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1648 **Author:** [@xel1uz](https://github.com/xel1uz) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/feature-scan-atomic-out` --- ### 📝 Commits (1) - [`362b854`](https://github.com/ubiquitarianism/leonida/commit/362b854007f09b5f0c61ea9c4829cd9f7d3b26ce) feature_scan: write --out atomically and keep the old report on failure ### 📊 Changes **2 files changed** (+84 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_feature_scan.py` (+68 -0) 📝 `tools/feature_scan.py` (+16 -2) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1662
No description provided.