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

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/733
Author: @trigger337
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: fix/timecycle-pairs-stage-cleanup


📝 Commits (1)

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

📊 Changes

2 files changed (+91 additions, -3 deletions)

View changed files

📝 tests/test_timecycle_pairs.py (+77 -0)
📝 tools/timecycle_pairs.py (+14 -3)

📄 Description

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


🔄 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/733 **Author:** [@trigger337](https://github.com/trigger337) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/timecycle-pairs-stage-cleanup` --- ### 📝 Commits (1) - [`095ecb0`](https://github.com/ubiquitarianism/leonida/commit/095ecb00842feb1c59f4e69843f4e4b0654f7682) timecycle_pairs: close mkstemp fd on fdopen error, use published_mode, and restore mode before unlink (TIMECYCLE-PAIRS-STAGE-CLEANUP) ### 📊 Changes **2 files changed** (+91 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_timecycle_pairs.py` (+77 -0) 📝 `tools/timecycle_pairs.py` (+14 -3) </details> ### 📄 Description 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). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#772
No description provided.