Why Rewriting a JSON File in Place Can Leave Readers With Broken Data
A developer's page-building script repeatedly failed with JSONDecodeError because it read a JSON index while another script was rewriting it. Testing on Linux with Python 3.10.12 showed that open(path, 'w') truncates the file before new data is written, so readers often saw empty or partial content. Killing the writer mid-write also left a corrupted fragment with no intact version to fall back on. Writing to a temporary file and then swapping it in with os.replace eliminated the broken reads in the same test. The author notes three related pitfalls to watch for: a temp folder on a different filesystem, file permissions, and symlinks.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in