diff options
author | Andrej Shadura <andrewsh@debian.org> | 2022-06-19 15:20:00 +0200 |
---|---|---|
committer | Andrej Shadura <andrewsh@debian.org> | 2022-06-19 15:21:39 +0200 |
commit | 734a8e556ce00029d9d7ab0fed73336d24fa91f3 (patch) | |
tree | b277733532b1b141d534133a4715a2fe765ab533 /synapse/storage/databases/main/lock.py | |
parent | 7a966d08c8403bcff00ac636d977097602501a69 (diff) | |
parent | 6dc64c92c6991f09910f3e6db368e6eeb4b1981e (diff) |
Update upstream source from tag 'upstream/1.61.0'
Update to upstream version '1.61.0'
with Debian dir 5b9bb60cc861cbccd0027b7db7acf826071dc6a0
Diffstat (limited to 'synapse/storage/databases/main/lock.py')
-rw-r--r-- | synapse/storage/databases/main/lock.py | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/synapse/storage/databases/main/lock.py b/synapse/storage/databases/main/lock.py index bedacaf0..2d7633fb 100644 --- a/synapse/storage/databases/main/lock.py +++ b/synapse/storage/databases/main/lock.py @@ -13,7 +13,7 @@ # limitations under the License. import logging from types import TracebackType -from typing import TYPE_CHECKING, Optional, Tuple, Type +from typing import TYPE_CHECKING, Optional, Set, Tuple, Type from weakref import WeakValueDictionary from twisted.internet.interfaces import IReactorCore @@ -84,6 +84,8 @@ class LockStore(SQLBaseStore): self._on_shutdown, ) + self._acquiring_locks: Set[Tuple[str, str]] = set() + @wrap_as_background_process("LockStore._on_shutdown") async def _on_shutdown(self) -> None: """Called when the server is shutting down""" @@ -103,6 +105,21 @@ class LockStore(SQLBaseStore): context manager if the lock is successfully acquired, which *must* be used (otherwise the lock will leak). """ + if (lock_name, lock_key) in self._acquiring_locks: + return None + try: + self._acquiring_locks.add((lock_name, lock_key)) + return await self._try_acquire_lock(lock_name, lock_key) + finally: + self._acquiring_locks.discard((lock_name, lock_key)) + + async def _try_acquire_lock( + self, lock_name: str, lock_key: str + ) -> Optional["Lock"]: + """Try to acquire a lock for the given name/key. Will return an async + context manager if the lock is successfully acquired, which *must* be + used (otherwise the lock will leak). + """ # Check if this process has taken out a lock and if it's still valid. lock = self._live_tokens.get((lock_name, lock_key)) |