Skip to content

Commit 966ad50

Browse files
committed
PYTHON-4782 Fix _ACondition initializations
1 parent 2aa02e0 commit 966ad50

File tree

6 files changed

+22
-13
lines changed

6 files changed

+22
-13
lines changed

pymongo/asynchronous/pool.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,8 @@ def __init__(
992992
# from the right side.
993993
self.conns: collections.deque = collections.deque()
994994
self.active_contexts: set[_CancellationContext] = set()
995-
self.lock = _ALock(_create_lock())
995+
_lock = _create_lock()
996+
self.lock = _ALock(_lock)
996997
self.active_sockets = 0
997998
# Monotonically increasing connection ID required for CMAP Events.
998999
self.next_connection_id = 1
@@ -1018,15 +1019,15 @@ def __init__(
10181019
# The first portion of the wait queue.
10191020
# Enforces: maxPoolSize
10201021
# Also used for: clearing the wait queue
1021-
self.size_cond = _ACondition(threading.Condition(self.lock)) # type: ignore[arg-type]
1022+
self.size_cond = _ACondition(threading.Condition(_lock))
10221023
self.requests = 0
10231024
self.max_pool_size = self.opts.max_pool_size
10241025
if not self.max_pool_size:
10251026
self.max_pool_size = float("inf")
10261027
# The second portion of the wait queue.
10271028
# Enforces: maxConnecting
10281029
# Also used for: clearing the wait queue
1029-
self._max_connecting_cond = _ACondition(threading.Condition(self.lock)) # type: ignore[arg-type]
1030+
self._max_connecting_cond = _ACondition(threading.Condition(_lock))
10301031
self._max_connecting = self.opts.max_connecting
10311032
self._pending = 0
10321033
self._client_id = client_id

pymongo/asynchronous/topology.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,9 @@ def __init__(self, topology_settings: TopologySettings):
170170
self._seed_addresses = list(topology_description.server_descriptions())
171171
self._opened = False
172172
self._closed = False
173-
self._lock = _ALock(_create_lock())
174-
self._condition = _ACondition(self._settings.condition_class(self._lock)) # type: ignore[arg-type]
173+
_lock = _create_lock()
174+
self._lock = _ALock(_lock)
175+
self._condition = _ACondition(self._settings.condition_class(_lock))
175176
self._servers: dict[_Address, Server] = {}
176177
self._pid: Optional[int] = None
177178
self._max_cluster_time: Optional[ClusterTime] = None

pymongo/lock.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def _release_locks() -> None:
4646
lock.release()
4747

4848

49+
# TODO: remove this.
50+
def _Lock(lock: threading.Lock) -> threading.Lock:
51+
return lock
52+
53+
4954
class _ALock:
5055
__slots__ = ("_lock",)
5156

pymongo/synchronous/pool.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
_CertificateError,
6363
)
6464
from pymongo.hello import Hello, HelloCompat
65-
from pymongo.lock import _create_lock
65+
from pymongo.lock import _create_lock, _Lock
6666
from pymongo.logger import (
6767
_CONNECTION_LOGGER,
6868
_ConnectionStatusMessage,
@@ -988,7 +988,8 @@ def __init__(
988988
# from the right side.
989989
self.conns: collections.deque = collections.deque()
990990
self.active_contexts: set[_CancellationContext] = set()
991-
self.lock = _create_lock()
991+
_lock = _create_lock()
992+
self.lock = _Lock(_lock)
992993
self.active_sockets = 0
993994
# Monotonically increasing connection ID required for CMAP Events.
994995
self.next_connection_id = 1
@@ -1014,15 +1015,15 @@ def __init__(
10141015
# The first portion of the wait queue.
10151016
# Enforces: maxPoolSize
10161017
# Also used for: clearing the wait queue
1017-
self.size_cond = threading.Condition(self.lock) # type: ignore[arg-type]
1018+
self.size_cond = threading.Condition(_lock)
10181019
self.requests = 0
10191020
self.max_pool_size = self.opts.max_pool_size
10201021
if not self.max_pool_size:
10211022
self.max_pool_size = float("inf")
10221023
# The second portion of the wait queue.
10231024
# Enforces: maxConnecting
10241025
# Also used for: clearing the wait queue
1025-
self._max_connecting_cond = threading.Condition(self.lock) # type: ignore[arg-type]
1026+
self._max_connecting_cond = threading.Condition(_lock)
10261027
self._max_connecting = self.opts.max_connecting
10271028
self._pending = 0
10281029
self._client_id = client_id

pymongo/synchronous/topology.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
WriteError,
4040
)
4141
from pymongo.hello import Hello
42-
from pymongo.lock import _create_lock
42+
from pymongo.lock import _create_lock, _Lock
4343
from pymongo.logger import (
4444
_SDAM_LOGGER,
4545
_SERVER_SELECTION_LOGGER,
@@ -170,8 +170,9 @@ def __init__(self, topology_settings: TopologySettings):
170170
self._seed_addresses = list(topology_description.server_descriptions())
171171
self._opened = False
172172
self._closed = False
173-
self._lock = _create_lock()
174-
self._condition = self._settings.condition_class(self._lock) # type: ignore[arg-type]
173+
_lock = _create_lock()
174+
self._lock = _Lock(_lock)
175+
self._condition = self._settings.condition_class(_lock)
175176
self._servers: dict[_Address, Server] = {}
176177
self._pid: Optional[int] = None
177178
self._max_cluster_time: Optional[ClusterTime] = None

tools/synchro.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def translate_locks(lines: list[str]) -> list[str]:
250250
lock_lines = [line for line in lines if "_Lock(" in line]
251251
cond_lines = [line for line in lines if "_Condition(" in line]
252252
for line in lock_lines:
253-
res = re.search(r"_Lock\(([^()]*\(\))\)", line)
253+
res = re.search(r"_Lock\(([^()]*\([^()]*\))\)", line)
254254
if res:
255255
old = res[0]
256256
index = lines.index(line)

0 commit comments

Comments
 (0)