[PR #570] [MERGED] calibrated_texture: atomic no-clobber export with MoveFileExW and cleanup (CALIBRATED-TEXTURE-NO-CLOBBER) #614

Closed
opened 2026-11-26 20:18:28 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/570
Author: @Managilvosh
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142

Base: main ← Head: fix-calibrated-texture-no-clobber


📝 Commits (1)

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

📊 Changes

2 files changed (+153 additions, -5 deletions)

View changed files

📝 tests/test_calibrated_texture_extra.py (+119 -0)
📝 tools/calibrated_texture.py (+34 -5)

📄 Description

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


🔄 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/570 **Author:** [@Managilvosh](https://github.com/Managilvosh) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `fix-calibrated-texture-no-clobber` --- ### 📝 Commits (1) - [`f8650dc`](https://github.com/ubiquitarianism/leonida/commit/f8650dc5aa9fb45ca7d360cdc6f22aecfbf7fdfd) calibrated_texture: atomic no-clobber export with MoveFileExW and cleanup (CALIBRATED-TEXTURE-NO-CLOBBER) ### 📊 Changes **2 files changed** (+153 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_calibrated_texture_extra.py` (+119 -0) 📝 `tools/calibrated_texture.py` (+34 -5) </details> ### 📄 Description 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:18:28 +01:00
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#614
No description provided.