From 885dc883504b2fc3989d29aa007d44d9d4240cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Tue, 16 Aug 2011 20:40:24 +0200 Subject: [PATCH] Remove store commit race condition --- darc/store.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/darc/store.py b/darc/store.py index f947e8001..0ca6a58c0 100644 --- a/darc/store.py +++ b/darc/store.py @@ -85,8 +85,9 @@ class Store(object): return msgpack.unpackb(fd.read()) def write_dict(self, filename, d): - with open(filename, 'wb') as fd: + with open(filename+'.tmp', 'wb') as fd: fd.write(msgpack.packb(d)) + os.rename(filename+'.tmp', filename) def delete_bands(self): delete_path = os.path.join(self.path, 'delete') @@ -95,7 +96,6 @@ class Store(object): for band in self.read_dict(delete_path): assert bands.pop(band, 0) == 0 self.io.delete_band(band, missing_ok=True) - os.unlink(delete_path) self.write_dict(os.path.join(self.path, 'band'), bands) def begin_txn(self): @@ -162,6 +162,10 @@ class Store(object): self.delete_bands() os.rename(os.path.join(self.path, 'txn.commit'), os.path.join(self.path, 'txn.tmp')) + + delete_path = os.path.join(self.path, 'delete') + if os.path.exists(delete_path): + os.unlink(delete_path) # Roll back active transaction txn_dir = os.path.join(self.path, 'txn.active') if os.path.exists(txn_dir):