Use atomic file writes for server side cache

We've gotten reports of users receiving corrupt file blobs directly from the
server. The corruption doesn't enter the cache pool, and we don't get any
further reports of it, so I think it's a transient issue caused certain readers
reading the file before the writer has finished writing it.

Let's use atomic rename files to make this not happen.
This commit is contained in:
Durham Goode 2015-09-28 10:31:38 -07:00
parent 6e7195b8ef
commit e9a9bad998

View File

@ -199,13 +199,17 @@ def _loadfileblob(repo, cachepath, path, node):
dirname = os.path.dirname(filecachepath)
if not os.path.exists(dirname):
os.makedirs(dirname)
f = None
try:
with open(filecachepath, "w") as f:
f.write(text)
except IOError:
f = util.atomictempfile(filecachepath, "w")
f.write(text)
except (IOError, OSError):
# Don't abort if the user only has permission to read,
# and not write.
pass
finally:
if f:
f.close()
finally:
os.umask(oldumask)
else: