From 8a42f170e70585e0c9d889444444399cf7b3cefe Mon Sep 17 00:00:00 2001 From: Constantinos Pigiotis Date: Wed, 23 Aug 2023 12:25:19 +0300 Subject: [PATCH] Return empty list if server_descriptions does not exist --- pymongo/topology_description.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pymongo/topology_description.py b/pymongo/topology_description.py index 21d47a531c..d539b0bf00 100644 --- a/pymongo/topology_description.py +++ b/pymongo/topology_description.py @@ -267,12 +267,18 @@ def srv_max_hosts(self) -> int: def _apply_local_threshold(self, selection: Optional[Selection]) -> List[ServerDescription]: if not selection: return [] + + server_descriptions = selection.server_descriptions + server_descriptions = [s for s in server_descriptions if s.round_trip_time != None] + if not server_descriptions: + return [] + # Round trip time in seconds. fastest = min(cast(float, s.round_trip_time) for s in selection.server_descriptions) threshold = self._topology_settings.local_threshold_ms / 1000.0 return [ s - for s in selection.server_descriptions + for s in server_descriptions if (cast(float, s.round_trip_time) - fastest) <= threshold ]