mirror of
https://github.com/markqvist/NomadNet.git
synced 2026-10-25 18:40:13 -04:00
RRC: serialize logfile writes to guarantee atomicity on non-*nix systems
This commit is contained in:
parent
f919b647c3
commit
b806bd7089
1 changed files with 10 additions and 4 deletions
|
|
@ -240,6 +240,9 @@ class RRCHub:
|
|||
self.auto_who = False
|
||||
|
||||
self._lock = threading.RLock()
|
||||
# we need this lock to serialize writes on non-*nix systems where O_APPEND
|
||||
# write()'s apparently aren't guaranteed to be atomic
|
||||
self._history_io_lock = threading.Lock()
|
||||
self._resource_expectations = {}
|
||||
self._sent_ids = deque(maxlen=256)
|
||||
|
||||
|
|
@ -699,8 +702,10 @@ class RRCHub:
|
|||
try:
|
||||
self.manager._ensure_history_dir(self)
|
||||
path = self.manager._history_path(self, room)
|
||||
with open(path, "ab") as f:
|
||||
f.write(cbor.encode(self._entry_for(msg)))
|
||||
payload = cbor.encode(self._entry_for(msg))
|
||||
with self._history_io_lock:
|
||||
with open(path, "ab") as f:
|
||||
f.write(payload)
|
||||
self._history_write_failed = False
|
||||
except Exception as e:
|
||||
if not self._history_write_failed:
|
||||
|
|
@ -712,8 +717,9 @@ class RRCHub:
|
|||
return
|
||||
path = self.manager._history_path(self, room)
|
||||
try:
|
||||
if os.path.isfile(path):
|
||||
os.unlink(path)
|
||||
with self._history_io_lock:
|
||||
if os.path.isfile(path):
|
||||
os.unlink(path)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue