ai-coding-minesIndexGitHub

A shared state file with no owner gets clobbered whole

Python and databases

Symptom

One JSON state file read by 3+ processes and written by 2 (a 30-minute cron, a 16-minute cron, a manual batch). Any write that lands between another process's read and write erases everything in between.

Fix (you need all three)

  1. fcntl.flock
  2. Reload after acquiring the lock. The lock alone is not enough: write from a value read before the lock and the changes in between vanish.
  3. Atomic replace with os.replace

Same root as the progress-file name collision: a shared resource with no owner.

There were days with zero loss. Pure luck.