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
This commit is contained in:
Andrey
2026-11-24 17:55:03 +03:00
parent c22a0a7322
commit 912d15cc7a
2 changed files with 38 additions and 1 deletions

View File

@ -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()

View File

@ -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" # <ms name>.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