[BUG] JsonProvider checkpoint writes are not atomic — a crash mid-write corrupts the newest checkpoint
Description
JsonProvider.checkpoint / acheckpoint (the default JSON checkpoint backend) write the state file directly to its final path with open(file_path, "w") (resp. aiofiles.open(file_path, "w")):
https://github.com/crewAIInc/crewAI/blob/97bbfb614a4ed0cf7e3f0ac8761f0d97b19c6ce7/lib/crewai/src/crewai/state/provider/json_provider.py#L66-L68
Opening with "w" truncates the destination and streams the payload. A process crash, OOM, or disk-full condition mid-write therefore leaves a truncated JSON file at the final path. Because prune() sorts by mtime and keeps the newest max_keep files, the corrupt file is precisely the one retained — and the one from_checkpoint reads on resume, so restore fails with a JSONDecodeError exactly when recovery is needed.
For a crash-recovery subsystem, the write should be atomic.
Steps to Reproduce
- Start a checkpoint write of a large payload (or inject an exception / kill the process after the first
f.writechunk). - Observe the checkpoint file existing at its final name containing half the JSON.
provider.from_checkpoint(path)raisesjson.JSONDecodeError, andprunepreserves the corrupt file as the newest one.
Equivalent deterministic repro of the failure window (the destination is truncated before the payload is written):
from crewai.state.provider.json_provider import JsonProvider
p = JsonProvider()
loc = p.checkpoint('{"step": 0}', tmp_dir) # fine
# simulate a crash during a rewrite of the same file:
with open(loc, "w", encoding="utf-8") as f: # truncates first
f.write('{"step": 1 "trunc') # process dies here
p.from_checkpoint(loc) # json.JSONDecodeError
Expected behavior
A crash during checkpoint() must never leave a partial checkpoint at the destination. Readers should see either the previous complete file or the new complete file — never a truncated one.
Possible Solution
Write to a temp file next to the destination and publish with os.replace(tmp, file_path) (atomic on POSIX and Windows; same directory → same volume). The async path can use aiofiles.os.replace. Since _build_path already generates a unique filename per checkpoint, the temp name cannot collide between concurrent writes.
I have this implemented with regression tests (successful publish leaves no temp files; a failed publish never creates the destination and the previous checkpoint stays readable; sync and async paths). Will open a PR linked to this issue.
Additional context
- crewAI version: current
main(97bbfb61), issue present since the provider was introduced. - Found while reviewing the checkpoint subsystem (see also #8843/#8845 for the SQLite backend's SQLite < 3.45 issue).
- This report and the accompanying fix/tests were prepared with an AI coding assistant, so per CONTRIBUTING.md the PR should carry the
llm-generatedlabel (I can't set labels from my account — could a maintainer add it, andbug?).
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.