bary_fallback_gs: restore write mode before unlink on replace failure and publish mode (BARY-FALLBACK-GS-STAGE-CLEANUP)

Apply published_mode(asm/spv) to temporary files 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:53:23 +03:00
parent c22a0a7322
commit ca8a627c5c
2 changed files with 38 additions and 2 deletions

View File

@ -849,6 +849,32 @@ class TestMainAndCLI(unittest.TestCase):
leftovers = [name for name in os.listdir(tmp) if name.startswith(".bary-gs-")]
self.assertEqual(leftovers, [])
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)
with tempfile.TemporaryDirectory() as tmp:
with open(os.path.join(tmp, "index.json"), "w", encoding="utf-8") as f:
json.dump({
"ps": {
"stage": "ps",
"inputs": [0],
"bary_fallback_input": {"location": 1},
}
}, f)
with mock.patch("subprocess.run", side_effect=_successful_spirv_tools), \
mock.patch("os.replace", side_effect=OSError("replace failed")), \
mock.patch("os.chmod", side_effect=track_chmod):
with self.assertRaisesRegex(OSError, "replace failed"):
bary_fallback_gs.build_for_index(tmp)
self.assertTrue(any(mode == 0o600 for _, mode in chmod_calls), chmod_calls)
if __name__ == "__main__":
unittest.main()

View File

@ -253,8 +253,18 @@ def build_for_index(spirv_dir: str | os.PathLike[str], pair: tuple[str, str] | N
r = subprocess.run(cmd, capture_output=True, text=True)
if r.returncode:
raise RuntimeError(f"{cmd[0]} failed for {asm}:\n{r.stdout}{r.stderr}")
os.replace(staged_asm, asm)
os.replace(staged_spv, spv)
os.chmod(staged_asm, published_mode(asm))
os.chmod(staged_spv, published_mode(spv))
try:
os.replace(staged_asm, asm)
os.replace(staged_spv, spv)
except BaseException:
for s in (staged_asm, staged_spv):
try:
os.chmod(s, 0o600)
except OSError:
pass
raise
built.append(spv)
return built