Skip to content

Commit 69dde9e

Browse files
committed
PYTHON-2285 Only start kill cursors background thread if/when the client connects to the MongoDB deployment
1 parent aeb0bd7 commit 69dde9e

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

pymongo/mongo_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,6 @@ def __init__(
719719
direct_connection=options.direct_connection)
720720

721721
self._topology = Topology(self._topology_settings)
722-
if connect:
723-
self._topology.open()
724722

725723
def target():
726724
client = self_ref()
@@ -739,7 +737,9 @@ def target():
739737
# this closure. When the client is freed, stop the executor soon.
740738
self_ref = weakref.ref(self, executor.close)
741739
self._kill_cursors_executor = executor
742-
executor.open()
740+
741+
if connect:
742+
self._get_topology()
743743

744744
self._encrypter = None
745745
if self.__options.auto_encryption_opts:

test/test_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,24 @@ def test_close_stops_kill_cursors_thread(self):
790790
client.close()
791791
self.assertTrue(client._kill_cursors_executor._stopped)
792792

793+
def test_uri_connect_option(self):
794+
# Ensure that topology is not opened if connect=False.
795+
client = rs_client(connect=False)
796+
self.assertFalse(client._topology._opened)
797+
798+
# Ensure kill cursors thread has not been started.
799+
kc_thread = client._kill_cursors_executor._thread
800+
self.assertFalse(kc_thread and kc_thread.is_alive())
801+
802+
# Using the client should open topology and start the thread.
803+
client.admin.command('isMaster')
804+
self.assertTrue(client._topology._opened)
805+
kc_thread = client._kill_cursors_executor._thread
806+
self.assertTrue(kc_thread and kc_thread.is_alive())
807+
808+
# Tear down.
809+
client.close()
810+
793811
def test_close_does_not_open_servers(self):
794812
client = rs_client(connect=False)
795813
topology = client._topology

0 commit comments

Comments
 (0)