@@ -239,7 +239,7 @@ async def unpin(self) -> None:
239
239
if pool :
240
240
await pool .checkin (self )
241
241
else :
242
- self .close_conn (ConnectionClosedReason .STALE )
242
+ await self .close_conn (ConnectionClosedReason .STALE )
243
243
244
244
def hello_cmd (self ) -> dict [str , Any ]:
245
245
# Handshake spec requires us to use OP_MSG+hello command for the
@@ -438,7 +438,7 @@ async def command(
438
438
raise
439
439
# Catch socket.error, KeyboardInterrupt, etc. and close ourselves.
440
440
except BaseException as error :
441
- self ._raise_connection_failure (error )
441
+ await self ._raise_connection_failure (error )
442
442
443
443
async def send_message (self , message : bytes , max_doc_size : int ) -> None :
444
444
"""Send a raw BSON message or raise ConnectionFailure.
@@ -454,7 +454,7 @@ async def send_message(self, message: bytes, max_doc_size: int) -> None:
454
454
try :
455
455
await async_sendall (self .conn .get_conn , message )
456
456
except BaseException as error :
457
- self ._raise_connection_failure (error )
457
+ await self ._raise_connection_failure (error )
458
458
459
459
async def receive_message (self , request_id : Optional [int ]) -> Union [_OpReply , _OpMsg ]:
460
460
"""Receive a raw BSON message or raise ConnectionFailure.
@@ -464,7 +464,7 @@ async def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _O
464
464
try :
465
465
return await async_receive_message (self , request_id , self .max_message_size )
466
466
except BaseException as error :
467
- self ._raise_connection_failure (error )
467
+ await self ._raise_connection_failure (error )
468
468
469
469
def _raise_if_not_writable (self , unacknowledged : bool ) -> None :
470
470
"""Raise NotPrimaryError on unacknowledged write if this socket is not
@@ -550,11 +550,11 @@ def validate_session(
550
550
"Can only use session with the AsyncMongoClient that started it"
551
551
)
552
552
553
- def close_conn (self , reason : Optional [str ]) -> None :
553
+ async def close_conn (self , reason : Optional [str ]) -> None :
554
554
"""Close this connection with a reason."""
555
555
if self .closed :
556
556
return
557
- self ._close_conn ()
557
+ await self ._close_conn ()
558
558
if reason :
559
559
if self .enabled_for_cmap :
560
560
assert self .listeners is not None
@@ -571,7 +571,7 @@ def close_conn(self, reason: Optional[str]) -> None:
571
571
error = reason ,
572
572
)
573
573
574
- def _close_conn (self ) -> None :
574
+ async def _close_conn (self ) -> None :
575
575
"""Close this connection."""
576
576
if self .closed :
577
577
return
@@ -580,7 +580,7 @@ def _close_conn(self) -> None:
580
580
# Note: We catch exceptions to avoid spurious errors on interpreter
581
581
# shutdown.
582
582
try :
583
- self .conn .close ()
583
+ await self .conn .close ()
584
584
except asyncio .CancelledError :
585
585
raise
586
586
except Exception : # noqa: S110
@@ -618,7 +618,7 @@ def idle_time_seconds(self) -> float:
618
618
"""Seconds since this socket was last checked into its pool."""
619
619
return time .monotonic () - self .last_checkin_time
620
620
621
- def _raise_connection_failure (self , error : BaseException ) -> NoReturn :
621
+ async def _raise_connection_failure (self , error : BaseException ) -> NoReturn :
622
622
# Catch *all* exceptions from socket methods and close the socket. In
623
623
# regular Python, socket operations only raise socket.error, even if
624
624
# the underlying cause was a Ctrl-C: a signal raised during socket.recv
@@ -638,7 +638,7 @@ def _raise_connection_failure(self, error: BaseException) -> NoReturn:
638
638
reason = None
639
639
else :
640
640
reason = ConnectionClosedReason .ERROR
641
- self .close_conn (reason )
641
+ await self .close_conn (reason )
642
642
# SSLError from PyOpenSSL inherits directly from Exception.
643
643
if isinstance (error , (IOError , OSError , SSLError )):
644
644
details = _get_timeout_details (self .opts )
@@ -864,7 +864,7 @@ async def _reset(
864
864
# publishing the PoolClearedEvent.
865
865
if close :
866
866
for conn in sockets :
867
- conn .close_conn (ConnectionClosedReason .POOL_CLOSED )
867
+ await conn .close_conn (ConnectionClosedReason .POOL_CLOSED )
868
868
if self .enabled_for_cmap :
869
869
assert listeners is not None
870
870
listeners .publish_pool_closed (self .address )
@@ -895,7 +895,7 @@ async def _reset(
895
895
serviceId = service_id ,
896
896
)
897
897
for conn in sockets :
898
- conn .close_conn (ConnectionClosedReason .STALE )
898
+ await conn .close_conn (ConnectionClosedReason .STALE )
899
899
900
900
async def update_is_writable (self , is_writable : Optional [bool ]) -> None :
901
901
"""Updates the is_writable attribute on all sockets currently in the
@@ -940,7 +940,7 @@ async def remove_stale_sockets(self, reference_generation: int) -> None:
940
940
and self .conns [- 1 ].idle_time_seconds () > self .opts .max_idle_time_seconds
941
941
):
942
942
conn = self .conns .pop ()
943
- conn .close_conn (ConnectionClosedReason .IDLE )
943
+ await conn .close_conn (ConnectionClosedReason .IDLE )
944
944
945
945
while True :
946
946
async with self .size_cond :
@@ -964,7 +964,7 @@ async def remove_stale_sockets(self, reference_generation: int) -> None:
964
964
# Close connection and return if the pool was reset during
965
965
# socket creation or while acquiring the pool lock.
966
966
if self .gen .get_overall () != reference_generation :
967
- conn .close_conn (ConnectionClosedReason .STALE )
967
+ await conn .close_conn (ConnectionClosedReason .STALE )
968
968
return
969
969
self .conns .appendleft (conn )
970
970
self .active_contexts .discard (conn .cancel_context )
@@ -1052,7 +1052,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
1052
1052
except BaseException :
1053
1053
async with self .lock :
1054
1054
self .active_contexts .discard (conn .cancel_context )
1055
- conn .close_conn (ConnectionClosedReason .ERROR )
1055
+ await conn .close_conn (ConnectionClosedReason .ERROR )
1056
1056
raise
1057
1057
1058
1058
return conn
@@ -1246,7 +1246,7 @@ async def _get_conn(
1246
1246
except IndexError :
1247
1247
self ._pending += 1
1248
1248
if conn : # We got a socket from the pool
1249
- if self ._perished (conn ):
1249
+ if await self ._perished (conn ):
1250
1250
conn = None
1251
1251
continue
1252
1252
else : # We need to create a new connection
@@ -1259,7 +1259,7 @@ async def _get_conn(
1259
1259
except BaseException :
1260
1260
if conn :
1261
1261
# We checked out a socket but authentication failed.
1262
- conn .close_conn (ConnectionClosedReason .ERROR )
1262
+ await conn .close_conn (ConnectionClosedReason .ERROR )
1263
1263
async with self .size_cond :
1264
1264
self .requests -= 1
1265
1265
if incremented :
@@ -1319,7 +1319,7 @@ async def checkin(self, conn: AsyncConnection) -> None:
1319
1319
await self .reset_without_pause ()
1320
1320
else :
1321
1321
if self .closed :
1322
- conn .close_conn (ConnectionClosedReason .POOL_CLOSED )
1322
+ await conn .close_conn (ConnectionClosedReason .POOL_CLOSED )
1323
1323
elif conn .closed :
1324
1324
# CMAP requires the closed event be emitted after the check in.
1325
1325
if self .enabled_for_cmap :
@@ -1343,7 +1343,7 @@ async def checkin(self, conn: AsyncConnection) -> None:
1343
1343
# Hold the lock to ensure this section does not race with
1344
1344
# Pool.reset().
1345
1345
if self .stale_generation (conn .generation , conn .service_id ):
1346
- conn .close_conn (ConnectionClosedReason .STALE )
1346
+ await conn .close_conn (ConnectionClosedReason .STALE )
1347
1347
else :
1348
1348
conn .update_last_checkin_time ()
1349
1349
conn .update_is_writable (bool (self .is_writable ))
@@ -1361,7 +1361,7 @@ async def checkin(self, conn: AsyncConnection) -> None:
1361
1361
self .operation_count -= 1
1362
1362
self .size_cond .notify ()
1363
1363
1364
- def _perished (self , conn : AsyncConnection ) -> bool :
1364
+ async def _perished (self , conn : AsyncConnection ) -> bool :
1365
1365
"""Return True and close the connection if it is "perished".
1366
1366
1367
1367
This side-effecty function checks if this socket has been idle for
@@ -1381,18 +1381,18 @@ def _perished(self, conn: AsyncConnection) -> bool:
1381
1381
self .opts .max_idle_time_seconds is not None
1382
1382
and idle_time_seconds > self .opts .max_idle_time_seconds
1383
1383
):
1384
- conn .close_conn (ConnectionClosedReason .IDLE )
1384
+ await conn .close_conn (ConnectionClosedReason .IDLE )
1385
1385
return True
1386
1386
1387
1387
if self ._check_interval_seconds is not None and (
1388
1388
self ._check_interval_seconds == 0 or idle_time_seconds > self ._check_interval_seconds
1389
1389
):
1390
1390
if conn .conn_closed ():
1391
- conn .close_conn (ConnectionClosedReason .ERROR )
1391
+ await conn .close_conn (ConnectionClosedReason .ERROR )
1392
1392
return True
1393
1393
1394
1394
if self .stale_generation (conn .generation , conn .service_id ):
1395
- conn .close_conn (ConnectionClosedReason .STALE )
1395
+ await conn .close_conn (ConnectionClosedReason .STALE )
1396
1396
return True
1397
1397
1398
1398
return False
@@ -1436,9 +1436,9 @@ def _raise_wait_queue_timeout(self, checkout_started_time: float) -> NoReturn:
1436
1436
f"maxPoolSize: { self .opts .max_pool_size } , timeout: { timeout } "
1437
1437
)
1438
1438
1439
- def __del__ (self ) -> None :
1440
- # Avoid ResourceWarnings in Python 3
1441
- # Close all sockets without calling reset() or close() because it is
1442
- # not safe to acquire a lock in __del__.
1443
- for conn in self .conns :
1444
- conn .close_conn (None )
1439
+ # def __del__(self) -> None:
1440
+ # # Avoid ResourceWarnings in Python 3
1441
+ # # Close all sockets without calling reset() or close() because it is
1442
+ # # not safe to acquire a lock in __del__.
1443
+ # for conn in self.conns:
1444
+ # conn.close_conn(None)
0 commit comments