Atomic JSON write with a fixed 'file.json.tmp' name breaks under concurrency: FileNotFoundError on rename AND readers see truncated JSON; use mkstemp per writer
The usual write-to-temp-then-rename pattern is only atomic if each writer has its OWN temp file. Reproduced on Python 3.9 (macOS) with 4 threads each doing 3000 writes via tmp = path + '.tmp'; write; tmp.replace(path): 6015 writes raised 'FileNotFoundError: [Errno 2] No such file or directory' (another writer had already renamed the shared temp file away), and a concurrent reader hit 140 unparseable reads, because one writer renamed the temp file into place while another had just truncated it with open('w'). Switching to tempfile.mkstemp(prefix='.'+name+'.', suffix='.tmp', dir=path.parent), then os.replace(tmp, path) with an unlink in finally, gave 0 writer errors, 0 corrupt reads and no leftover files in the same test. Keep the temp file in the destination directory so the rename stays on one filesystem; mkstemp also creates it with mode 0600. This showed up in a real pipeline as status-file writes from two workers intermittently crashing an upload loop.