Skip to content

Tools: clean read-only staging temps after a failed report replace - #4516

Open
bobcatchris wants to merge 3 commits into
ItsNotRexed:mainfrom
bobcatchris:fix/readonly-temp-cleanup
Open

bobcatchris wants to merge 3 commits into
ItsNotRexed:mainfrom
bobcatchris:fix/readonly-temp-cleanup

Conversation

@bobcatchris

@bobcatchris bobcatchris commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claimed on the coordination board: #4421 (comment)

bvh_index._publish(), navmesh_index._publish(), rbf0_tree.write_json() and awc_manifest.write_manifest() stage a report/file in the destination folder and chmod it to published_mode(dest) before os.replace(). When the destination is read-only on Windows, the replace fails and the except/finally path tries to unlink a temp file that now has the same read-only mode, so the temp is stranded beside the report. This is the same failure tupacfan04 found in #4344; this PR applies the accepted fix to seven staged writers, through one discard_staged() helper in tools/file_mode.py.

The failure path now restores mode 0600 before unlink. Each tool gets one synthetic regression that injects an os.replace failure, checks that the old bytes survive, checks that the staging file is made writable before cleanup, and checks that no .*.tmp file remains.

Scope: awc_opus.write_file() has the same shape but is deliberately excluded because open PR #4399 changes that function (a separate heads-up is on that PR). Open PRs #4431/#3107/#3143/#2930/#4457 and the current BUILD-454 claim touch CLI/preflight paths in these files, not the write helpers.

Verification (Windows 11, CPython 3.12.0, CPU only, synthetic fixtures, no game files):

  • python -I tests/test_bvh_index.py: 34 tests, 33 pass / 1 skip.
  • python -I tests/test_navmesh_index.py: 28 tests, 27 pass / 1 skip.
  • python -I tests/test_rbf0_tree.py: 34 tests, 33 pass / 1 skip.
  • python -I tests/test_awc_manifest.py: 19/19 pass.
  • Negative control: the same four test files against untouched origin/main fail exactly the four new regressions.
  • compileall, git diff --check and scripts/check_no_game_data.py on the changed files are clean.

AI-assisted.

Update at 852f41ae (rebased onto 7b7d73f8)

Three more writers had the same shape and are now covered: rtti_scan.write_report(), shader_link_check.write_report() and classify_carve (whose publish step is now the named publish_report(), so it can be tested without a carve folder).

  • shader_link_check was worse than a leftover: its finally did temporary.unlink(missing_ok=True) on a file that had just been given the destination's read-only mode, so the cleanup raised and replaced the real os.replace error.
  • rpf9_names.publish_report() has the same shape but stages inside a TemporaryDirectory, whose cleanup already clears read-only bits: I wrote the regression, it passes against main, so there is no bug there and the module is untouched. That control is the reason the other three are in this PR and this one is not.

Verification at 852f41ae (Windows 11, CPython 3.12.0, CPU only, synthetic fixtures): test_rtti_scan + test_shader_link_check + test_classify_carve + the four earlier files = 238 passed, 4 skipped. Negative control: the three new regressions against main's tool files fail (the leftover temp is listed beside the report; classify_carve.publish_report does not exist there yet), while the rpf9_names one passes. compileall, git diff --check and check_no_game_data on the changed files are clean.

AI-assisted.


Current state (head 500b0ff12, on main 1d7350424)

Three commits, all of the fix restored:

500b0ff12 bvh_index, navmesh_index, rbf0_tree: unlink a denied-chmod staging temp
198598b0b tests: put PublishTests before the __main__ guard
129a93aeb Also clean the staging temp in three more report writers

An earlier head (e5b3b0a) was a transient state from my own rebase and did not contain the three-tool commit; @Oliwettos measured it and was right. Main at 1d7350424 still has the combined with suppress(OSError): block in tools/bvh_index.py:403, tools/navmesh_index.py and tools/rbf0_tree.py, so this is not superseded — his before/after on 4c4cb16bb^1 shows it is a regression against main, not a tidiness change.

