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

Merged
Managilvosh merged 1 commit from upstream-pr/570 into main 2026-11-25 19:47:31 +00:00
Managilvosh commented 2026-11-24 08:34:55 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Managilvosh on 2026-11-24T08:34:55Z.


Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7125764622

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).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5468776534 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/570). Originally posted by [Managilvosh](https://github.com/Managilvosh) on 2026-11-24T08:34:55Z. --- Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7125764622 ### 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).
PierreEbele commented 2026-11-25 00:41:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by PierreEbele on 2026-11-25T00:41:50Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7148781712 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/570#issuecomment-7148781712). Originally posted by [PierreEbele](https://github.com/PierreEbele) on 2026-11-25T00:41:50Z. --- 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
aslan2142 commented 2026-11-25 19:47:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-25T19:47:33Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7172771530 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/570#issuecomment-7172771530). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-25T19:47:33Z. --- 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).
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!570
No description provided.