Skip to content

Commit 5e34bdc

Browse files
committed
clean up logic
1 parent 7548f7b commit 5e34bdc

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

pymongo/asynchronous/pool.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,20 +1030,24 @@ async def remove_stale_sockets(self, reference_generation: int) -> None:
10301030
self.requests -= 1
10311031
self.size_cond.notify()
10321032

1033-
# Assume all non dns/tcp/timeout errors mean the server rejected the connection due to overload.
1034-
# if not errorDuringDnsTcp and not timeoutError:
1035-
# error._add_error_label("SystemOverloadedError")
1036-
1037-
def _handle_connection_error(self, error: Exception, phase: str) -> None:
1033+
def _handle_connection_error(self, error: BaseException, phase: str) -> None:
10381034
# Handle system overload condition for non-sdam pools.
1039-
# Look for an AutoReconnect error raise from a ConnectionResetError with
1040-
# errno == errno.ECONNRESET. If found, set backoff and add error labels.
1035+
# Look for an AutoReconnect error raised from a ConnectionResetError with
1036+
# errno == errno.ECONNRESET or raised from an OSError that we've created due to
1037+
# a closed connection.
1038+
# If found, set backoff and add error labels.
10411039
if self.is_sdam or type(error) != AutoReconnect or not len(error.errors):
10421040
return
1043-
if not isinstance(error.errors[0], ConnectionResetError):
1044-
return
1045-
if error.errors[0].errno != errno.ECONNRESET:
1046-
return
1041+
if hasattr(error.errors, "values"):
1042+
root_err = next(iter(error.errors.values()))
1043+
else:
1044+
root_err = error.errors[0]
1045+
if isinstance(root_err, ConnectionResetError):
1046+
if root_err.errno != errno.ECONNRESET:
1047+
return
1048+
elif isinstance(root_err, OSError):
1049+
if str(root_err) != "connection closed":
1050+
return
10471051
self._backoff += 1
10481052
error._add_error_label("SystemOverloadedError")
10491053
error._add_error_label("RetryableError")

pymongo/synchronous/pool.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,20 +1026,24 @@ def remove_stale_sockets(self, reference_generation: int) -> None:
10261026
self.requests -= 1
10271027
self.size_cond.notify()
10281028

1029-
# Assume all non dns/tcp/timeout errors mean the server rejected the connection due to overload.
1030-
# if not errorDuringDnsTcp and not timeoutError:
1031-
# error._add_error_label("SystemOverloadedError")
1032-
1033-
def _handle_connection_error(self, error: Exception, phase: str) -> None:
1029+
def _handle_connection_error(self, error: BaseException, phase: str) -> None:
10341030
# Handle system overload condition for non-sdam pools.
1035-
# Look for an AutoReconnect error raise from a ConnectionResetError with
1036-
# errno == errno.ECONNRESET. If found, set backoff and add error labels.
1031+
# Look for an AutoReconnect error raised from a ConnectionResetError with
1032+
# errno == errno.ECONNRESET or raised from an OSError that we've created due to
1033+
# a closed connection.
1034+
# If found, set backoff and add error labels.
10371035
if self.is_sdam or type(error) != AutoReconnect or not len(error.errors):
10381036
return
1039-
if not isinstance(error.errors[0], ConnectionResetError):
1040-
return
1041-
if error.errors[0].errno != errno.ECONNRESET:
1042-
return
1037+
if hasattr(error.errors, "values"):
1038+
root_err = next(iter(error.errors.values()))
1039+
else:
1040+
root_err = error.errors[0]
1041+
if isinstance(root_err, ConnectionResetError):
1042+
if root_err.errno != errno.ECONNRESET:
1043+
return
1044+
elif isinstance(root_err, OSError):
1045+
if str(root_err) != "connection closed":
1046+
return
10431047
self._backoff += 1
10441048
error._add_error_label("SystemOverloadedError")
10451049
error._add_error_label("RetryableError")

0 commit comments

Comments
 (0)