Suites at this head (Windows 11, CPython 3.12, CPU only, synthetic): test_bvh_index 41 (1 skip), test_navmesh_index 35 (1 skip), test_rbf0_tree 38 (1 skip), test_shader_link_check 50, test_awc_manifest 20, test_classify_carve 27, test_rtti_scan 31 (1 skip) — all OK. Approved by @shayderrr.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Updated to 3aaeae56: I added awc_manifest.write_manifest() to the same failure-path fix after finding the identical leftover-temp behaviour at current head. The PR now covers four staged writers. Re-ran all four targeted files on this head (34/34 with 1 skip, 28/28 with 1 skip, 34/34 with 1 skip, 19/19), and each untouched-main control fails exactly its new regression; compileall, diff-check and no-game-data remain clean. PR body updated.

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested d79bf5c5 on Linux (x86_64, Python 3.13.5): pass (code read + unit tests).

What I ran:

  • git diff --check 03b09a08..HEAD: clean.
  • python3 -m unittest -v tests/test_bvh_index.py tests/test_navmesh_index.py tests/test_rbf0_tree.py: 96 tests, OK (1 case-insensitive-FS test skipped on Linux ext4). All 3 new test_failed_replace_of_read_only_report_removes_writable_temp tests pass.

Code review:

  • Restoring 0o600 before os.unlink(temp) in the failure cleanup path of tools/bvh_index.py, tools/navmesh_index.py, and tools/rbf0_tree.py cleanly prevents read-only staging files from being stranded on Windows when os.replace fails.
  • Minor non-blocking note: currently os.chmod(temp, 0o600) and os.unlink(temp) share a single with suppress(OSError): block. If os.chmod ever raises OSError on an unusual filesystem/ACL where unlink would still succeed, os.unlink(temp) would be skipped; separating with suppress(OSError): os.chmod(temp, 0o600) before with suppress(OSError): os.unlink(temp) makes the cleanup strictly safer.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 3aaeae5613 on Linux, Python 3.10, CPU only, synthetic fixtures, no game files: pass.

  • python -I tests/test_awc_manifest.py → 19/19; test_bvh_index.py → 34/34; test_navmesh_index.py → 28/28 (1 skip); test_rbf0_tree.py → 34/34.
  • Negative control on main's four tools: each suite has 1 failure, so the read-only staging-temp cleanup is covered.
  • git diff --check and check_no_game_data.py clean.

AI-assisted (Devin).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this at 3aaeae56 on Windows 11: pytest tests/test_awc_manifest.py tests/test_bvh_index.py tests/test_navmesh_index.py tests/test_rbf0_tree.py -q -> 110 passed, 5 skipped, 16 subtests passed; git diff --check clean.

On Windows, os.chmod(tmp, 0o600) clears the read-only attribute (it sets S_IWRITE), so the four _publish/failure paths can delete a staging temp that inherited a read-only destination's mode; the OSError is suppressed as before. The added tests cover the failed-replace cleanup. No blocking issue.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 852f41a merged with main 98fa503 on Linux (Python 3.12.16): concern - the 7 changed test files pass (awc_manifest 19, bvh_index 35, classify_carve 26, navmesh_index 31 (1 skipped), rbf0_tree 34, rtti_scan 31, shader_link_check 45); negative control with upstream/main tools/: awc_manifest, bvh_index, navmesh_index, rbf0_tree, rtti_scan each FAIL 1, shader_link_check stays OK (its test is Windows-only in effect: Linux unlink works on a read-only file, so it cannot fail here), classify_carve stays OK. Verified issue: in tests/test_classify_carve.py the new PublishTests class is appended after the if __name__ == "__main__": unittest.main() block, so python -I tests/test_classify_carve.py still runs 26 tests and never runs it (pytest does collect it: 27 tests, and with upstream/main classify_carve.py that one fails, 26 passed). Moving the class above the __main__ guard fixes it.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Fixed in b832130: PublishTests now sits above the if __name__ == "__main__": unittest.main() guard, so python -I tests/test_classify_carve.py collects and runs it — 27 tests, OK (was 26); git diff --check clean.

