Skip to content

bvh_slot_check: publish output with the bits of a plain open(), not the staging file's 0600 - #7625

Closed
best2135 wants to merge 1 commit into
ProjectLeonida:mainfrom
best2135:hunt/bvh-slot-check-mode
Closed

best2135 wants to merge 1 commit into
ProjectLeonida:mainfrom
best2135:hunt/bvh-slot-check-mode

Conversation

@best2135

Copy link
Copy Markdown

Claimed in the coordination issue (claim).

save_evidence in tools/bvh_slot_check.py stages its output in a private temporary file (tempfile creates it 0600) and os.replace()s it into place without restoring the mode. So:

  • a new output comes out 0600 instead of what a plain open() gives under the umask (0644 under umask 022);
  • a re-run resets a file the user made 0640 (or 0644) to 0600.

tools/file_mode.py exists for exactly this and is used by the other staged writers (#1082). This calls os.chmod(tmp, published_mode(dest)) before the replace, as they do.

Test: two new tests in tests/test_bvh_slot_check.py, both built by hand (no game data): a new output gets file_mode.new_file_mode(dir), and a replaced 0640 output stays 0640. Both fail on main (384 != 420 / 384 != 416) and pass with this change.

Ran (Linux, Python 3.14.8, CPU only):

Skipped on Windows (os.name == "nt"), where the POSIX bits don't apply. Same bug class as the timecycle_pairs (#7572) and shader_disasm (#7573) PRs, but in different files, so the PRs don't overlap.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

… not 0600

save_evidence stages the JSON through tempfile.NamedTemporaryFile, which creates
the file 0600, and os.replace()s it into place without restoring the mode. New
evidence came out 0600 instead of the umask's bits, and an incremental run reset a
0640 file to 0600. Give the staged file file_mode.published_mode(path) first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kapitche

Copy link
Copy Markdown
Collaborator

Tested 3c7d73a merged with main 08d1698 on Linux/Python 3.12.16: pass

Commands run (python -I tests/<file>.py, worktree = main + PR merge), results:

tests/test_bvh_slot_check.py:  Ran 11 tests in 0.082s  FAILED (errors=3) 

Negative control (non-test files restored from main, tests kept):

tests/test_bvh_slot_check.py:  Ran 11 tests in 0.084s  FAILED (failures=2, errors=3) 

The 3 errors in SlotControls (ValueError: too many values to unpack (expected 5) at tools/bvh_slot_check.py line 153, from M.model_geometries) are present identically on main without this PR (negative-control run: errors=3 plus the 2 new failures), so they are not caused by this PR. With the PR, the two new EvidenceModeTests pass; without the tools/bvh_slot_check.py change they fail. Diff read: os.chmod(temporary, published_mode(path)) is added before the os.replace in save_evidence.

@slint6614-dev

Copy link
Copy Markdown
Contributor

Checked on Linux: merged onto main 77fa308 (merges cleanly).

tests/test_bvh_slot_check.py fails with 3 errors here, but main fails the same way (ValueError: too many values to unpack (expected 5, got 6) at bvh_slot_check.py:153): since #6330, bvh_match.model_geometries returns a 6-tuple. #6513 (and #7176) fix that line and are still open.

With that one-line fix applied on top of this PR:

  • python3 -B tests/test_bvh_slot_check.py → OK
  • Negative control: with only the PR's save_evidence chmod reverted, 3 tests fail.

So this change is fine, but it should probably say Depends on: #6513 to go green.

@AsgerCode

Copy link
Copy Markdown

Tested 3c7d73a on Windows 11 (NTFS), Python 3.12.10 (CPU only, no GPU, no game files): fail, on Windows a read-only output now leaves a read-only staging file behind

What I ran:

case main this PR
new output 0o666, writable 0o666, writable
re-run on a writable output 0o666 0o666
re-run on a read-only output (os.chmod(out, stat.S_IREAD)) PermissionError from the replace, no files left PermissionError on the staging file, tmpXXXXXXXX (0o444) left in the output folder

On Windows, os.chmod only sets or clears the read-only attribute. With a read-only output, published_mode(path) returns 0o444, so the staging file becomes read-only before os.replace. The replace fails, as on main, but then temporary.unlink(missing_ok=True) in the finally also fails with [WinError 5] Access is denied. The staging file stays next to the output. The exception that reaches main() (and its error: ... line) is the unlink's, so it names the staging file instead of the output.

tools/bvh_index.py (around line 399) and tools/rbf0_tree.py (around line 668) already handle this case. They os.chmod(temp, 0o600) before the unlink, with the comment "make it deletable first, or Windows strands it beside the report when the replace fails". The same fix works here (needs from contextlib import suppress; I tried it locally: no staging file left, and the error names the replace again):

    finally:
        with suppress(OSError):
            os.chmod(temporary, 0o600)   # may carry a read-only destination's mode
        temporary.unlink(missing_ok=True)

POSIX: I can only reason about this, not run it. On POSIX, rename() and unlink() depend on the directory permissions, not on the file mode. So the cleanup works there, and the code does what the description says: a new output gets the probe's 0666 & ~umask bits, and a replaced 0640 output stays 0640.

Nit, not blocking: the test class says "POSIX permission bits", but the change has an effect on Windows too (the read-only attribute). A Windows test with a read-only output that checks that no staging file is left would catch the case above.

AI-assisted (Claude Code).

@rideinstead

Copy link
Copy Markdown

✅ Automated security review: CLEAN (commit 3c7d73ae)

  • No findings.

Scope: static analysis of added diff lines only; no code was executed. Please review findings manually.

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Tested 3c7d73ae on Linux, CPython 3.14, CPU only, synthetic inputs, merged onto main 867d922f: pass for this PR's own change, with one failure that is already on main

  • python -I tests/test_bvh_slot_check.py: Ran 11 tests, FAILED (errors=3) on unmodified main too. With only the non-test files reverted to main: FAILED (failures=2, errors=3).
  • compileall and scripts/check_no_game_data.py on the changed files: clean.

Three errors are already on main (ValueError: too many values to unpack (expected 5, got 6) in the test helpers), so a maintainer merging this inherits a red file that is not this PR's fault; the new publish-mode test is the only extra failure without the change.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting.

@GonZiin

GonZiin commented Nov 23, 2026

Copy link
Copy Markdown

Overlap note from the author of #8004: it makes the same published_mode change in bvh_slot_check.save_evidence and additionally handles a failed os.replace (restores write permissions, keeps the existing file). Your PR is the older one, so I am leaving it to the maintainers to pick one; whichever lands second will rebase.

AI-assisted (Claude Code).

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.

8 participants