calibrated_texture: atomic no-clobber export with MoveFileExW and cleanup (CALIBRATED-TEXTURE-NO-CLOBBER) #570

Manually merged
github merged 1 commit from gh-pr/570/head into gh-pr/570/base 2026-11-26 09:09:07 +00:00
Owner

@⁠Managilvosh on GitHub · 2026-11-24 08:34 UTC · original
Managilvosh:fix-calibrated-texture-no-clobber → main · +153 −5 · 2 files · 1 commits · merged 2026-11-25 19:47 UTC

Claimed in the coordination issue as: #14

Summary

Resolves a Windows-specific failure in tools/calibrated_texture.py:export_file() where unprivileged executions failed with PermissionError: [WinError 5] Access is denied during atomic texture export (force=False) because os.link(tmp, target) requires hardlink creation rights or specific local filesystem configurations not available to standard user accounts.

Technical Implementation

  • Atomic No-Clobber on Windows (force=False): Introduced _rename_no_clobber() wrapping os.rename(src, dst). On Windows, os.rename() calls Win32 MoveFileExW(src, dst, 0) without MOVEFILE_REPLACE_EXISTING, which is an atomic filesystem operation that raises FileExistsError ([WinError 183]) if dst exists while permitting standard unprivileged rename operations.
  • Atomic No-Clobber on POSIX: Retains os.link(src, dst) followed by tmp.unlink() in finally:. If os.link raises any error (FileExistsError or PermissionError), it fails safely by re-raising immediately without non-atomic existence checks or fallback replacement, eliminating any potential TOCTOU race.
  • Overwrite Exports (force=True): Upgraded replacement from bare os.replace() to replace_retry.replace(), making overwrite exports resilient against transient background file locks.
  • Transient Lock Recovery: Integrated replace_retry retry loop with exponential backoff (FIRST_DELAY = 0.01s, MAX_DELAY = 0.25s, RETRY_SECONDS = 5.0s) for _rename_no_clobber(), retrying transient Windows error codes (WinError 5 and 32) while propagating non-transient errors immediately.
  • Resource Lifecycle: Encapsulated file writes in with f: to guarantee handle closure before rename or unlinking. Temporary files are guaranteed to be cleaned up via finally: tmp.unlink(missing_ok=True).

Tested

  • Environment: Windows 11 (x64), Python 3.12.10, standard library tests (no game data, no GPU needed).
  • python -m compileall -q tools/calibrated_texture.py tests/test_calibrated_texture_extra.py (PASS, exit code 0)
  • python -I scripts/check_no_game_data.py tools/calibrated_texture.py tests/test_calibrated_texture_extra.py (PASS, 0 violations)
  • python -I tests/test_calibrated_texture_extra.py (30/30 tests PASS, exit code 0; previously failed 2 tests with WinError 5)
  • python -I tests/test_calibrated_texture.py (2 tests, PASS, 1 skipped)
  • python -I tests/test_ttpf_assets.py (31 tests, PASS, 3 skipped)
  • python -I tests/test_ttpf_mips.py (93 tests, PASS)
  • Added regression tests covering:
    • Windows atomic no-clobber behavior and destination preservation.
    • POSIX link PermissionError safe failure and tempfile cleanup.
    • Concurrent destination creation race rejection (Windows & POSIX).
    • Transient sharing violation retry (WinError 32) and immediate failure for non-transient errors (WinError 1314).
    • Temporary-file cleanup across all failure paths.

AI-assisted: Yes (Google DeepMind Antigravity).

