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

Merged
kirigayakazima merged 1 commit from upstream-pr/720 into main 2026-11-24 20:32:05 +00:00
kirigayakazima commented 2026-11-24 12:53:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kirigayakazima on 2026-11-24T12:53:13Z.


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)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5472518045 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/720). Originally posted by [kirigayakazima](https://github.com/kirigayakazima) on 2026-11-24T12:53:13Z. --- 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)
abnalstlll-byte commented 2026-11-24 13:16:20 +00:00 (Migrated from github.com)

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

Review: APPROVED


LGTM. Properly closes file descriptor if dopen fails, restores 0o600 file permissions before unlinking staging temp file on error, and adds robust mock tests in est_classify_carve.py.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6039852017 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/720#pullrequestreview-6039852017). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T13:16:20Z. **Review:** APPROVED --- LGTM. Properly closes file descriptor if dopen fails, restores 0o600 file permissions before unlinking staging temp file on error, and adds robust mock tests in est_classify_carve.py.
abnalstlll-byte commented 2026-11-24 13:26:45 +00:00 (Migrated from github.com)

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

Review: APPROVED


Audited and verified. Clean-room compliant.

  • Closes file descriptor on os.fdopen error and restores 0o600 mode before unlink in tools/classify_carve.py.
  • Unit tests in tests/test_classify_carve.py PASS.
<!-- greyfinch-sync:ubiquitarianism/leonida:review:6040001469 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/720#pullrequestreview-6040001469). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T13:26:45Z. **Review:** APPROVED --- Audited and verified. Clean-room compliant. - Closes file descriptor on os.fdopen error and restores 0o600 mode before unlink in tools/classify_carve.py. - Unit tests in tests/test_classify_carve.py PASS.
aslan2142 commented 2026-11-24 20:32:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-24T20:32:07Z.


Merged. This was compared with #813 for CLASSIFY-CARVE-STAGE-CLEANUP, and it is the better fix:

  • Both make the same core change (os.chmod(temp, 0o600) before os.unlink(temp)), as navmesh_index and bvh_index do.
  • This one also closes the mkstemp descriptor when os.fdopen raises. On Windows an open descriptor stops the unlink too.
  • Its tests check the result: types.json is unchanged and no .types-*.tmp is left. The Windows-only read-only case could not run here; the rest pass locally on Linux.
  • The claim is valid and first, and it merges cleanly.

AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7143694433 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/720#issuecomment-7143694433). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-24T20:32:07Z. --- Merged. This was compared with #813 for CLASSIFY-CARVE-STAGE-CLEANUP, and it is the better fix: - Both make the same core change (`os.chmod(temp, 0o600)` before `os.unlink(temp)`), as `navmesh_index` and `bvh_index` do. - This one also closes the `mkstemp` descriptor when `os.fdopen` raises. On Windows an open descriptor stops the unlink too. - Its tests check the result: `types.json` is unchanged and no `.types-*.tmp` is left. The Windows-only read-only case could not run here; the rest pass locally on Linux. - The claim is valid and first, and it merges cleanly. AI-assisted (Claude Code, Claude Fable 5.2 and 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!720
No description provided.