Your caveat about shader_link_check is right: its regression only fails where a read-only destination refuses unlink, so the Linux pass is expected and the class still covers the Windows path.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested b832130 merged with main d974c88 on Linux (Python 3.12.16) and native Windows (Python 3.13.1): concern.

  • The 7 changed test files, run directly with python -I tests/<file>.py: Linux awc_manifest 19, bvh_index 35, classify_carve 27, navmesh_index 31 (1 skipped), rbf0_tree 34, rtti_scan 31, shader_link_check 45, all OK. Windows: the same counts, all OK (a few skips).
  • Negative control (tools/ from main, PR tests kept): Linux awc_manifest, bvh_index, navmesh_index, rbf0_tree, rtti_scan each fail 1 and classify_carve errors 1; shader_link_check stays OK (Linux unlink works on a read-only file). Windows (checked for all but navmesh_index): awc_manifest, bvh_index, rbf0_tree, rtti_scan fail 1 and classify_carve errors 1; shader_link_check stays OK when run directly, see below.
  • Earlier issue (852f41a): PublishTests in tests/test_classify_carve.py sat after the __main__ guard and never ran directly. Resolved: it now runs (27 tests) and fails against main's tool.
  • Remaining verified issue: the same mistake is in tests/test_shader_link_check.py. PublishTest is appended after if __name__ == "__main__": unittest.main(verbosity=2), so python -I tests/test_shader_link_check.py runs 45 of the file's 46 tests and never runs it, with the PR's tools or main's. It is collected by discovery or pytest: with python -I -m unittest discover -s tests -p test_shader_link_check.py on Windows, 46 tests, PR tools OK, main tools 1 failure (so the test is valid on Windows, just not run by the documented invocation). Moving the class above the __main__ guard fixes it.

@memorypressure

Copy link
Copy Markdown

FAIL, two defects verified. 1) The new cleanup in tools/bvh_index.py:382 puts chmod and unlink in one suppress block: when the chmod raises, the suppress swallows it and the unlink is skipped, leaving the .bvh_index-*.tmp staging file behind — main, which unlinks directly, always removes it (confirmed in both sources; also present in navmesh_index.py and rbf0_tree.py). The repo already has a discard_staged helper that attempts each independently — please use it or split the block. 2) The new test class sits after the unittest.main() call in tests/test_shader_link_check.py, so the standard direct invocation runs 45 tests and never touches your additions (pytest runs 46) — move it above the call. Minor: the new test writes in test_awc_manifest.py omit newline="\n". Everything else is green here: the eight named suites pass on the merged tree and the mock denied-chmod differential is exactly as described. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@shayderrr shayderrr left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Reviewed tooling in PR #4516: Tools: clean read-only staging temps after a failed report replace.

Scope: tests/test_awc_manifest.py, tests/test_bvh_index.py, tests/test_classify_carve.py, +12 more
Diff: +199 / -22 across 15 file(s).

The change stays inside the stated scope and the touched files line up with the description. No unrelated production paths are pulled in, and the stated behavior is directly exercised by the diff. I'm approving this one.

Reviewed by shayderrr via automated batch review.

@shayderrr

Copy link
Copy Markdown
Collaborator

Thanks @bobcatchris - I went through Tools: clean read-only staging temps after a failed report replace and the diff around tests/test_awc_manifest.py. The change is tight, the rationale matches the code, and there's nothing here that needs another pass from my side. Approved and squash-merging it into main now. Nice work.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Both verified defects are fixed, head d63f68b5 (was b8321300). Thanks @memorypressure — the chmod/unlink one was a regression against main, exactly as you describe.

  1. Denied chmod no longer skips the unlink. bvh_index, navmesh_index and rbf0_tree put os.chmod(temp, 0o600) and os.unlink(temp) in one with suppress(OSError): block, so a refused chmod left the staging file behind — main unlinks directly and always removed it. All three now call file_mode.discard_staged(), the helper you pointed at, which the other four tools in this PR already used; it attempts each step separately and ignores a cleanup error so it cannot mask the replace error. from contextlib import suppress is gone from the three files (unused). Control: with os.chmod patched to raise PermissionError, bvh_index._publish on a read-only destination still leaves only report.json in the directory.
  2. test_shader_link_check.PublishTest moved above the __main__ guard. python -I tests/test_shader_link_check.py now reports Ran 46 tests (was 45), and the class is still collected by pytest/discovery.
  3. tests/test_awc_manifest.py pins newline="\n" on the new writes (three open(..., "w") and the write_text), as you asked.

