Skip to content

Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.

fix: serialize concurrent checkpoint publication (#320)

Wu Shuwen28c0fb5d1b98
diff --git a/ifixai/evaluation/checkpoint.py b/ifixai/evaluation/checkpoint.py
index cdaf420..5f52faf 100644
--- a/ifixai/evaluation/checkpoint.py
+++ b/ifixai/evaluation/checkpoint.py
@@ -19,6 +19,7 @@ import json
import logging
from pathlib import Path
from tempfile import NamedTemporaryFile
+from threading import Lock
from typing import Optional
from ifixai.core.types import TestResult, TestStatus
@@ -26,6 +27,7 @@ from ifixai.evaluation.manifest import RunManifest
from ifixai.evaluation.schemas import ResumeState
_logger = logging.getLogger(__name__)
+_checkpoint_publish_lock = Lock()
def _checkpoint_path(runs_root: Path, run_id: str) -> Path:
@@ -85,7 +87,10 @@ def save_checkpoint(
try:
with fh:
json.dump(payload, fh, indent=2, sort_keys=True, default=str)
- tmp.replace(path)
+ # Windows can reject simultaneous replacements of the same destination.
+ # Serialize only the atomic publish step; staging remains concurrent.
+ with _checkpoint_publish_lock:
+ tmp.replace(path)
finally:
tmp.unlink(missing_ok=True)
return path
diff --git a/ifixai/tests/evaluation/test_checkpoint_atomic_save.py b/ifixai/tests/evaluation/test_checkpoint_atomic_save.py
index 815acf9..647d917 100644
--- a/ifixai/tests/evaluation/test_checkpoint_atomic_save.py
+++ b/ifixai/tests/evaluation/test_checkpoint_atomic_save.py
@@ -7,19 +7,19 @@ from threading import Barrier
import pytest
from ifixai.core.types import TestResult
+from ifixai.evaluation import checkpoint
from ifixai.evaluation.checkpoint import load_checkpoint, save_checkpoint
def test_overlapping_saves_publish_complete_checkpoints(tmp_path, monkeypatch):
barrier = Barrier(2)
- replace = Path.replace
+ dump = checkpoint.json.dump
- def synchronized_replace(self, target):
- if Path(target).name == "checkpoint.json":
- barrier.wait(timeout=5)
- return replace(self, target)
+ def synchronized_dump(payload, stream, **kwargs):
+ dump(payload, stream, **kwargs)
+ barrier.wait(timeout=5)
- monkeypatch.setattr(Path, "replace", synchronized_replace)
+ monkeypatch.setattr(checkpoint.json, "dump", synchronized_dump)
results = [{key: TestResult(test_id=key)} for key in ("B01", "B08")]
with ThreadPoolExecutor(max_workers=2) as executor:
futures = [executor.submit(save_checkpoint, tmp_path, "run-1", r) for r in results]