bink_index: close mkstemp fd on fdopen error, restore mode before unlink, and publish mode on Windows (BINK-INDEX-STAGE-CLEANUP) #740

Merged
leonida/mandy
trigger337 merged 1 commit from fix/bink-index-stage-cleanup into main 2026-11-28 11:01:20 +00:00

Pull request successfully merged and closed

trigger337 commented 2026-11-24 13:15:02 +00:00

In tools/bink_index.py:

  1. _temp_stream: if os.fdopen(fd, "wb") raises an exception, fd was leaked. If an error occurred while streaming bytes, unlinking could fail if file mode was touched. Added try...except BaseException: os.close(fd); raise and guaranteed write-mode restoration (os.chmod(..., 0o600)) before os.unlink in both exception handling and cleanup callback.
  2. _publish_text: skipped os.chmod(staged.name, published_mode(destination)) on Windows (if os.name != "nt":) because unlinking a read-only temp file in finally fails on Windows with PermissionError. By ensuring os.chmod(staged.name, 0o600) before os.unlink in finally, Windows now safely inherits published_mode(destination), and temporary staging files are never leaked on failed writes/replacements.
  3. Added regression tests covering fd closure on fdopen failure, cleanup callback behavior, read-only temp file removal with strict Windows unlinking simulation, and Windows published mode application.

agent: yes
AI-assisted: yes

In `tools/bink_index.py`: 1. `_temp_stream`: if `os.fdopen(fd, "wb")` raises an exception, `fd` was leaked. If an error occurred while streaming bytes, unlinking could fail if file mode was touched. Added `try...except BaseException: os.close(fd); raise` and guaranteed write-mode restoration (`os.chmod(..., 0o600)`) before `os.unlink` in both exception handling and cleanup callback. 2. `_publish_text`: skipped `os.chmod(staged.name, published_mode(destination))` on Windows (`if os.name != "nt":`) because unlinking a read-only temp file in `finally` fails on Windows with PermissionError. By ensuring `os.chmod(staged.name, 0o600)` before `os.unlink` in `finally`, Windows now safely inherits `published_mode(destination)`, and temporary staging files are never leaked on failed writes/replacements. 3. Added regression tests covering fd closure on fdopen failure, cleanup callback behavior, read-only temp file removal with strict Windows unlinking simulation, and Windows published mode application. agent: yes AI-assisted: yes
abnalstlll-byte approved these changes 2026-11-24 13:24:21 +00:00
abnalstlll-byte left a comment

Audited and verified. Clean-room compliant.

  • Properly closes temporary file descriptor on os.fdopen failure in tools/bink_index.py.
  • Restores 0o600 file mode before unlinking to prevent Windows PermissionErrors on read-only files.
  • Applies published_mode across all platforms, with regression coverage in tests/test_bink_index.py.
Audited and verified. Clean-room compliant. - Properly closes temporary file descriptor on os.fdopen failure in tools/bink_index.py. - Restores 0o600 file mode before unlinking to prevent Windows PermissionErrors on read-only files. - Applies published_mode across all platforms, with regression coverage in tests/test_bink_index.py.
MoonEyes commented 2026-11-28 11:01:15 +00:00

Red/green: the PR's tests fail on main and pass with this PR (Linux, Python 3.13.18).
Targeted batch of 23 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13.
AI-assisted (Claude Code, Claude Opus 5.5)

Red/green: the PR's tests fail on main and pass with this PR (Linux, Python 3.13.18). Targeted batch of 23 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No description provided.