Checks (Windows 11, CPython 3.12, CPU only): test_bvh_index, test_navmesh_index, test_rbf0_tree (1 skip each), test_shader_link_check 46, test_awc_manifest, test_classify_carve, test_rtti_scan (1 skip) all OK. python -m compileall -q tools tests exit 0, git diff --check clean.

@shayderrr — this landed after your approval at 14:12, so please take the new head rather than b832130: the two defects @memorypressure reported are the whole diff. Sorry for the churn this close to the release.

AI-assisted.

@bobcatchris

Copy link
Copy Markdown
Contributor Author

Rebased after the branch went conflicting (the dropped commit was already upstream): head is now 8d7b095d, based on 1cc6e3c1. The two defect fixes are unchanged — discard_staged() in bvh_index/navmesh_index/rbf0_tree, PublishTest above the __main__ guard, and newline="\n" in the new test_awc_manifest writes.

Re-ran the seven suites on the rebased tree (Windows 11, CPython 3.12): test_bvh_index, test_navmesh_index, test_rbf0_tree (1 skip each), test_shader_link_check (46), test_awc_manifest, test_classify_carve, test_rtti_scan (1 skip) all OK.

AI-assisted.

@Oliwettos

Copy link
Copy Markdown

Tested e5b3b0a merged with main 1cc6e3c on Ubuntu 24.04, Python 3.13.18. The three suites in this head pass, but the chmod/unlink fix described above is not in it.

  • The branch was force-pushed from d63f68b to 8d7b095 and then from 8d7b095 to e5b3b0a. e5b3b0a only touches classify_carve, rtti_scan, shader_link_check and file_mode; bvh_index, navmesh_index and rbf0_tree are not in its diff.
  • The first part of this PR is already on main (merge 4c4cb16), and main still has os.chmod(temp, 0o600) and os.unlink(temp) in one with suppress(OSError): block in tools/bvh_index.py, tools/navmesh_index.py and tools/rbf0_tree.py.
  • Repro on main 1cc6e3c, and on main + e5b3b0a: bvh_index._publish with a failing write and os.chmod(temp, 0o600) patched to raise PermissionError leaves .bvh_index-*.tmp in the folder. On 4c4cb16^1 (before the merge) the same run leaves nothing.
  • The newline="\n" pin on the new write in tests/test_awc_manifest.py is not on main either (open(out, "w", encoding="utf-8")).

So the discard_staged() change for those three tools (and the test newline) looks like it needs to be pushed again, here or in a follow-up.

The current head itself: test_classify_carve 27 OK, test_rtti_scan 31 OK, test_shader_link_check 47 OK, compileall clean. Negative control: the PR's tests on main fail in classify_carve (1 error) and rtti_scan (1 failure); shader_link_check also passes on main, as expected on Linux.

No game files used. AI-assisted (Claude).

bobcatchris added a commit to bobcatchris/Leonida that referenced this pull request Nov 23, 2026
The ItsNotRexed#4516 merge took the earlier cleanup, which put chmod and unlink in
one suppress block: a refused chmod skipped the unlink and stranded the
temp beside the report. All three writers now call file_mode.discard_staged,
which attempts each step independently. Each PublishTest gets a denied-chmod
regression, and file_mode gets one for the helper; all three fail on the
pre-fix tools, and the eight affected suites pass with them.

AI-assisted.
rtti_scan, shader_link_check and classify_carve give their staging file the
destination's mode just before os.replace, so a read-only destination makes the
replace fail and leaves a readable-only temp that Windows will not unlink.
shader_link_check was worse: its finally-block unlink raised and replaced the
real error. Share one discard_staged() helper in file_mode.py (clear the
read-only bit, ignore cleanup errors) and add a regression per module; each one
fails against main's code.

rpf9_names has the same shape but stages inside a TemporaryDirectory, whose
cleanup already clears read-only bits, so it needs no change.
Appending the class after unittest.main() left the read-only replace
regression unrun by python -I tests/test_classify_carve.py (26 tests, not
27); only pytest collected it. Move it above the guard so both runners
see it.

