From 912d15cc7ae4445e4b973f234b8d8db1bbf31c5c Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 24 Nov 2026 17:55:03 +0300 Subject: [PATCH] mesh_compute_vs: restore write mode before unlink on replace failure and publish mode (MESH-COMPUTE-VS-STAGE-CLEANUP) Apply published_mode(spv) to temporary staged SPV before atomic replacement, and restore write permission (0o600) before directory cleanup in exception block when replace fails, preventing Windows PermissionError [WinError 5] and stranded temporary files. agent: yes AI-assisted: yes --- tests/test_mesh_compute_vs.py | 26 ++++++++++++++++++++++++++ tools/mesh_compute_vs.py | 13 ++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/test_mesh_compute_vs.py b/tests/test_mesh_compute_vs.py index 0d85d74fa..a3f44b286 100644 --- a/tests/test_mesh_compute_vs.py +++ b/tests/test_mesh_compute_vs.py @@ -273,6 +273,32 @@ class Cli(unittest.TestCase): self.assertEqual(len(stderr.splitlines()), 1) self.assertIn("Permission denied", stderr) + def test_stage_cleanup_restores_write_mode_on_replace_failure(self): + chmod_calls = [] + real_chmod = os.chmod + + def track_chmod(path, mode, *args, **kwargs): + chmod_calls.append((str(path), mode)) + return real_chmod(path, mode, *args, **kwargs) + + def fake_run(cmd, *args, **kwargs): + if cmd[0] == "spirv-as": + with open(cmd[cmd.index("-o") + 1], "wb") as f: + f.write(b"spv binary") + return mock.Mock(returncode=0, stdout="", stderr="") + + with tempfile.TemporaryDirectory() as tmp: + write_index(tmp, {"ms_a": {"mesh_compute": layout(clip=1)}}) + + with mock.patch.object(vs, "tool_path", side_effect=lambda name: name), \ + mock.patch("subprocess.run", side_effect=fake_run), \ + mock.patch("os.replace", side_effect=OSError("replace failed")), \ + mock.patch("os.chmod", side_effect=track_chmod): + with self.assertRaisesRegex(OSError, "replace failed"): + vs.build_for_index(tmp) + + self.assertTrue(any(mode == 0o600 for _, mode in chmod_calls), chmod_calls) + if __name__ == "__main__": unittest.main() diff --git a/tools/mesh_compute_vs.py b/tools/mesh_compute_vs.py index b5341c431..34a658fd5 100644 --- a/tools/mesh_compute_vs.py +++ b/tools/mesh_compute_vs.py @@ -28,6 +28,9 @@ import subprocess import sys import tempfile +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +from file_mode import published_mode + SUFFIX = ".mesh_vs" # .mesh_vs.spvasm / .spv next to the mesh shader USER_SGPRS = 32 # push constants: s0..s31, heap base, code base, then the output address (rdna2spirv.py) PC_TAIL_SET = 1 # set/binding of rdna2spirv.py --pc-tail-ubo's uniform block (6 dwords with --mesh-compute) @@ -317,7 +320,15 @@ def build_for_index(spirv_dir): r = subprocess.run(cmd, capture_output=True, text=True) if r.returncode: raise RuntimeError(f"{name} failed for {asm}:\n{r.stdout}{r.stderr}") - os.replace(staged_spv, spv) + os.chmod(staged_spv, published_mode(spv)) + try: + os.replace(staged_spv, spv) + except BaseException: + try: + os.chmod(staged_spv, 0o600) + except OSError: + pass + raise built.append(spv) return built