|
14 | 14 |
|
15 | 15 | from __future__ import annotations
|
16 | 16 |
|
| 17 | +import asyncio |
17 | 18 | import collections
|
18 | 19 | import contextlib
|
19 | 20 | import logging
|
|
75 | 76 | from pymongo.network_layer import AsyncNetworkingInterface, async_receive_message, async_sendall
|
76 | 77 | from pymongo.pool_options import PoolOptions
|
77 | 78 | from pymongo.pool_shared import (
|
| 79 | + SSLErrors, |
78 | 80 | _CancellationContext,
|
79 | 81 | _configured_protocol_interface,
|
80 | 82 | _get_timeout_details,
|
|
85 | 87 | from pymongo.server_api import _add_to_command
|
86 | 88 | from pymongo.server_type import SERVER_TYPE
|
87 | 89 | from pymongo.socket_checker import SocketChecker
|
88 |
| -from pymongo.ssl_support import SSLError |
89 | 90 |
|
90 | 91 | if TYPE_CHECKING:
|
91 | 92 | from bson import CodecOptions
|
@@ -637,7 +638,7 @@ async def _raise_connection_failure(self, error: BaseException) -> NoReturn:
|
637 | 638 | reason = ConnectionClosedReason.ERROR
|
638 | 639 | await self.close_conn(reason)
|
639 | 640 | # SSLError from PyOpenSSL inherits directly from Exception.
|
640 |
| - if isinstance(error, (IOError, OSError, SSLError)): |
| 641 | + if isinstance(error, (IOError, OSError, *SSLErrors)): |
641 | 642 | details = _get_timeout_details(self.opts)
|
642 | 643 | _raise_connection_failure(self.address, error, timeout_details=details)
|
643 | 644 | else:
|
@@ -860,8 +861,14 @@ async def _reset(
|
860 | 861 | # PoolClosedEvent but that reset() SHOULD close sockets *after*
|
861 | 862 | # publishing the PoolClearedEvent.
|
862 | 863 | if close:
|
863 |
| - for conn in sockets: |
864 |
| - await conn.close_conn(ConnectionClosedReason.POOL_CLOSED) |
| 864 | + if not _IS_SYNC: |
| 865 | + await asyncio.gather( |
| 866 | + *[conn.close_conn(ConnectionClosedReason.POOL_CLOSED) for conn in sockets], |
| 867 | + return_exceptions=True, |
| 868 | + ) |
| 869 | + else: |
| 870 | + for conn in sockets: |
| 871 | + await conn.close_conn(ConnectionClosedReason.POOL_CLOSED) |
865 | 872 | if self.enabled_for_cmap:
|
866 | 873 | assert listeners is not None
|
867 | 874 | listeners.publish_pool_closed(self.address)
|
@@ -891,8 +898,14 @@ async def _reset(
|
891 | 898 | serverPort=self.address[1],
|
892 | 899 | serviceId=service_id,
|
893 | 900 | )
|
894 |
| - for conn in sockets: |
895 |
| - await conn.close_conn(ConnectionClosedReason.STALE) |
| 901 | + if not _IS_SYNC: |
| 902 | + await asyncio.gather( |
| 903 | + *[conn.close_conn(ConnectionClosedReason.STALE) for conn in sockets], |
| 904 | + return_exceptions=True, |
| 905 | + ) |
| 906 | + else: |
| 907 | + for conn in sockets: |
| 908 | + await conn.close_conn(ConnectionClosedReason.STALE) |
896 | 909 |
|
897 | 910 | async def update_is_writable(self, is_writable: Optional[bool]) -> None:
|
898 | 911 | """Updates the is_writable attribute on all sockets currently in the
|
@@ -938,8 +951,14 @@ async def remove_stale_sockets(self, reference_generation: int) -> None:
|
938 | 951 | and self.conns[-1].idle_time_seconds() > self.opts.max_idle_time_seconds
|
939 | 952 | ):
|
940 | 953 | close_conns.append(self.conns.pop())
|
941 |
| - for conn in close_conns: |
942 |
| - await conn.close_conn(ConnectionClosedReason.IDLE) |
| 954 | + if not _IS_SYNC: |
| 955 | + await asyncio.gather( |
| 956 | + *[conn.close_conn(ConnectionClosedReason.IDLE) for conn in close_conns], |
| 957 | + return_exceptions=True, |
| 958 | + ) |
| 959 | + else: |
| 960 | + for conn in close_conns: |
| 961 | + await conn.close_conn(ConnectionClosedReason.IDLE) |
943 | 962 |
|
944 | 963 | while True:
|
945 | 964 | async with self.size_cond:
|
@@ -1033,7 +1052,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
|
1033 | 1052 | reason=_verbose_connection_error_reason(ConnectionClosedReason.ERROR),
|
1034 | 1053 | error=ConnectionClosedReason.ERROR,
|
1035 | 1054 | )
|
1036 |
| - if isinstance(error, (IOError, OSError, SSLError)): |
| 1055 | + if isinstance(error, (IOError, OSError, *SSLErrors)): |
1037 | 1056 | details = _get_timeout_details(self.opts)
|
1038 | 1057 | _raise_connection_failure(self.address, error, timeout_details=details)
|
1039 | 1058 |
|
|
0 commit comments