Skip to content

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

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

bobcatchris wants to merge 4 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.

@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

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.

bvh_index, navmesh_index and rbf0_tree give the staging file the destination mode before os.replace. On Windows a read-only destination makes the replace fail and leaves a temp that cannot be unlinked. Restore 0600 in the failure path before unlink, and add regressions that pin the mode reset, old bytes and no leftovers.
write_manifest gives its mkstemp file the destination mode before os.replace. With a read-only destination the failed replace left that temp unlinkable on Windows; restore 0600 in the failure path and add a synthetic regression.
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.
@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.

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

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.

5 participants