PYTHON-5044 - Fix successive AsyncMongoClients on a single loop always ti… (#2065)

This commit is contained in:
Noah Stapp 2025-01-22 08:49:16 -05:00 committed by GitHub
parent 2235b8354c
commit f1af917894
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 22 deletions

View File

@ -267,18 +267,25 @@ async def async_receive_data(
else:
read_task = create_task(_async_receive(sock, length, loop)) # type: ignore[arg-type]
tasks = [read_task, cancellation_task]
done, pending = await asyncio.wait(
tasks, timeout=timeout, return_when=asyncio.FIRST_COMPLETED
)
for task in pending:
task.cancel()
if pending:
await asyncio.wait(pending)
if len(done) == 0:
raise socket.timeout("timed out")
if read_task in done:
return read_task.result()
raise _OperationCancelled("operation cancelled")
try:
done, pending = await asyncio.wait(
tasks, timeout=timeout, return_when=asyncio.FIRST_COMPLETED
)
for task in pending:
task.cancel()
if pending:
await asyncio.wait(pending)
if len(done) == 0:
raise socket.timeout("timed out")
if read_task in done:
return read_task.result()
raise _OperationCancelled("operation cancelled")
except asyncio.CancelledError:
for task in tasks:
task.cancel()
await asyncio.wait(tasks)
raise
finally:
sock.settimeout(sock_timeout)

View File

@ -78,14 +78,7 @@ class AsyncPeriodicExecutor:
async def join(self, timeout: Optional[int] = None) -> None:
if self._task is not None:
try:
await asyncio.wait_for(self._task, timeout=timeout) # type-ignore: [arg-type]
except asyncio.TimeoutError:
# Task timed out
pass
except asyncio.exceptions.CancelledError:
# Task was already finished, or not yet started.
raise
await asyncio.wait([self._task], timeout=timeout) # type-ignore: [arg-type]
def wake(self) -> None:
"""Execute the target function soon."""

View File

@ -22,7 +22,6 @@ sys.path[0:0] = [""]
from test.asynchronous import (
AsyncIntegrationTest,
async_client_context,
reset_client_context,
unittest,
)
from test.asynchronous.helpers import async_repl_set_step_down

View File

@ -22,7 +22,6 @@ sys.path[0:0] = [""]
from test import (
IntegrationTest,
client_context,
reset_client_context,
unittest,
)
from test.helpers import repl_set_step_down