From 5d5e2b199c132e8c937ede91350d1c7bee1a971f Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 27 Mar 2024 18:32:40 +0100 Subject: [PATCH] remove timeout support, it's not needed --- chatmaild/src/chatmaild/filedict.py | 25 ++++++++----------- .../src/chatmaild/tests/test_filedict.py | 12 --------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/chatmaild/src/chatmaild/filedict.py b/chatmaild/src/chatmaild/filedict.py index 006fa73..6e7fdef 100644 --- a/chatmaild/src/chatmaild/filedict.py +++ b/chatmaild/src/chatmaild/filedict.py @@ -8,26 +8,21 @@ from contextlib import contextmanager class FileDict: """Concurrency-safe multi-reader-single-writer Persistent Dict.""" - def __init__(self, path, timeout=5.0): + def __init__(self, path): self.path = path self.lock_path = path.with_name(path.name + ".lock") - self.timeout = timeout @contextmanager def modify(self): - try: - with filelock.FileLock(self.lock_path, timeout=self.timeout): - data = self.read() - yield data - write_path = self.path.with_suffix(".tmp") - with write_path.open("wb") as f: - marshal.dump(data, f) - os.rename(write_path, self.path) - except filelock.Timeout: - logging.warning("could not obtain lock, removing: %r", self.lock_path) - os.remove(self.lock_path) - with self.modify() as d: - yield d + # the OS will release the lock if the process dies, + # and the contextmanager will otherwise guarantee release + with filelock.FileLock(self.lock_path): + data = self.read() + yield data + write_path = self.path.with_suffix(".tmp") + with write_path.open("wb") as f: + marshal.dump(data, f) + os.rename(write_path, self.path) def read(self): try: diff --git a/chatmaild/src/chatmaild/tests/test_filedict.py b/chatmaild/src/chatmaild/tests/test_filedict.py index 81802cd..7bc31ea 100644 --- a/chatmaild/src/chatmaild/tests/test_filedict.py +++ b/chatmaild/src/chatmaild/tests/test_filedict.py @@ -12,18 +12,6 @@ def test_basic(tmp_path): assert new["456"] == 4.2 -def test_dying_lock(tmp_path, caplog): - fdict1 = FileDict(tmp_path.joinpath("metadata")) - fdict2 = FileDict(tmp_path.joinpath("metadata"), timeout=0.1) - with fdict1.modify() as d: - with fdict2.modify() as d2: - d2["1"] = "2" - assert "could not obtain" in caplog.records[0].msg - d["1"] = "3" - assert fdict1.read()["1"] == "3" - assert fdict2.read()["1"] == "3" - - def test_bad_marshal_file(tmp_path, caplog): fdict1 = FileDict(tmp_path.joinpath("metadata")) fdict1.path.write_bytes(b"l12k3l12k3l")