[PR #720] [MERGED] classify_carve: close mkstemp fd on fdopen error and restore mode before unlink (CLASSIFY-CARVE-STAGE-CLEANUP) #759

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

📋 Pull Request Information

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

Base: main ← Head: fix/classify-carve-stage-cleanup


📝 Commits (1)

  • 8d296d8 classify_carve: close mkstemp fd on fdopen error and restore mode before unlink

📊 Changes

2 files changed (+79 additions, -1 deletions)

View changed files

📝 tests/test_classify_carve.py (+72 -0)
📝 tools/classify_carve.py (+7 -1)

📄 Description

Claimed in the coordination issue as: CLASSIFY-CARVE-STAGE-CLEANUP https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7132017328

What and why

Hardens tools/classify_carve.py when publishing types.json fails:

  1. Close mkstemp fd on os.fdopen failure: Wraps os.fdopen(fd, ...) so if wrapping the descriptor raises, os.close(fd) runs before re-raising (matching tools/world_grid_atlas.py). On Windows, leaving fd open also causes os.unlink(temp) in except BaseException: to fail with PermissionError: [WinError 32] and leak .types-*.tmp.
  2. Restore writable mode before os.unlink(temp): On Windows, os.chmod(temp, published_mode(dest)) copies 0o444 onto temp before os.replace(temp, dest); when os.replace fails over a read-only types.json, os.unlink(temp) hits PermissionError and leaves .types-*.tmp behind. Calling os.chmod(temp, 0o600) before os.unlink(temp) lets the cleanup remove temp.

Adds 3 synthetic regression tests in tests/test_classify_carve.py.

Tested

Windows 11 x64, Python 3.14.7, CPU only, synthetic index JSON only (no game files). Commit 8d296d81584d028ce317cb2f332bb8f6f63089cc.

  • python -I tests/test_classify_carve.py -v:
    • against main (21625bca) with the three new tests: all 3 fail (leaked .types-*.tmp and unclosed fd in fdopen error path);
    • with the fix: 33 tests, OK (0 skipped).
  • python -I tests/test_ci_registration.py: 15 tests, OK.
  • python -I tests/test_text_write_line_endings.py: 3 tests, OK.
  • python -I scripts/check_no_game_data.py tools/classify_carve.py tests/test_classify_carve.py, python -m compileall -q tools tests, git diff --check: clean (exit 0).

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🔄 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/720 **Author:** [@kirigayakazima](https://github.com/kirigayakazima) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `fix/classify-carve-stage-cleanup` --- ### 📝 Commits (1) - [`8d296d8`](https://github.com/ubiquitarianism/leonida/commit/8d296d81584d028ce317cb2f332bb8f6f63089cc) classify_carve: close mkstemp fd on fdopen error and restore mode before unlink ### 📊 Changes **2 files changed** (+79 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_classify_carve.py` (+72 -0) 📝 `tools/classify_carve.py` (+7 -1) </details> ### 📄 Description Claimed in the coordination issue as: CLASSIFY-CARVE-STAGE-CLEANUP https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7132017328 ### What and why Hardens `tools/classify_carve.py` when publishing `types.json` fails: 1. **Close `mkstemp` `fd` on `os.fdopen` failure**: Wraps `os.fdopen(fd, ...)` so if wrapping the descriptor raises, `os.close(fd)` runs before re-raising (matching `tools/world_grid_atlas.py`). On Windows, leaving `fd` open also causes `os.unlink(temp)` in `except BaseException:` to fail with `PermissionError: [WinError 32]` and leak `.types-*.tmp`. 2. **Restore writable mode before `os.unlink(temp)`**: On Windows, `os.chmod(temp, published_mode(dest))` copies `0o444` onto `temp` before `os.replace(temp, dest)`; when `os.replace` fails over a read-only `types.json`, `os.unlink(temp)` hits `PermissionError` and leaves `.types-*.tmp` behind. Calling `os.chmod(temp, 0o600)` before `os.unlink(temp)` lets the cleanup remove `temp`. Adds 3 synthetic regression tests in `tests/test_classify_carve.py`. ### Tested Windows 11 x64, Python 3.14.7, CPU only, synthetic index JSON only (no game files). Commit `8d296d81584d028ce317cb2f332bb8f6f63089cc`. - `python -I tests/test_classify_carve.py -v`: - against `main` (`21625bca`) with the three new tests: all 3 fail (leaked `.types-*.tmp` and unclosed `fd` in `fdopen` error path); - with the fix: **33 tests, OK** (0 skipped). - `python -I tests/test_ci_registration.py`: **15 tests, OK**. - `python -I tests/test_text_write_line_endings.py`: **3 tests, OK**. - `python -I scripts/check_no_game_data.py tools/classify_carve.py tests/test_classify_carve.py`, `python -m compileall -q tools tests`, `git diff --check`: clean (exit `0`). GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) --- <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:57 +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#759
No description provided.