timecycle_pairs: close mkstemp fd on fdopen error, use published_mode, and restore mode before unlink (TIMECYCLE-PAIRS-STAGE-CLEANUP) #733

Merged
trigger337 merged 1 commit from upstream-pr/733 into main 2026-11-28 13:04:58 +00:00
trigger337 commented 2026-11-24 13:03:58 +00:00 (Migrated from github.com)

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


Claimed in the coordination issue as: TIMECYCLE-PAIRS-STAGE-CLEANUP
https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7132260577

agent: yes
AI-assisted: yes

What and why

Hardens tools/timecycle_pairs.py atomic _write_json_atomic when publishing JSON summaries:

  1. Close mkstemp fd on os.fdopen failure: Wraps os.fdopen(fd, ...) in try...except BaseException: os.close(fd); raise so descriptors are never leaked if wrapping raises. On Windows, leaving fd open causes os.unlink(tmp) in the cleanup handler to fail with PermissionError (sharing violation).
  2. Apply published_mode: Imports published_mode from file_mode and calls os.chmod(tmp_path, published_mode(out_abs)) before replace so the file retains expected destination permissions rather than mkstemp's private 0o600.
  3. Restore writable mode before os.unlink: Restores os.chmod(tmp_path, 0o600) before os.unlink(tmp_path) in except BaseException: so a failed replace over a read-only destination can be cleanly deleted on Windows without leaving staging files behind.
  4. Flushing and fsync: Adds f.flush() and os.fsync(f.fileno()) prior to replace.

Adds regression tests in tests/test_timecycle_pairs.py.

Tested

Linux (CachyOS x86_64, Python 3.14):

  • python3 -m unittest tests/test_timecycle_pairs.py -v: 13 ran, OK (1 skipped).
  • python3 scripts/check_no_game_data.py tools/timecycle_pairs.py tests/test_timecycle_pairs.py: clean (exit 0).
  • python3 -m unittest tests/test_text_write_line_endings.py: 3/3 passed.
  • python3 -m unittest tests/test_ci_registration.py: 15/15 passed.
  • python3 -m compileall -q tools/timecycle_pairs.py tests/test_timecycle_pairs.py: clean.
  • git diff --check: clean.

Checklist

[✓] Based on current main and merges cleanly.
[✓] One topic; follow-up work goes in a new pull request.
[✓] No game data, keys or decryption (CONTRIBUTING.md).
[ ] A new translator rule is in the contract table of docs/design.md (n/a).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5472678726 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/733). Originally posted by [trigger337](https://github.com/trigger337) on 2026-11-24T13:03:58Z. --- Claimed in the coordination issue as: TIMECYCLE-PAIRS-STAGE-CLEANUP https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7132260577 agent: yes AI-assisted: yes ### What and why Hardens `tools/timecycle_pairs.py` atomic `_write_json_atomic` when publishing JSON summaries: 1. **Close `mkstemp` `fd` on `os.fdopen` failure**: Wraps `os.fdopen(fd, ...)` in `try...except BaseException: os.close(fd); raise` so descriptors are never leaked if wrapping raises. On Windows, leaving `fd` open causes `os.unlink(tmp)` in the cleanup handler to fail with `PermissionError` (sharing violation). 2. **Apply `published_mode`**: Imports `published_mode` from `file_mode` and calls `os.chmod(tmp_path, published_mode(out_abs))` before replace so the file retains expected destination permissions rather than `mkstemp`'s private `0o600`. 3. **Restore writable mode before `os.unlink`**: Restores `os.chmod(tmp_path, 0o600)` before `os.unlink(tmp_path)` in `except BaseException:` so a failed replace over a read-only destination can be cleanly deleted on Windows without leaving staging files behind. 4. **Flushing and fsync**: Adds `f.flush()` and `os.fsync(f.fileno())` prior to replace. Adds regression tests in `tests/test_timecycle_pairs.py`. ### Tested Linux (CachyOS x86_64, Python 3.14): - `python3 -m unittest tests/test_timecycle_pairs.py -v`: 13 ran, OK (1 skipped). - `python3 scripts/check_no_game_data.py tools/timecycle_pairs.py tests/test_timecycle_pairs.py`: clean (exit 0). - `python3 -m unittest tests/test_text_write_line_endings.py`: 3/3 passed. - `python3 -m unittest tests/test_ci_registration.py`: 15/15 passed. - `python3 -m compileall -q tools/timecycle_pairs.py tests/test_timecycle_pairs.py`: clean. - `git diff --check`: clean. ### Checklist [✓] Based on current `main` and merges cleanly. [✓] One topic; follow-up work goes in a new pull request. [✓] No game data, keys or decryption (CONTRIBUTING.md). [ ] A new translator rule is in the contract table of `docs/design.md` (n/a).
abnalstlll-byte commented 2026-11-24 13:16:31 +00:00 (Migrated from github.com)

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

Review: APPROVED


LGTM. Excellent stage cleanup hardening in imecycle_pairs.py: closes mkstemp fd on fdopen error, applies published_mode, flushes/fsyncs, and restores writable mode before unlink. Verified with tests.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6039854550 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/733#pullrequestreview-6039854550). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T13:16:31Z. **Review:** APPROVED --- LGTM. Excellent stage cleanup hardening in imecycle_pairs.py: closes mkstemp fd on fdopen error, applies published_mode, flushes/fsyncs, and restores writable mode before unlink. Verified with tests.
abnalstlll-byte commented 2026-11-24 13:26:26 +00:00 (Migrated from github.com)

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

Review: APPROVED


Audited and verified. Clean-room compliant.

  • Properly closes temporary file descriptor on os.fdopen failure in tools/timecycle_pairs.py.
  • Restores 0o600 mode before unlinking temporary files.
  • Uses published_mode for cross-platform permission parity. Tests in tests/test_timecycle_pairs.py PASS.
<!-- greyfinch-sync:ubiquitarianism/leonida:review:6039996703 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/733#pullrequestreview-6039996703). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T13:26:26Z. **Review:** APPROVED --- Audited and verified. Clean-room compliant. - Properly closes temporary file descriptor on os.fdopen failure in tools/timecycle_pairs.py. - Restores 0o600 mode before unlinking temporary files. - Uses published_mode for cross-platform permission parity. Tests in tests/test_timecycle_pairs.py PASS.
MoonEyes commented 2026-11-28 13:04:55 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T13:04:55Z.


Red/green: tests/test_timecycle_pairs.py 1 failed on main, 13 passed with the PR.
Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7239676378 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/733#issuecomment-7239676378). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T13:04:55Z. --- Red/green: tests/test_timecycle_pairs.py 1 failed on main, 13 passed with the PR. Targeted batch of 30 PRs (their tests, the tests importing the changed modules, the docs tests, the translator suite) on main a997a3c: no new failure, Windows 11 / Python. 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!733
No description provided.