Skip to content

Commit 9a9f42b

Browse files
committed
PYTHON-2236 Reset the server pool only after marking the server Unknown
1 parent fbafa9c commit 9a9f42b

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

pymongo/monitor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,12 @@ def _check_with_retry(self):
142142
if self._publish:
143143
self._listeners.publish_server_heartbeat_failed(
144144
address, error_time, error)
145-
self._topology.reset_pool(address)
146145
default = ServerDescription(address, error=error)
146+
# Reset the server pool only after marking the server Unknown.
147+
self._topology.on_change(default)
148+
self._topology.reset_pool(address)
149+
self._avg_round_trip_time.reset()
147150
if not retry:
148-
self._avg_round_trip_time.reset()
149151
# Server type defaults to Unknown.
150152
return default
151153

test/test_client.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ def test_reconnect(self):
17671767
host='b:2', # Pass a secondary.
17681768
replicaSet='rs',
17691769
retryReads=False,
1770-
serverSelectionTimeoutMS=100,
1770+
serverSelectionTimeoutMS=1000,
17711771
)
17721772
self.addCleanup(c.close)
17731773

@@ -1782,9 +1782,6 @@ def test_reconnect(self):
17821782
# ServerSelectionTimeoutError or AutoReconnect (from
17831783
# MockPool.get_socket).
17841784
self.assertRaises(AutoReconnect, c.db.collection.find_one)
1785-
# The second attempt always raises ServerSelectionTimeoutError.
1786-
self.assertRaises(ServerSelectionTimeoutError,
1787-
c.db.collection.find_one)
17881785

17891786
# But it can reconnect.
17901787
c.revive_host('a:1')
@@ -1804,7 +1801,7 @@ def _test_network_error(self, operation_callback):
18041801
replicaSet='rs',
18051802
connect=False,
18061803
retryReads=False,
1807-
serverSelectionTimeoutMS=100)
1804+
serverSelectionTimeoutMS=1000)
18081805
self.addCleanup(c.close)
18091806

18101807
# Set host-specific information so we can test whether it is reset.

test/test_topology.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,18 +730,23 @@ def _check_with_socket(self, *args, **kwargs):
730730
if ismaster_count[0] in (1, 3):
731731
return IsMaster({'ok': 1, 'maxWireVersion': 6}), 0
732732
else:
733-
raise AutoReconnect('mock monitor error')
733+
raise AutoReconnect(
734+
'mock monitor error #%s' % (ismaster_count[0],))
734735

735736
t = create_mock_topology(monitor_class=TestMonitor)
736737
server = wait_for_master(t)
737738
self.assertEqual(1, ismaster_count[0])
738739
self.assertEqual(SERVER_TYPE.Standalone,
739740
server.description.server_type)
740741

741-
# Second ismaster call, then immediately the third.
742+
# Second ismaster call.
742743
t.request_check_all()
743-
self.assertEqual(3, ismaster_count[0])
744+
# The third ismaster call (the immediate retry) happens sometime soon
745+
# after the failed check triggered by request_check_all. Wait until
746+
# the server becomes known again.
747+
t.select_server(writable_server_selector, 0.250)
744748
self.assertEqual(SERVER_TYPE.Standalone, get_type(t, 'a'))
749+
self.assertEqual(3, ismaster_count[0])
745750

746751
def test_internal_monitor_error(self):
747752
exception = AssertionError('internal error')

0 commit comments

Comments
 (0)