crewAI icon indicating copy to clipboard operation
crewAI copied to clipboard

[BUG] JsonProvider checkpoint writes are not atomic — a crash mid-write corrupts the newest checkpoint

Open AP24110011695 opened this issue 1 month ago • 1 comments

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

  1. Start a checkpoint write of a large payload (or inject an exception / kill the process after the first f.write chunk).
  2. Observe the checkpoint file existing at its final name containing half the JSON.
  3. provider.from_checkpoint(path) raises json.JSONDecodeError, and prune preserves 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-generated label (I can't set labels from my account — could a maintainer add it, and bug?).

AP24110011695 avatar Oct 30 '26 23:10 AP24110011695

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.

github-actions[bot] avatar Nov 30 '26 12:11 github-actions[bot]