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

Merged
trigger337 merged 1 commit from upstream-pr/740 into main 2026-11-28 11:01:20 +00:00
trigger337 commented 2026-11-24 13:15:02 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by trigger337 on 2026-11-24T13:15:02Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5472850353 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/740). Originally posted by [trigger337](https://github.com/trigger337) on 2026-11-24T13:15:02Z. --- 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 commented 2026-11-24 13:24:21 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-24T13:24:21Z.

Review: APPROVED


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.
<!-- greyfinch-sync:ubiquitarianism/leonida:review:6039966391 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/740#pullrequestreview-6039966391). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T13:24:21Z. **Review:** APPROVED --- 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 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T11:01:15Z.


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)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238272344 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/740#issuecomment-7238272344). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T11:01:15Z. --- 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 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!740
No description provided.