Skip to content

Commit c262110

Browse files
author
yolo h8cker 93
committed
Ensure atomic writes in matrix.py
1 parent ba1738d commit c262110

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

codeclash/analysis/matrix.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ def matrices(self) -> dict:
8787

8888
# -----------------------
8989

90+
def _save(self):
91+
"""Save metadata to matrix.json."""
92+
with self._save_lock:
93+
# let's make this high stakes write atomic, because else if you run out of disk space,
94+
# you'll lose all progress
95+
tmp_file = self.output_file.with_suffix(".tmp")
96+
tmp_file.write_text(json.dumps(self._metadata, indent=2))
97+
tmp_file.rename(self.output_file)
98+
9099
def _load_existing_progress(self):
91100
"""Load existing progress from matrix.json if it exists."""
92101
if not self.output_file.exists():
@@ -213,7 +222,7 @@ def _evaluate_matrix_cell_parallel(
213222

214223
with self._save_lock:
215224
self.matrices[matrix_id][str(i)][str(j)] = result
216-
self.output_file.write_text(json.dumps(self._metadata, indent=2))
225+
self._save()
217226

218227
return (i, j, result)
219228

@@ -300,7 +309,7 @@ def evaluate_all_matrices(self) -> dict:
300309

301310
def end(self):
302311
"""Save metadata and clean up resources."""
303-
self.output_file.write_text(json.dumps(self._metadata, indent=2))
312+
self._save()
304313
self.logger.info(f"Matrix evaluation results saved to {self.output_file}")
305314

306315
# Clean up all game workers

0 commit comments

Comments
 (0)