Skip to content

glb_writer, rtti_scan: find a new file's mode without touching the umask - #866

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
quackottu:glb-umask-lock
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
quackottu:glb-umask-lock

Conversation

@quackottu

@quackottu quackottu commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

New BUILD item, claimed in #634 (#634 (comment)), from my review of #728.

tools/glb_writer.py and tools/rtti_scan.py read the umask with os.umask(0) then os.umask(mask). The umask is process-wide, so while one thread is inside that pair every other thread that creates a file gets umask 0, and two GLB writers can interleave so the process stays at 0 for good.

My first version put a lock around the pair; @kapitche and @kotorvr pointed out in review that it only orders GLB writers, not other threads. This now uses @kotorvr's commit (a85be7b, authored by them, taken with their offer in the review) instead, which never touches the umask:

  • tools/file_mode.py: new_file_mode(dir) creates a .mode-<random>.probe with os.open(..., O_CREAT|O_EXCL, 0o666), reads the bits back with fstat and removes it, so the current umask and a directory's default ACL apply without changing process state. published_mode(path) returns the replaced file's bits, or that.
  • glb_writer and rtti_scan both use it.
  • tests/test_file_mode.py: patches os.umask to raise and runs write_glb and write_report on a new and a replaced file (fails with main's tools); POSIX cases for 0640 under umask 027 and 0664 under 002, and no probe left behind.

Tested on Windows 11 Pro (26300), Python 3.12.10: test_file_mode OK (2 POSIX skips), test_glb_atomic OK (1 skip), test_rtti_scan OK (1 skip), test_glb_writer OK. @kapitche's WSL run covered the POSIX side of the earlier version; the POSIX cases of test_file_mode haven't run yet.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Linux (WSL2 Ubuntu), Python 3.12, CPU only: pass

@kotorvr

kotorvr commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Reviewed 808c947: 1 finding, plus an offer

The lock fixes writer-vs-writer, but as @kapitche says, any other thread creating a file during the set-and-restore still gets umask 0. I'd also started on this from their #728/#750 comments before seeing your claim, so I haven't opened a PR. Instead, here is a branch you can take or ignore: kotorvr:fix/umask-probe (a85be7b).

  • tools/file_mode.py: new_file_mode(dir) creates a .mode-<random>.probe with os.open(..., O_CREAT|O_EXCL, 0o666), reads the bits with fstat, then removes it. published_mode(path) returns the replaced file's bits, or that. It never calls os.umask, so there's no window at all, and it follows a directory's default ACL and later umask changes (a value read at import misses both).
  • glb_writer and rtti_scan both use it.
  • tests/test_file_mode.py: patches os.umask to raise and runs write_glb and write_report on a new file and a replaced one. With main's two tools that fails for both writers. The POSIX cases check 0640 under umask 027 and 0664 under 002, and that no probe is left behind.

Windows 11, Python 3.13.7: test_file_mode 5 OK (2 POSIX skips); test_glb_atomic, test_rtti_scan, test_glb_writer and test_world_build OK. The POSIX cases haven't run yet.

If you'd rather keep the lock, that's fine too. Then reading once at import, as kapitche suggested, would close the window for other threads. Say if you want me to open the branch as its own PR after yours instead.

AI-assisted (Claude Code).

@quackottu

Copy link
Copy Markdown
Contributor Author

Thanks both. You're right that the lock only ordered GLB writers. I've replaced my commit with @kotorvr's a85be7b (their authorship kept), so neither tool touches the umask any more; description updated. Rebased on 4a36bb1; test_file_mode, test_glb_atomic, test_rtti_scan and test_glb_writer pass on Windows. @kapitche, if you still have the WSL setup, a run of tests/test_file_mode.py there would cover its POSIX cases, which haven't run yet.

ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
…the umask

glb_writer._output_mode and rtti_scan._report_mode read the umask with
os.umask(0) then os.umask(mask). The umask is process-wide, so any thread
creating a file in that window got mask 0. A probe file created with mode
0666 gives the same bits (and honours a directory's default ACL) without
changing anything. Raised by kapitche on ItsNotRexed#750 and ItsNotRexed#728.

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

Copy link
Copy Markdown
Contributor Author

The first version (808c947, the umask lock) landed on main in 85e8388, but the fix @kotorvr and @kapitche asked for didn't: the lock only orders GLB writers. I've rebased this onto main, so it now carries only @kotorvr's a85be7b (now 430265a): file_mode.published_mode with a probe file, the lock removed. test_file_mode + test_glb_writer: 49 tests OK (2 POSIX-only skipped on Windows).

@abilliontokens

Copy link
Copy Markdown
Contributor

Review: not ready — the rebase onto current main leaves one of main's own tests broken.

Blocking:

  • tools/glb_writer.py (this diff) deletes _output_mode and the umask lock, but origin/main's tests/test_glb_atomic.py:218 still calls glb_writer._output_mode(...) in test_concurrent_new_files_leave_the_umask_alone (line 199). On head 430265a (rebased onto 067d3e8): python -I tests/test_glb_atomic.py -> 12 tests, 1 failure: AttributeError: module 'glb_writer' has no attribute '_output_mode', then AssertionError: [] != [0o644, 0o644].
  • CI agrees: on 430265a the check job (run 44988185762, job 137103593292) fails at step "Tests (standard library only, no game data)", which is the loop over every tests/test_*.py (.github/workflows/ci.yml:52,70, windows-latest).
  • That test's subject is the lock this PR replaces, so it should be removed or repointed at the new helper; tests/test_file_mode.py::test_writes_never_call_umask already covers the new invariant. If you keep it, make it POSIX-only and use glb_writer.published_mode: the probe yields 0o644 under umask 022 on POSIX but 0o666 on Windows, so the 0o644 expectation is POSIX-specific.

Everything else on this head is green here: test_file_mode 5 OK (2 POSIX skips), test_glb_writer 44 OK, test_rtti_scan 17 OK (1 skip). The POSIX-only cases (0640 under umask 027, 0664 under 002) are skipped on Windows, so that side is still unverified on this box, as the description says.

The approach itself is right: neither tool calls os.umask any more; tools/file_mode.py:16 new_file_mode() creates and removes a .mode-<hex>.probe with O_CREAT|O_EXCL and returns the fstat bits, and published_mode() (line 36) prefers the replaced file's bits. Stdlib only (os, secrets, stat), no game data, the diff is 4 files with no README or other base drift.

AI-assisted (omp orchestrator).

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Thanks @quackottu for taking it, and @abilliontokens for catching the stale test. My fault: I wrote the helper against a main that didn't have the lock test yet.

The fix is one commit on top of your head, 430265a: kotorvr:fix/umask-probe-2 (65a9e14). Fetch it with git pull https://github.com/kotorvr/Leonida fix/umask-probe-2. It keeps the test's name and what it's for, but no longer calls _output_mode. Instead, 8 threads publish new GLBs with os.umask patched, and the test requires that it's never called. main's lock version fails it, which is what the old test was meant to catch.

Ran on Windows 11, Python 3.13.7, on 65a9e14:

  • test_glb_atomic: 12 OK (1 POSIX skip)
  • test_file_mode: 5 OK (2 POSIX skips)
  • test_glb_writer, test_rtti_scan and test_world_build: OK
  • Negative control: with main's glb_writer.py, test_concurrent_new_files_leave_the_umask_alone fails.

AI-assisted (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 4ebaf29 into ItsNotRexed:main Nov 22, 2026
10 of 11 checks passed
ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
test_glb_atomic still drove glb_writer._output_mode through a fake umask; #866 moved the mode to
file_mode.published_mode, which uses a probe file and never sets the umask, so the test now checks exactly that.
test_run_gpu_wavemask's fake run_cmd takes the gpu= keyword.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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