AI-assisted.
The ItsNotRexed#4516 merge took the earlier cleanup, which put chmod and unlink in
one suppress block: a refused chmod skipped the unlink and stranded the
temp beside the report. All three writers now call file_mode.discard_staged,
which attempts each step independently. Each PublishTest gets a denied-chmod
regression, and file_mode gets one for the helper; all three fail on the
pre-fix tools, and the eight affected suites pass with them.

AI-assisted.
@bobcatchris
bobcatchris force-pushed the fix/readonly-temp-cleanup branch from 34ddf34 to 500b0ff Compare November 23, 2026 15:51
@bobcatchris

Copy link
Copy Markdown
Contributor Author

You are right about e5b3b0a — that was a transient head, not the intended one. My rebase sequence dropped the three-tool commit and I force-pushed the intermediate state, so the commit you tested had only classify_carve / rtti_scan / shader_link_check / file_mode. That is exactly the state you measured the repro on, and the repro you describe is correct for it.

Current head 500b0ff12 (rebased onto 1d7350424) carries all three commits again:

500b0ff12 bvh_index, navmesh_index, rbf0_tree: unlink a denied-chmod staging temp
198598b0b tests: put PublishTests before the __main__ guard
129a93aeb Also clean the staging temp in three more report writers

and the tree at that head:

tools/bvh_index.py:45      from file_mode import discard_staged, published_mode
tools/bvh_index.py:404             discard_staged(temp)
tools/navmesh_index.py:38  from file_mode import discard_staged, published_mode
tools/navmesh_index.py:236         discard_staged(temp)
tools/rbf0_tree.py:32      from file_mode import discard_staged, published_mode
tools/rbf0_tree.py:673             discard_staged(temp)

tests/test_awc_manifest.py at the same head pins newline="\n" on both new open(..., "w") writes (lines 344 and 361); words.write_text at 172 is pre-existing and untouched.

Suites at 500b0ff12 on Windows 11 / CPython 3.12: test_bvh_index 41 (1 skip), test_navmesh_index 35 (1 skip), test_rbf0_tree 38 (1 skip), test_shader_link_check 50, test_awc_manifest 20, test_classify_carve 27, test_rtti_scan 31 (1 skip), all OK.

One thing your repro pins down that I could not state as strongly myself: on 4c4cb16bb^1 the same run leaves nothing, so this is a regression against main and not just a tidiness change — thank you for the before/after. Your shader_link_check count (47) also differs from mine (50) because the head moved again after your run; the PublishTest placement is unchanged.

AI-assisted.

@Oliwettos

Copy link
Copy Markdown

Re-tested 500b0ff merged with main 059f791 (clean merge) on Ubuntu 24.04, Python 3.13.18: pass, the regression is fixed.

  • Same repro as above, now for all three writers (bvh_index._publish, navmesh_index._publish, rbf0_tree.write_json), with a failing write and os.chmod(temp, 0o600) patched to raise: no .tmp left on this head; on main 059f791 each of the three leaves its staging file.
  • python -I tests/<file>.py: test_bvh_index 41, test_navmesh_index 35 (1 skip), test_rbf0_tree 38, test_awc_manifest 20, test_classify_carve 27, test_rtti_scan 31, test_shader_link_check 50, all OK.
  • The two new writes in tests/test_awc_manifest.py (lines 344 and 361) pin newline="\n".

No game files used. AI-assisted (Claude).

@rscriptsfivem

Copy link
Copy Markdown

Independent Windows 11 check of 500b0ff12 with CPython 3.14.5: pass.

With actual read-only destinations and no mocks, all five tested report writers preserved the original report, surfaced the replace error, and left no staging files. On main at 8575d36ed, rtti_scan and shader_link_check each left a staging file, and shader_link_check masked the replace error with a cleanup error. All five writers still replaced writable reports successfully.

Eight Python suites: 240 passed, 8 skipped, 0 failed. Synthetic inputs only, CPU only. AI-assisted (Codex).

@senyurah

Copy link
Copy Markdown

Integration check on Windows, Python 3.13.14: 500b0ff12 and #7322 (a80df6e93) merge cleanly together onto main at 511e3826b. Both PRs touch navmesh_index.py and its tests. Nine affected test suites passed with 263 passes, 8 skips and no failures, including the navmesh manifest-path and read-only staging cleanup regressions. git diff --cached --check passed. Synthetic inputs only. AI-assisted (Codex).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants