Skip to content

Commit 0e03581

Browse files
committed
Fix async ssl handshake
1 parent 45b4045 commit 0e03581

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

pymongo/network_layer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ async def async_receive_data(
238238
raise socket.timeout("timed out")
239239
if read_task in done:
240240
return read_task.result()
241-
raise _OperationCancelled("operation cancelled")
241+
elif conn.cancel_context.cancelled:
242+
raise _OperationCancelled("operation cancelled")
243+
return read_task.result()
242244
finally:
243245
sock.settimeout(sock_timeout)
244246

pymongo/pyopenssl_context.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,15 @@ def __init__(
117117
self._is_async = is_async
118118

119119
def _call(self, call: Callable[..., _T], *args: Any, **kwargs: Any) -> _T:
120+
is_async = kwargs.pop("allow_async", True) and self._is_async
120121
timeout = self.gettimeout()
121122
if timeout:
122123
start = _time.monotonic()
123124
while True:
124125
try:
125126
return call(*args, **kwargs)
126127
except BLOCKING_IO_ERRORS as exc:
127-
if self._is_async:
128+
if is_async:
128129
raise exc
129130
# Check for closed socket.
130131
if self.fileno() == -1:
@@ -146,6 +147,7 @@ def _call(self, call: Callable[..., _T], *args: Any, **kwargs: Any) -> _T:
146147
continue
147148

148149
def do_handshake(self, *args: Any, **kwargs: Any) -> None:
150+
kwargs["allow_async"] = False
149151
return self._call(super().do_handshake, *args, **kwargs)
150152

151153
def recv(self, *args: Any, **kwargs: Any) -> bytes:

test/asynchronous/test_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,7 @@ def compression_settings(client):
17031703
# No error
17041704
await client.pymongo_test.test.find_one()
17051705

1706+
@async_client_context.require_sync
17061707
async def test_reset_during_update_pool(self):
17071708
client = await self.async_rs_or_single_client(minPoolSize=10)
17081709
await client.admin.command("ping")
@@ -1727,10 +1728,7 @@ async def _run(self):
17271728
await asyncio.sleep(0.001)
17281729

17291730
def run(self):
1730-
if _IS_SYNC:
1731-
self._run()
1732-
else:
1733-
asyncio.run(self._run())
1731+
self._run()
17341732

17351733
t = ResetPoolThread(pool)
17361734
t.start()

test/test_client.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,7 @@ def compression_settings(client):
16611661
# No error
16621662
client.pymongo_test.test.find_one()
16631663

1664+
@client_context.require_sync
16641665
def test_reset_during_update_pool(self):
16651666
client = self.rs_or_single_client(minPoolSize=10)
16661667
client.admin.command("ping")
@@ -1685,10 +1686,7 @@ def _run(self):
16851686
time.sleep(0.001)
16861687

16871688
def run(self):
1688-
if _IS_SYNC:
1689-
self._run()
1690-
else:
1691-
asyncio.run(self._run())
1689+
self._run()
16921690

16931691
t = ResetPoolThread(pool)
16941692
t.start()

0 commit comments

Comments
 (0)