fix: write the database cache atomically

This commit is contained in:
Abdessamad Derraz committed 2026-10-17 01:54:35 +02:00
1 parent 9ea581d1d6
commit 78162e7022
2 files changed
+28 -4

No files matched your search

+8 -4
View File
@@ -29,6 +29,7 @@ from common import (
require_yaml,
source_yaml_paths,
write_if_changed,
write_text_atomic,
yaml_load,
)
@@ -258,11 +259,14 @@ def load_cache(cache_path: str) -> dict:
return {}
def save_cache(cache_path: str, cache: dict):
"""Save cache to disk."""
def save_cache(cache_path: str, cache: dict) -> None:
"""Save the cache whole or not at all.
Written in place, a crash while serializing left a truncated file that
the next run read as empty and rehashed the collection from scratch.
"""
os.makedirs(os.path.dirname(cache_path), exist_ok=True)
with open(cache_path, "w") as f:
json.dump(cache, f)
write_text_atomic(cache_path, json.dumps(cache))
def _load_gitignored_large_files() -> set[str]: