Skip to content

Commit f1af917

Browse files
authored
PYTHON-5044 - Fix successive AsyncMongoClients on a single loop always ti… (#2065)
1 parent 2235b83 commit f1af917

4 files changed

+20
-22
lines changed

pymongo/network_layer.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,25 @@ async def async_receive_data(
267267
else:
268268
read_task = create_task(_async_receive(sock, length, loop)) # type: ignore[arg-type]
269269
tasks = [read_task, cancellation_task]
270-
done, pending = await asyncio.wait(
271-
tasks, timeout=timeout, return_when=asyncio.FIRST_COMPLETED
272-
)
273-
for task in pending:
274-
task.cancel()
275-
if pending:
276-
await asyncio.wait(pending)
277-
if len(done) == 0:
278-
raise socket.timeout("timed out")
279-
if read_task in done:
280-
return read_task.result()
281-
raise _OperationCancelled("operation cancelled")
270+
try:
271+
done, pending = await asyncio.wait(
272+
tasks, timeout=timeout, return_when=asyncio.FIRST_COMPLETED
273+
)
274+
for task in pending:
275+
task.cancel()
276+
if pending:
277+
await asyncio.wait(pending)
278+
if len(done) == 0:
279+
raise socket.timeout("timed out")
280+
if read_task in done:
281+
return read_task.result()
282+
raise _OperationCancelled("operation cancelled")
283+
except asyncio.CancelledError:
284+
for task in tasks:
285+
task.cancel()
286+
await asyncio.wait(tasks)
287+
raise
288+
282289
finally:
283290
sock.settimeout(sock_timeout)
284291

pymongo/periodic_executor.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,7 @@ def close(self, dummy: Any = None) -> None:
7878

7979
async def join(self, timeout: Optional[int] = None) -> None:
8080
if self._task is not None:
81-
try:
82-
await asyncio.wait_for(self._task, timeout=timeout) # type-ignore: [arg-type]
83-
except asyncio.TimeoutError:
84-
# Task timed out
85-
pass
86-
except asyncio.exceptions.CancelledError:
87-
# Task was already finished, or not yet started.
88-
raise
81+
await asyncio.wait([self._task], timeout=timeout) # type-ignore: [arg-type]
8982

9083
def wake(self) -> None:
9184
"""Execute the target function soon."""

test/asynchronous/test_connections_survive_primary_stepdown_spec.py

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from test.asynchronous import (
2323
AsyncIntegrationTest,
2424
async_client_context,
25-
reset_client_context,
2625
unittest,
2726
)
2827
from test.asynchronous.helpers import async_repl_set_step_down

test/test_connections_survive_primary_stepdown_spec.py

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from test import (
2323
IntegrationTest,
2424
client_context,
25-
reset_client_context,
2625
unittest,
2726
)
2827
from test.helpers import repl_set_step_down

0 commit comments

Comments
 (0)