It turns out that shutil.move() is not atomic, which can lead to
issues when replacing the database under heavy load. There is an
alternative in os.replace() that *is* atomic, but does not work across
filesystems. For the best of both worlds, we now try os.replace(), and
fall back to shutil.move() if it fails.
Testing shows that os.replace() raises an OSError across multiple
filesystems, so that's what we catch. The documentation doesn't
specify one way or the other.
Thanks to Paulo Magalhães for report.
# minus whatever the user has set in his umask.
os.chmod(g.name, 0o664 & ~old_umask)
- # Overwrite the old database file with the new (gunzipped) one.
- shutil.move(g.name, dbfile)
+ # Overwrite the old database file with the new (gunzipped)
+ # one. The os.replace() method is preferable because it is
+ # atomic, but it will raise an OSError if the source and
+ # destination are on different filesystems. We fall back
+ # to shutil.move in that case.
+ try:
+ os.replace(g.name, dbfile)
+ except OSError:
+ shutil.move(g.name, dbfile)