Skip to content

Commit 7411be6

Browse files
committed
add logic for multiple pending connections
1 parent b2b4507 commit 7411be6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

pymongo/asynchronous/pool.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,10 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
10831083
Note that the pool does not keep a reference to the socket -- you
10841084
must call checkin() when you're done with it.
10851085
"""
1086+
# Mark whether we were in ready state before starting the process, to
1087+
# handle the case of multiple pending connections.
1088+
was_ready = self.state == PoolState.READY
1089+
10861090
async with self.lock:
10871091
conn_id = self.next_connection_id
10881092
self.next_connection_id += 1
@@ -1131,7 +1135,9 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
11311135
reason=_verbose_connection_error_reason(ConnectionClosedReason.ERROR),
11321136
error=ConnectionClosedReason.ERROR,
11331137
)
1134-
if context["has_created_socket"]:
1138+
if context["has_created_socket"] and not (
1139+
was_ready and self.state == PoolState.BACKOFF
1140+
):
11351141
await self._handle_connection_error(error, "handshake")
11361142
if isinstance(error, (IOError, OSError, *SSLErrors)):
11371143
details = _get_timeout_details(self.opts)
@@ -1158,7 +1164,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
11581164
except BaseException as e:
11591165
async with self.lock:
11601166
self.active_contexts.discard(conn.cancel_context)
1161-
if not has_completed_hello:
1167+
if not has_completed_hello and not (was_ready and self.state == PoolState.BACKOFF):
11621168
await self._handle_connection_error(e, "hello")
11631169
await conn.close_conn(ConnectionClosedReason.ERROR)
11641170
raise

pymongo/synchronous/pool.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,10 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
10811081
Note that the pool does not keep a reference to the socket -- you
10821082
must call checkin() when you're done with it.
10831083
"""
1084+
# Mark whether we were in ready state before starting the process, to
1085+
# handle the case of multiple pending connections.
1086+
was_ready = self.state == PoolState.READY
1087+
10841088
with self.lock:
10851089
conn_id = self.next_connection_id
10861090
self.next_connection_id += 1
@@ -1129,7 +1133,9 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
11291133
reason=_verbose_connection_error_reason(ConnectionClosedReason.ERROR),
11301134
error=ConnectionClosedReason.ERROR,
11311135
)
1132-
if context["has_created_socket"]:
1136+
if context["has_created_socket"] and not (
1137+
was_ready and self.state == PoolState.BACKOFF
1138+
):
11331139
self._handle_connection_error(error, "handshake")
11341140
if isinstance(error, (IOError, OSError, *SSLErrors)):
11351141
details = _get_timeout_details(self.opts)
@@ -1156,7 +1162,7 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
11561162
except BaseException as e:
11571163
with self.lock:
11581164
self.active_contexts.discard(conn.cancel_context)
1159-
if not has_completed_hello:
1165+
if not has_completed_hello and not (was_ready and self.state == PoolState.BACKOFF):
11601166
self._handle_connection_error(e, "hello")
11611167
conn.close_conn(ConnectionClosedReason.ERROR)
11621168
raise

0 commit comments

Comments
 (0)