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

Manually merged
github merged 1 commit from gh-pr/720/head into gh-pr/720/base 2026-11-26 09:17:54 +00:00
Owner

@⁠kirigayakazima on GitHub · 2026-11-24 12:53 UTC · original
kirigayakazima:fix/classify-carve-stage-cleanup → main · +79 −1 · 2 files · 1 commits · merged 2026-11-24 20:32 UTC

Claimed in the coordination issue as: CLASSIFY-CARVE-STAGE-CLEANUP #14

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)
**@⁠kirigayakazima** on GitHub · 2026-11-24 12:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/720) `kirigayakazima:fix/classify-carve-stage-cleanup` → `main` · +79 −1 · 2 files · 1 commits · merged 2026-11-24 20:32 UTC Claimed in the coordination issue as: CLASSIFY-CARVE-STAGE-CLEANUP https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 ### 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:17:54 +00:00
github manually merged commit 8d296d8158 into gh-pr/720/base 2026-11-26 09:17:54 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 13:16 UTC · original · approved these changes

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** on GitHub · 2026-11-24 13:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/720#pullrequestreview-6039852017) · **approved these changes** 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.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 13:26 UTC · original · approved these changes

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.
**@⁠abnalstlll-byte** on GitHub · 2026-11-24 13:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/720#pullrequestreview-6040001469) · **approved these changes** 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.
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-24 20:32 UTC · original

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

**@⁠aslan2142** on GitHub · 2026-11-24 20:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/720#issuecomment-7143694433) 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).
This discussion has been locked. Commenting is limited to contributors.
No description provided.