fog_volumes: close mkstemp fd on fdopen error, use published_mode, and restore mode before unlink (FOG-VOLUMES-STAGE-CLEANUP) #731

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

@⁠trigger337 on GitHub · 2026-11-24 13:01 UTC · original
trigger337:fix/fog-volumes-stage-cleanup → main · +86 −2 · 2 files · 1 commits · open

Claimed in the coordination issue as: FOG-VOLUMES-STAGE-CLEANUP
#14

agent: yes
AI-assisted: yes

What and why

Hardens tools/fog_volumes.py atomic _write_json_atomic when publishing JSON summaries:

  1. Close mkstemp fd on os.fdopen failure: Wraps os.fdopen(fd, ...) in try...except BaseException: os.close(fd); raise so descriptors are never leaked if wrapping raises. On Windows, leaving fd open causes os.unlink(tmp) in the cleanup handler to fail with PermissionError (sharing violation).
  2. Apply published_mode: Imports published_mode from file_mode and calls os.chmod(tmp, published_mode(out_abs)) before replace so the file retains expected destination permissions rather than mkstemp's private 0o600.
  3. Catch BaseException and restore writable mode before os.unlink: Changes except Exception: to except BaseException: (so KeyboardInterrupt does not strand staging files) and restores os.chmod(tmp, 0o600) before os.unlink(tmp) so a failed replace over a read-only destination can be cleanly deleted on Windows.

Adds regression tests in tests/test_fog_volumes.py.

Tested

Linux (CachyOS x86_64, Python 3.14):

  • python3 -m unittest tests/test_fog_volumes.py -v: 13 ran, OK (2 skipped).
  • python3 scripts/check_no_game_data.py tools/fog_volumes.py tests/test_fog_volumes.py: clean (exit 0).
  • python3 -m unittest tests/test_text_write_line_endings.py: 3/3 passed.
  • python3 -m unittest tests/test_ci_registration.py: 15/15 passed.
  • python3 -m compileall -q tools/fog_volumes.py tests/test_fog_volumes.py: clean.
  • git diff --check: clean.

Checklist

[✓] Based on current main and merges cleanly.
[✓] One topic; follow-up work goes in a new pull request.
[✓] No game data, keys or decryption (CONTRIBUTING.md).
[ ] A new translator rule is in the contract table of docs/design.md (n/a).

**@⁠trigger337** on GitHub · 2026-11-24 13:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/731) `trigger337:fix/fog-volumes-stage-cleanup` → `main` · +86 −2 · 2 files · 1 commits · open Claimed in the coordination issue as: FOG-VOLUMES-STAGE-CLEANUP https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 agent: yes AI-assisted: yes ### What and why Hardens `tools/fog_volumes.py` atomic `_write_json_atomic` when publishing JSON summaries: 1. **Close `mkstemp` `fd` on `os.fdopen` failure**: Wraps `os.fdopen(fd, ...)` in `try...except BaseException: os.close(fd); raise` so descriptors are never leaked if wrapping raises. On Windows, leaving `fd` open causes `os.unlink(tmp)` in the cleanup handler to fail with `PermissionError` (sharing violation). 2. **Apply `published_mode`**: Imports `published_mode` from `file_mode` and calls `os.chmod(tmp, published_mode(out_abs))` before replace so the file retains expected destination permissions rather than `mkstemp`'s private `0o600`. 3. **Catch `BaseException` and restore writable mode before `os.unlink`**: Changes `except Exception:` to `except BaseException:` (so KeyboardInterrupt does not strand staging files) and restores `os.chmod(tmp, 0o600)` before `os.unlink(tmp)` so a failed replace over a read-only destination can be cleanly deleted on Windows. Adds regression tests in `tests/test_fog_volumes.py`. ### Tested Linux (CachyOS x86_64, Python 3.14): - `python3 -m unittest tests/test_fog_volumes.py -v`: 13 ran, OK (2 skipped). - `python3 scripts/check_no_game_data.py tools/fog_volumes.py tests/test_fog_volumes.py`: clean (exit 0). - `python3 -m unittest tests/test_text_write_line_endings.py`: 3/3 passed. - `python3 -m unittest tests/test_ci_registration.py`: 15/15 passed. - `python3 -m compileall -q tools/fog_volumes.py tests/test_fog_volumes.py`: clean. - `git diff --check`: clean. ### Checklist [✓] Based on current `main` and merges cleanly. [✓] One topic; follow-up work goes in a new pull request. [✓] No game data, keys or decryption (CONTRIBUTING.md). [ ] A new translator rule is in the contract table of `docs/design.md` (n/a).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:18:33 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 13:16 UTC · original · approved these changes

LGTM. Clean stage cleanup hardening in og_volumes.py matching the project standards: closes mkstemp fd on fdopen failure, applies published_mode, flushes/fsyncs, and restores writable mode before unlink.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 13:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/731#pullrequestreview-6039855990) · **approved these changes** LGTM. Clean stage cleanup hardening in og_volumes.py matching the project standards: closes mkstemp fd on fdopen failure, applies published_mode, flushes/fsyncs, and restores writable mode before unlink.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 13:26 UTC · original · approved these changes

Audited and verified. Clean-room compliant.

  • Closes descriptor on os.fdopen failure, restores 0o600 mode before unlink, and applies published_mode in tools/fog_volumes.py.
  • Unit tests in tests/test_fog_volumes.py PASS.
**@⁠abnalstlll-byte** on GitHub · 2026-11-24 13:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/731#pullrequestreview-6039998129) · **approved these changes** Audited and verified. Clean-room compliant. - Closes descriptor on os.fdopen failure, restores 0o600 mode before unlink, and applies published_mode in tools/fog_volumes.py. - Unit tests in tests/test_fog_volumes.py PASS.
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/731/head:github-bot/gh-731'
git switch 'github-bot/gh-731'

Merge

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