Skip to content

Commit a1c33e0

Browse files
authored
PYTHON-3257 Fix "connection pool paused" errors in child after fork (#944)
1 parent cbab615 commit a1c33e0

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

pymongo/pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ def _get_socket(self):
14201420
# See test.test_client:TestClient.test_fork for an example of
14211421
# what could go wrong otherwise
14221422
if self.pid != os.getpid():
1423-
self.reset()
1423+
self.reset_without_pause()
14241424

14251425
if self.closed:
14261426
if self.enabled_for_cmap:
@@ -1526,7 +1526,7 @@ def return_socket(self, sock_info):
15261526
if self.enabled_for_cmap:
15271527
listeners.publish_connection_checked_in(self.address, sock_info.id)
15281528
if self.pid != os.getpid():
1529-
self.reset()
1529+
self.reset_without_pause()
15301530
else:
15311531
if self.closed:
15321532
sock_info.close_socket(ConnectionClosedReason.POOL_CLOSED)

pymongo/topology.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,24 @@ def open(self):
169169
forking.
170170
171171
"""
172+
pid = os.getpid()
172173
if self._pid is None:
173-
self._pid = os.getpid()
174-
else:
175-
if os.getpid() != self._pid:
176-
warnings.warn(
177-
"MongoClient opened before fork. Create MongoClient only "
178-
"after forking. See PyMongo's documentation for details: "
179-
"https://pymongo.readthedocs.io/en/stable/faq.html#"
180-
"is-pymongo-fork-safe"
181-
)
182-
with self._lock:
183-
# Reset the session pool to avoid duplicate sessions in
184-
# the child process.
185-
self._session_pool.reset()
174+
self._pid = pid
175+
elif pid != self._pid:
176+
self._pid = pid
177+
warnings.warn(
178+
"MongoClient opened before fork. Create MongoClient only "
179+
"after forking. See PyMongo's documentation for details: "
180+
"https://pymongo.readthedocs.io/en/stable/faq.html#"
181+
"is-pymongo-fork-safe"
182+
)
183+
with self._lock:
184+
# Close servers and clear the pools.
185+
for server in self._servers.values():
186+
server.close()
187+
# Reset the session pool to avoid duplicate sessions in
188+
# the child process.
189+
self._session_pool.reset()
186190

187191
with self._lock:
188192
self._ensure_opened()

0 commit comments

Comments
 (0)