Atomic File Writes

Recently I’ve seen a couple of cases where users have ended up with corrupted files — almost always in relation to a plugin crashing coincidentally at the same time the song, rack or settings file was being written.

The result is a half written file that can’t be read back.

To mitigate against this, Cantabile now writes all JSON files atomically — that is it’s either an all or nothing result. This applies to Cantabile’s settings file, song and rack files, set lists and a few other files.

How it Works

The approach used for writing JSON files is pretty simple — its saves the file first with an extra .tmp file extension and then renames it afterwards.

Suppose it’s saving MySong.CantabileSong it will:

  • Write a file called MySong.CantabileSong.tmp
  • Wait for it to be successfully written and flushed
  • Delete the old MySong.CantabileSong file
  • Rename MySong.CantabileSong.tmp to MySong.CantabileSong

Worst Case

The worst case scenario now is if it crashes between steps 3 and 4. In that case you’re song file will be missing, but an updated version will be available in the .tmp file if you need to recover it.

This will be available in build 3239.


Sven Godly
Cantabile is getting so powerful now we should call it Atomic


If the main file is missing or corrupt, one could look for the tmp file and offer a msg about recovering it.
In systems where I’m paranoid I write the new tmp, rename the main as .old, rename the new and then either keep one copy of the old or delete it last thing.

In the old days I worked with our own database ISAM on minicomputers and record protection was everything.
It’s an old problem - welcome to the club.

1 Like

As does Sound Forge. Excellent feature.