Merge pull request #845 from trigger337/fix/native-pso-hashes-stage-cleanup

native_pso_hashes: restore write mode before unlink on replace failure and publish mode (NATIVE-PSO-HASHES-STAGE-CLEANUP)
This commit is contained in:
MoonEyes
2026-11-28 12:09:33 +01:00
committed by GitHub
2 changed files with 21 additions and 0 deletions

View File

@ -152,6 +152,21 @@ class NativePsoHashesTest(unittest.TestCase):
with self.assertRaises(tool.DigestClash):
tool.build(self.dxbc, self.spirv)
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 mock.patch.object(tool.os, "replace", side_effect=OSError("replace failed")), \
mock.patch.object(tool.os, "chmod", side_effect=track_chmod):
with self.assertRaisesRegex(OSError, "replace failed"):
tool.write(self.spirv, {"key": ("vs", "path")})
self.assertTrue(any(mode == 0o600 for _, mode in chmod_calls), chmod_calls)
if __name__ == "__main__":
unittest.main()

View File

@ -16,6 +16,7 @@ import sys
import tempfile
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from file_mode import published_mode
from shader_carve import parts
HEADER = "# native_pso_hashes 1"
@ -88,9 +89,14 @@ def write(spirv: str, table: dict) -> str:
for key in sorted(table):
stage, path = table[key]
tmp.write(f"{key}\t{stage}\t{path}\n")
os.chmod(tmp.name, published_mode(out))
os.replace(tmp.name, out)
finally:
if os.path.exists(tmp.name):
try:
os.chmod(tmp.name, 0o600)
except OSError:
pass
os.unlink(tmp.name)
return out