Skip to content

Commit 35a41e9

Browse files
committed
fix fork tests
1 parent 58a58a0 commit 35a41e9

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,11 @@ def __init__(
861861
srv_max_hosts=srv_max_hosts,
862862
server_monitoring_mode=options.server_monitoring_mode,
863863
)
864-
self._topology = Topology(self._topology_settings)
865864

866865
self._opened = False
867866
self._closed = False
867+
if not is_srv:
868+
self._init_background(first=True)
868869

869870
self._resolve_srv_info.update(
870871
{
@@ -977,19 +978,13 @@ def _resolve_srv(self) -> None:
977978
srv_max_hosts=srv_max_hosts,
978979
server_monitoring_mode=self._options.server_monitoring_mode,
979980
)
980-
self._topology = Topology(self._topology_settings)
981981

982982
if self._options.auto_encryption_opts:
983983
from pymongo.asynchronous.encryption import _Encrypter
984984

985985
self._encrypter = _Encrypter(self, self._options.auto_encryption_opts)
986986
self._timeout = self._options.timeout
987987

988-
if _HAS_REGISTER_AT_FORK:
989-
# Add this client to the list of weakly referenced items.
990-
# This will be used later if we fork.
991-
AsyncMongoClient._clients[self._topology._topology_id] = self
992-
993988
def _normalize_and_validate_options(
994989
self, opts: common._CaseInsensitiveDictionary, seeds: set[tuple[str, int | None]]
995990
) -> common._CaseInsensitiveDictionary:
@@ -1019,7 +1014,12 @@ async def aconnect(self) -> None:
10191014
"""Explicitly connect to MongoDB asynchronously instead of on the first operation."""
10201015
await self._get_topology()
10211016

1022-
def _init_background(self, old_pid: Optional[int] = None) -> None:
1017+
def _init_background(self, old_pid: Optional[int] = None, first=False) -> None:
1018+
self._topology = Topology(self._topology_settings)
1019+
if first and _HAS_REGISTER_AT_FORK:
1020+
# Add this client to the list of weakly referenced items.
1021+
# This will be used later if we fork.
1022+
AsyncMongoClient._clients[self._topology._topology_id] = self
10231023
# Seed the topology with the old one's pid so we can detect clients
10241024
# that are opened before a fork and used after.
10251025
self._topology._pid = old_pid
@@ -1679,7 +1679,7 @@ async def close(self) -> None:
16791679
.. versionchanged:: 3.6
16801680
End all server sessions created by this client.
16811681
"""
1682-
if self._opened:
1682+
if hasattr(self, "_topology"):
16831683
session_ids = self._topology.pop_all_sessions()
16841684
if session_ids:
16851685
await self._end_sessions(session_ids)
@@ -1712,7 +1712,7 @@ async def _get_topology(self) -> Topology:
17121712
if not self._opened:
17131713
if self._resolve_srv_info["is_srv"]:
17141714
self._resolve_srv()
1715-
self._init_background()
1715+
self._init_background(first=True)
17161716
await self._topology.open()
17171717
async with self._lock:
17181718
self._kill_cursors_executor.open()

pymongo/synchronous/mongo_client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,11 @@ def __init__(
859859
srv_max_hosts=srv_max_hosts,
860860
server_monitoring_mode=options.server_monitoring_mode,
861861
)
862-
self._topology = Topology(self._topology_settings)
863862

864863
self._opened = False
865864
self._closed = False
865+
if not is_srv:
866+
self._init_background(first=True)
866867

867868
self._resolve_srv_info.update(
868869
{
@@ -975,19 +976,13 @@ def _resolve_srv(self) -> None:
975976
srv_max_hosts=srv_max_hosts,
976977
server_monitoring_mode=self._options.server_monitoring_mode,
977978
)
978-
self._topology = Topology(self._topology_settings)
979979

980980
if self._options.auto_encryption_opts:
981981
from pymongo.synchronous.encryption import _Encrypter
982982

983983
self._encrypter = _Encrypter(self, self._options.auto_encryption_opts)
984984
self._timeout = self._options.timeout
985985

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-
MongoClient._clients[self._topology._topology_id] = self
990-
991986
def _normalize_and_validate_options(
992987
self, opts: common._CaseInsensitiveDictionary, seeds: set[tuple[str, int | None]]
993988
) -> common._CaseInsensitiveDictionary:
@@ -1017,7 +1012,12 @@ def _connect(self) -> None:
10171012
"""Explicitly connect to MongoDB synchronously instead of on the first operation."""
10181013
self._get_topology()
10191014

1020-
def _init_background(self, old_pid: Optional[int] = None) -> None:
1015+
def _init_background(self, old_pid: Optional[int] = None, first=False) -> None:
1016+
self._topology = Topology(self._topology_settings)
1017+
if first and _HAS_REGISTER_AT_FORK:
1018+
# Add this client to the list of weakly referenced items.
1019+
# This will be used later if we fork.
1020+
MongoClient._clients[self._topology._topology_id] = self
10211021
# Seed the topology with the old one's pid so we can detect clients
10221022
# that are opened before a fork and used after.
10231023
self._topology._pid = old_pid
@@ -1673,7 +1673,7 @@ def close(self) -> None:
16731673
.. versionchanged:: 3.6
16741674
End all server sessions created by this client.
16751675
"""
1676-
if self._opened:
1676+
if hasattr(self, "_topology"):
16771677
session_ids = self._topology.pop_all_sessions()
16781678
if session_ids:
16791679
self._end_sessions(session_ids)
@@ -1706,7 +1706,7 @@ def _get_topology(self) -> Topology:
17061706
if not self._opened:
17071707
if self._resolve_srv_info["is_srv"]:
17081708
self._resolve_srv()
1709-
self._init_background()
1709+
self._init_background(first=True)
17101710
self._topology.open()
17111711
with self._lock:
17121712
self._kill_cursors_executor.open()

0 commit comments

Comments
 (0)