[tor-commits] [stem/master] Replace `CombinedReentrantAndAsyncioLock` with the plain `asyncio.Lock`

atagar at torproject.org atagar at torproject.org
Thu Jul 16 01:29:00 UTC 2020


commit 6be7d88f9e6bf82e5ae20813e6294c6862ea58c6
Author: Illia Volochii <illia.volochii at gmail.com>
Date:   Sun May 24 02:29:32 2020 +0300

    Replace `CombinedReentrantAndAsyncioLock` with the plain `asyncio.Lock`
    
    `CombinedReentrantAndAsyncioLock` cannot be used in multiple threads anyway.
---
 stem/client/__init__.py |  3 ++-
 stem/control.py         |  4 ++--
 stem/util/__init__.py   | 29 -----------------------------
 3 files changed, 4 insertions(+), 32 deletions(-)

diff --git a/stem/client/__init__.py b/stem/client/__init__.py
index 8ea7b3c1..8c8da923 100644
--- a/stem/client/__init__.py
+++ b/stem/client/__init__.py
@@ -25,6 +25,7 @@ a wrapper for :class:`~stem.socket.RelaySocket`, much the same way as
     +- close - closes this circuit
 """
 
+import asyncio
 import hashlib
 
 import stem
@@ -70,7 +71,7 @@ class Relay(object):
     self.link_protocol = LinkProtocol(link_protocol)
     self._orport = orport
     self._orport_buffer = b''  # unread bytes
-    self._orport_lock = stem.util.CombinedReentrantAndAsyncioLock()
+    self._orport_lock = asyncio.Lock()
     self._circuits = {}  # type: Dict[int, stem.client.Circuit]
 
   @staticmethod
diff --git a/stem/control.py b/stem/control.py
index 293d4bd3..084976ad 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -623,7 +623,7 @@ class BaseController(_BaseControllerSocketMixin):
 
     self._asyncio_loop = asyncio.get_event_loop()
 
-    self._msg_lock = stem.util.CombinedReentrantAndAsyncioLock()
+    self._msg_lock = asyncio.Lock()
 
     self._status_listeners = []  # type: List[Tuple[Callable[[stem.control.BaseController, stem.control.State, float], None], bool]] # tuples of the form (callback, spawn_thread)
     self._status_listeners_lock = threading.RLock()
@@ -1062,7 +1062,7 @@ class AsyncController(BaseController):
     # mapping of event types to their listeners
 
     self._event_listeners = {}  # type: Dict[stem.control.EventType, List[Callable[[stem.response.events.Event], Union[None, Awaitable[None]]]]]
-    self._event_listeners_lock = stem.util.CombinedReentrantAndAsyncioLock()
+    self._event_listeners_lock = asyncio.Lock()
     self._enabled_features = []  # type: List[str]
 
     self._last_address_exc = None  # type: Optional[BaseException]
diff --git a/stem/util/__init__.py b/stem/util/__init__.py
index 7c53730c..a90aa7ac 100644
--- a/stem/util/__init__.py
+++ b/stem/util/__init__.py
@@ -10,7 +10,6 @@ import datetime
 import threading
 from concurrent.futures import Future
 
-from types import TracebackType
 from typing import Any, AsyncIterator, Iterator, Optional, Type, Union
 
 __all__ = [
@@ -145,34 +144,6 @@ def _hash_attr(obj: Any, *attributes: str, **kwargs: Any):
   return my_hash
 
 
-class CombinedReentrantAndAsyncioLock:
-  """
-  Lock that combines thread-safe reentrant and not thread-safe asyncio locks.
-  """
-
-  __slots__ = ('_r_lock', '_async_lock')
-
-  def __init__(self) -> None:
-    self._r_lock = threading.RLock()
-    self._async_lock = asyncio.Lock()
-
-  async def acquire(self) -> bool:
-    await self._async_lock.acquire()
-    self._r_lock.acquire()
-    return True
-
-  def release(self) -> None:
-    self._r_lock.release()
-    self._async_lock.release()
-
-  async def __aenter__(self) -> 'CombinedReentrantAndAsyncioLock':
-    await self.acquire()
-    return self
-
-  async def __aexit__(self, exit_type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> None:
-    self.release()
-
-
 class ThreadForWrappedAsyncClass(threading.Thread):
   def __init__(self, *args: Any, **kwargs: Any) -> None:
     super().__init__(*args, *kwargs)





More information about the tor-commits mailing list