**@⁠Managilvosh** on GitHub · 2026-11-24 08:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/570) `Managilvosh:fix-calibrated-texture-no-clobber` → `main` · +153 −5 · 2 files · 1 commits · merged 2026-11-25 19:47 UTC Claimed in the coordination issue as: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 ### Summary Resolves a Windows-specific failure in `tools/calibrated_texture.py:export_file()` where unprivileged executions failed with `PermissionError: [WinError 5] Access is denied` during atomic texture export (`force=False`) because `os.link(tmp, target)` requires hardlink creation rights or specific local filesystem configurations not available to standard user accounts. ### Technical Implementation - **Atomic No-Clobber on Windows (`force=False`)**: Introduced `_rename_no_clobber()` wrapping `os.rename(src, dst)`. On Windows, `os.rename()` calls Win32 `MoveFileExW(src, dst, 0)` without `MOVEFILE_REPLACE_EXISTING`, which is an atomic filesystem operation that raises `FileExistsError` ([WinError 183]) if `dst` exists while permitting standard unprivileged rename operations. - **Atomic No-Clobber on POSIX**: Retains `os.link(src, dst)` followed by `tmp.unlink()` in `finally:`. If `os.link` raises any error (`FileExistsError` or `PermissionError`), it fails safely by re-raising immediately without non-atomic existence checks or fallback replacement, eliminating any potential TOCTOU race. - **Overwrite Exports (`force=True`)**: Upgraded replacement from bare `os.replace()` to `replace_retry.replace()`, making overwrite exports resilient against transient background file locks. - **Transient Lock Recovery**: Integrated `replace_retry` retry loop with exponential backoff (`FIRST_DELAY = 0.01s`, `MAX_DELAY = 0.25s`, `RETRY_SECONDS = 5.0s`) for `_rename_no_clobber()`, retrying transient Windows error codes (WinError 5 and 32) while propagating non-transient errors immediately. - **Resource Lifecycle**: Encapsulated file writes in `with f:` to guarantee handle closure before rename or unlinking. Temporary files are guaranteed to be cleaned up via `finally: tmp.unlink(missing_ok=True)`. ### Tested - Environment: Windows 11 (x64), Python 3.12.10, standard library tests (no game data, no GPU needed). - `python -m compileall -q tools/calibrated_texture.py tests/test_calibrated_texture_extra.py` (PASS, exit code 0) - `python -I scripts/check_no_game_data.py tools/calibrated_texture.py tests/test_calibrated_texture_extra.py` (PASS, 0 violations) - `python -I tests/test_calibrated_texture_extra.py` (30/30 tests PASS, exit code 0; previously failed 2 tests with WinError 5) - `python -I tests/test_calibrated_texture.py` (2 tests, PASS, 1 skipped) - `python -I tests/test_ttpf_assets.py` (31 tests, PASS, 3 skipped) - `python -I tests/test_ttpf_mips.py` (93 tests, PASS) - Added regression tests covering: - Windows atomic no-clobber behavior and destination preservation. - POSIX link PermissionError safe failure and tempfile cleanup. - Concurrent destination creation race rejection (Windows & POSIX). - Transient sharing violation retry (WinError 32) and immediate failure for non-transient errors (WinError 1314). - Temporary-file cleanup across all failure paths. AI-assisted: Yes (Google DeepMind Antigravity).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:09:06 +00:00
github manually merged commit f8650dc5aa into gh-pr/570/base 2026-11-26 09:09:07 +00:00
Author
Owner

@⁠PierreEbele on GitHub · 2026-11-25 00:41 UTC · original

Reviewed f8650dc5: ran tests/test_calibrated_texture_extra.py (30 OK) and tests/test_calibrated_texture.py in the sandbox
on this branch, and the same test file on main - 4 of the 5 new tests fail there, so they pin the fix (the concurrent-
destination one also passes on main, as os.link already had that property on POSIX). Ran a probe on both trees: the
existing target survives reject byte-for-byte, every failure path cleans the temp file, the TOCTOU race is rejected
through real os.link and a simulated no-clobber Windows rename, the retry is bounded by seconds, non-transient errors
raise on attempt one. No findings; the Windows os.rename contract rests on CPython's docs (nothing here runs Windows).
AI-assisted review.

Recommendation: Merge

**@⁠PierreEbele** on GitHub · 2026-11-25 00:41 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/570#issuecomment-7148781712) Reviewed f8650dc5: ran tests/test_calibrated_texture_extra.py (30 OK) and tests/test_calibrated_texture.py in the sandbox on this branch, and the same test file on main - 4 of the 5 new tests fail there, so they pin the fix (the concurrent- destination one also passes on main, as os.link already had that property on POSIX). Ran a probe on both trees: the existing target survives reject byte-for-byte, every failure path cleans the temp file, the TOCTOU race is rejected through real os.link and a simulated no-clobber Windows rename, the retry is bounded by `seconds`, non-transient errors raise on attempt one. No findings; the Windows os.rename contract rests on CPython's docs (nothing here runs Windows). AI-assisted review. **Recommendation:** Merge
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 19:47 UTC · original

Merged, thanks! The new test fails on main and passes with this.

  • Checked on main c606788bc + f8650dc5a: the semantic suite passes (491 files, 6,312 tests, 0 files failed); test_calibrated_texture_extra pass; the 15 docs and writer tests pass; check_no_game_data.py is clean on the 2 changed files.

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

**@⁠aslan2142** on GitHub · 2026-11-25 19:47 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/570#issuecomment-7172771530) Merged, thanks! The new test fails on `main` and passes with this. - **Checked on `main` `c606788bc` + `f8650dc5a`:** the semantic suite passes (491 files, 6,312 tests, 0 files failed); `test_calibrated_texture_extra` pass; the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 2 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.