prevent file corruption during json.dump

This commit is contained in:
Jakob Ketterl
2021-02-16 17:17:09 +01:00
parent 496e771e17
commit 3c0a26eaa8
3 changed files with 9 additions and 3 deletions

View File

@@ -185,8 +185,10 @@ class UserList(object):
usersFile = self._getUsersFile()
users = [u.toJson() for u in self.values()]
try:
# don't write directly to file to avoid corruption on exceptions
jsonContent = json.dumps(users, indent=4)
with open(usersFile, "w") as f:
json.dump(users, f, indent=4)
f.write(jsonContent)
except Exception:
logger.exception("error while writing users file %s", usersFile)
self.refresh()