Skip to content

Commit 1a3efed

Browse files
committed
move init_background to only be called upon client connection
1 parent 8d48f44 commit 1a3efed

File tree

4 files changed

+60
-58
lines changed

4 files changed

+60
-58
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,10 @@ def __init__(
859859
server_monitoring_mode=options.server_monitoring_mode,
860860
)
861861

862+
self._topology = Topology(self._topology_settings)
863+
862864
self._opened = False
863865
self._closed = False
864-
self._init_background()
865866

866867
self._resolve_uri_info.update(
867868
{
@@ -878,17 +879,8 @@ def __init__(
878879
self._get_topology() # type: ignore[unused-coroutine]
879880

880881
self._encrypter = None
881-
if self._options.auto_encryption_opts:
882-
from pymongo.asynchronous.encryption import _Encrypter
883-
884-
self._encrypter = _Encrypter(self, self._options.auto_encryption_opts)
885882
self._timeout = self._options.timeout
886883

887-
if _HAS_REGISTER_AT_FORK:
888-
# Add this client to the list of weakly referenced items.
889-
# This will be used later if we fork.
890-
AsyncMongoClient._clients[self._topology._topology_id] = self
891-
892884
def _resolve_uri(self) -> None:
893885
keyword_opts = self._resolve_uri_info["keyword_opts"]
894886
seeds = set()
@@ -985,6 +977,17 @@ def _resolve_uri(self) -> None:
985977

986978
self._topology = Topology(self._topology_settings)
987979

980+
if self._options.auto_encryption_opts:
981+
from pymongo.asynchronous.encryption import _Encrypter
982+
983+
self._encrypter = _Encrypter(self, self._options.auto_encryption_opts)
984+
self._timeout = self._options.timeout
985+
986+
if _HAS_REGISTER_AT_FORK:
987+
# Add this client to the list of weakly referenced items.
988+
# This will be used later if we fork.
989+
AsyncMongoClient._clients[self._topology._topology_id] = self
990+
988991
def _normalize_and_validate_options(
989992
self, opts: common._CaseInsensitiveDictionary, seeds: set[tuple[str, int | None]]
990993
) -> common._CaseInsensitiveDictionary:
@@ -1672,19 +1675,20 @@ async def close(self) -> None:
16721675
await self._end_sessions(session_ids)
16731676
# Stop the periodic task thread and then send pending killCursor
16741677
# requests before closing the topology.
1675-
self._kill_cursors_executor.close()
1676-
await self._process_kill_cursors()
1677-
await self._topology.close()
1678-
if self._encrypter:
1679-
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
1680-
await self._encrypter.close()
1681-
self._closed = True
1682-
if not _IS_SYNC:
1683-
await asyncio.gather(
1684-
self._topology.cleanup_monitors(), # type: ignore[func-returns-value]
1685-
self._kill_cursors_executor.join(), # type: ignore[func-returns-value]
1686-
return_exceptions=True,
1687-
)
1678+
if self._opened:
1679+
self._kill_cursors_executor.close()
1680+
await self._process_kill_cursors()
1681+
await self._topology.close()
1682+
if self._encrypter:
1683+
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
1684+
await self._encrypter.close()
1685+
self._closed = True
1686+
if not _IS_SYNC:
1687+
await asyncio.gather(
1688+
self._topology.cleanup_monitors(), # type: ignore[func-returns-value]
1689+
self._kill_cursors_executor.join(), # type: ignore[func-returns-value]
1690+
return_exceptions=True,
1691+
)
16881692

16891693
if not _IS_SYNC:
16901694
# Add support for contextlib.aclosing.
@@ -1698,6 +1702,7 @@ async def _get_topology(self) -> Topology:
16981702
"""
16991703
if not self._opened:
17001704
self._resolve_uri()
1705+
self._init_background()
17011706
await self._topology.open()
17021707
async with self._lock:
17031708
self._kill_cursors_executor.open()

pymongo/synchronous/mongo_client.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -857,9 +857,10 @@ def __init__(
857857
server_monitoring_mode=options.server_monitoring_mode,
858858
)
859859

860+
self._topology = Topology(self._topology_settings)
861+
860862
self._opened = False
861863
self._closed = False
862-
self._init_background()
863864

864865
self._resolve_uri_info.update(
865866
{
@@ -876,17 +877,8 @@ def __init__(
876877
self._get_topology() # type: ignore[unused-coroutine]
877878

878879
self._encrypter = None
879-
if self._options.auto_encryption_opts:
880-
from pymongo.synchronous.encryption import _Encrypter
881-
882-
self._encrypter = _Encrypter(self, self._options.auto_encryption_opts)
883880
self._timeout = self._options.timeout
884881

885-
if _HAS_REGISTER_AT_FORK:
886-
# Add this client to the list of weakly referenced items.
887-
# This will be used later if we fork.
888-
MongoClient._clients[self._topology._topology_id] = self
889-
890882
def _resolve_uri(self) -> None:
891883
keyword_opts = self._resolve_uri_info["keyword_opts"]
892884
seeds = set()
@@ -983,6 +975,17 @@ def _resolve_uri(self) -> None:
983975

984976
self._topology = Topology(self._topology_settings)
985977

978+
if self._options.auto_encryption_opts:
979+
from pymongo.synchronous.encryption import _Encrypter
980+
981+
self._encrypter = _Encrypter(self, self._options.auto_encryption_opts)
982+
self._timeout = self._options.timeout
983+
984+
if _HAS_REGISTER_AT_FORK:
985+
# Add this client to the list of weakly referenced items.
986+
# This will be used later if we fork.
987+
MongoClient._clients[self._topology._topology_id] = self
988+
986989
def _normalize_and_validate_options(
987990
self, opts: common._CaseInsensitiveDictionary, seeds: set[tuple[str, int | None]]
988991
) -> common._CaseInsensitiveDictionary:
@@ -1666,19 +1669,20 @@ def close(self) -> None:
16661669
self._end_sessions(session_ids)
16671670
# Stop the periodic task thread and then send pending killCursor
16681671
# requests before closing the topology.
1669-
self._kill_cursors_executor.close()
1670-
self._process_kill_cursors()
1671-
self._topology.close()
1672-
if self._encrypter:
1673-
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
1674-
self._encrypter.close()
1675-
self._closed = True
1676-
if not _IS_SYNC:
1677-
asyncio.gather(
1678-
self._topology.cleanup_monitors(), # type: ignore[func-returns-value]
1679-
self._kill_cursors_executor.join(), # type: ignore[func-returns-value]
1680-
return_exceptions=True,
1681-
)
1672+
if self._opened:
1673+
self._kill_cursors_executor.close()
1674+
self._process_kill_cursors()
1675+
self._topology.close()
1676+
if self._encrypter:
1677+
# TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
1678+
self._encrypter.close()
1679+
self._closed = True
1680+
if not _IS_SYNC:
1681+
asyncio.gather(
1682+
self._topology.cleanup_monitors(), # type: ignore[func-returns-value]
1683+
self._kill_cursors_executor.join(), # type: ignore[func-returns-value]
1684+
return_exceptions=True,
1685+
)
16821686

16831687
if not _IS_SYNC:
16841688
# Add support for contextlib.closing.
@@ -1692,6 +1696,7 @@ def _get_topology(self) -> Topology:
16921696
"""
16931697
if not self._opened:
16941698
self._resolve_uri()
1699+
self._init_background()
16951700
self._topology.open()
16961701
with self._lock:
16971702
self._kill_cursors_executor.open()

test/asynchronous/test_client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,12 +1058,8 @@ async def test_uri_connect_option(self):
10581058
self.assertFalse(client._topology._opened)
10591059

10601060
# Ensure kill cursors thread has not been started.
1061-
if _IS_SYNC:
1062-
kc_thread = client._kill_cursors_executor._thread
1063-
self.assertFalse(kc_thread and kc_thread.is_alive())
1064-
else:
1065-
kc_task = client._kill_cursors_executor._task
1066-
self.assertFalse(kc_task and not kc_task.done())
1061+
# _kill_cursors_executor is initialized upon client connection
1062+
self.assertFalse(hasattr(client, "_kill_cursors_executor"))
10671063
# Using the client should open topology and start the thread.
10681064
await client.admin.command("ping")
10691065
self.assertTrue(client._topology._opened)

test/test_client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,12 +1031,8 @@ def test_uri_connect_option(self):
10311031
self.assertFalse(client._topology._opened)
10321032

10331033
# Ensure kill cursors thread has not been started.
1034-
if _IS_SYNC:
1035-
kc_thread = client._kill_cursors_executor._thread
1036-
self.assertFalse(kc_thread and kc_thread.is_alive())
1037-
else:
1038-
kc_task = client._kill_cursors_executor._task
1039-
self.assertFalse(kc_task and not kc_task.done())
1034+
# _kill_cursors_executor is initialized upon client connection
1035+
self.assertFalse(hasattr(client, "_kill_cursors_executor"))
10401036
# Using the client should open topology and start the thread.
10411037
client.admin.command("ping")
10421038
self.assertTrue(client._topology._opened)

0 commit comments

Comments
 (0)