@@ -254,9 +254,9 @@ class PyMongoBaseProtocol(Protocol):
254254 def __init__ (self , timeout : Optional [float ] = None ):
255255 self .transport : Transport = None # type: ignore[assignment]
256256 self ._timeout = timeout
257- self ._closing_error = asyncio .get_running_loop ().create_future ()
258257 self ._closed = asyncio .get_running_loop ().create_future ()
259258 self ._connection_lost = False
259+ self ._closing_exception = None
260260
261261 def settimeout (self , timeout : float | None ) -> None :
262262 self ._timeout = timeout
@@ -270,11 +270,11 @@ def close(self, exc: Optional[Exception] = None) -> None:
270270 self .transport .abort ()
271271 self ._resolve_pending (exc )
272272 self ._connection_lost = True
273+ self ._closing_exception = exc
273274
274275 def connection_lost (self , exc : Optional [Exception ] = None ) -> None :
275- if exc is not None and not self ._closing_error .done ():
276- self ._closing_error .set_exception (exc )
277276 self ._resolve_pending (exc )
277+ self ._closing_exception = exc
278278 if not self ._closed .done ():
279279 self ._closed .set_result (None )
280280
@@ -325,7 +325,6 @@ def connection_made(self, transport: BaseTransport) -> None:
325325 """
326326 self .transport = transport # type: ignore[assignment]
327327 self .transport .set_write_buffer_limits (MAX_MESSAGE_SIZE , MAX_MESSAGE_SIZE )
328- super ().connection_made (self )
329328
330329 async def read (self , request_id : Optional [int ], max_message_size : int ) -> tuple [bytes , int ]:
331330 """Read a single MongoDB Wire Protocol message from this connection."""
@@ -339,8 +338,6 @@ async def read(self, request_id: Optional[int], max_message_size: int) -> tuple[
339338 if self ._done_messages :
340339 message = await self ._done_messages .popleft ()
341340 else :
342- if self .transport and self .transport .is_closing ():
343- return await self ._closing_error
344341 read_waiter = asyncio .get_running_loop ().create_future ()
345342 self ._pending_messages .append (read_waiter )
346343 try :
@@ -478,6 +475,7 @@ def _resolve_pending(self, exc: Optional[Exception] = None) -> None:
478475 else :
479476 msg .set_exception (exc )
480477 self ._done_messages .append (msg )
478+ self ._pending_messages .clear ()
481479
482480
483481class PyMongoKMSProtocol (PyMongoBaseProtocol ):
@@ -493,7 +491,6 @@ def connection_made(self, transport: BaseTransport) -> None:
493491 The transport argument is the transport representing the write side of the connection.
494492 """
495493 self .transport = transport # type: ignore[assignment]
496- super ().connection_made (self )
497494
498495 def data_received (self , data : bytes ) -> None :
499496 if self ._connection_lost :
0 commit comments