Skip to content

Commit 6b90d0f

Browse files
committed
Merge remote-tracking branch 'upstream/master' into docsp-46362-toc-reorg
2 parents cafde51 + 60bd545 commit 6b90d0f

File tree

13 files changed

+695
-341
lines changed

13 files changed

+695
-341
lines changed

source/connect/connection-targets.txt

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Choose a Connection Target
99
:values: reference
1010

1111
.. meta::
12-
:keywords: connection string, URI, server, settings, client, load balancing
12+
:keywords: connection string, URI, server, settings, client, load balancing, srv, dns
1313

1414
.. contents:: On this page
1515
:local:
@@ -151,11 +151,60 @@ option to ``True``. You can do this in two ways: by passing an argument to the
151151
"directConnection=true")
152152
client = MongoClient(uri)
153153

154+
DNS Service Discovery
155+
---------------------
156+
157+
To use DNS service discovery to look up the DNS SRV record of the service you're connecting to,
158+
specify the SRV connection format in your connection string. Additionally, if you enable
159+
the SRV connection format, {+driver-short+} automatically re-scans for new hosts without
160+
having to change the client configuration.
161+
162+
The following code shows a connection string that uses the SRV connection format:
163+
164+
.. code-block:: python
165+
166+
uri = "mongodb+srv://<hostname>/"
167+
168+
To learn more about the SRV connection format, see the :manual:`SRV Connection Format </reference/connection-string/#std-label-connections-dns-seedlist>`
169+
entry in the {+mdb-server+} manual.
170+
154171
Troubleshooting
155172
---------------
156173

157174
.. include:: /includes/troubleshooting/connection-targets.rst
158175

176+
Timeout When Accessing MongoDB from {+driver-short+} with Tunneling
177+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178+
179+
If you try to connect to a MongoDB replica set over an SSH tunnel, you
180+
receive the following error:
181+
182+
.. code-block:: python
183+
184+
File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1560, in count
185+
return self._count(cmd, collation, session)
186+
File "/Library/Python/2.7/site-packages/pymongo/collection.py", line 1504, in _count
187+
with self._socket_for_reads() as (connection, slave_ok):
188+
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
189+
return self.gen.next()
190+
File "/Library/Python/2.7/site-packages/pymongo/mongo_client.py", line 982, in _socket_for_reads
191+
server = topology.select_server(read_preference)
192+
File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 224, in select_server
193+
address))
194+
File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 183, in select_servers
195+
selector, server_timeout, address)
196+
File "/Library/Python/2.7/site-packages/pymongo/topology.py", line 199, in _select_servers_loop
197+
self._error_message(selector))
198+
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: timed out
199+
200+
This occurs because {+driver-short+} discovers replica set members by using the response
201+
from the ``isMaster`` command, which contains the addresses and ports of the other
202+
replica set members. However, you can't access these addresses and ports through the SSH
203+
tunnel.
204+
205+
Instead, you can connect directly to a single MongoDB node by using the
206+
``directConnection=True`` option with SSH tunneling.
207+
159208
API Documentation
160209
-----------------
161210

source/connect/mongoclient.txt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ child process.
199199
a high probability of deadlock among ``MongoClient`` instances in the child process.
200200
{+driver-short+} tries to issue a warning if this deadlock might occur.
201201

202+
For more information about deadlock in forked processes, see
203+
:ref:`pymongo-fork-deadlock`.
204+
202205
Multiprocessing
203206
~~~~~~~~~~~~~~~
204207

@@ -245,6 +248,70 @@ create the ``MongoClient`` object, as shown in the following example:
245248
from typing import Any, Dict
246249
client: MongoClient[Dict[str, Any]] = MongoClient()
247250

251+
Troubleshooting
252+
---------------
253+
254+
MongoClient Fails ConfigurationError
255+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
256+
257+
Providing invalid keyword argument names causes the driver to raise this error.
258+
259+
Ensure that the keyword arguments that you specify exist and are
260+
spelled correctly.
261+
262+
.. _pymongo-fork-deadlock:
263+
264+
Forking a Process Causes a Deadlock
265+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
266+
267+
A ``MongoClient`` instance spawns multiple threads to run background tasks, such as
268+
monitoring connected servers. These threads share state that is protected by instances
269+
of the ``threading.Lock`` class, which are themselves
270+
`not fork-safe <http://bugs.python.org/issue6721>`__.
271+
{+driver-short+} is subject to the same limitations as any other multithreaded
272+
code that uses the ``threading.Lock`` class, or any mutexes.
273+
274+
One of these limitations is that the locks become useless after calling the
275+
``fork()`` method. When ``fork()`` executes, the driver copies all the parent process's locks to
276+
the child process in the same state as they were in the parent. If they are
277+
locked in the parent process, they are also locked in the child process. The child process
278+
created by ``fork()`` has only one thread, so any locks created by
279+
other threads in the parent process are never released in the child process.
280+
The next time the child process attempts to acquire one of these locks, deadlock occurs.
281+
282+
Starting in {+driver-short+} version 4.3, after you call the ``os.fork()`` method, the
283+
driver uses the ``os.register_at_fork()`` method to reset its locks and other shared state
284+
in the child process. Although this reduces the likelihood of a deadlock,
285+
{+driver-short+} depends
286+
on libraries that aren't fork-safe in multithreaded applications, including
287+
`OpenSSL <https://github.com/openssl/openssl/issues/19066>`__ and
288+
`getaddrinfo(3). <https://man7.org/linux/man-pages/man3/gai_strerror.3.html>`__
289+
Therefore, a deadlock can still occur.
290+
291+
The Linux manual page for `fork(2) <https://man7.org/linux/man-pages/man2/fork.2.html>`__
292+
also imposes the following restriction:
293+
294+
.. blockquote::
295+
296+
After a ``fork()`` in a multithreaded program, the child can
297+
safely call only async-signal-safe functions (see
298+
`signal-safety(7) <https://man7.org/linux/man-pages/man7/signal-safety.7.html>`__)
299+
until such time as it calls
300+
`execve(2) <https://man7.org/linux/man-pages/man2/execve.2.html>`__.
301+
302+
Because {+driver-short+} relies on functions that are *not*
303+
async-signal-safe, it can cause deadlocks or crashes when running in a child
304+
process.
305+
306+
.. tip::
307+
308+
For an example of a deadlock in a child process, see
309+
`PYTHON-3406 <https://jira.mongodb.org/browse/PYTHON-3406>`__ in Jira.
310+
311+
For more information about the problems caused by Python locks in
312+
multithreaded contexts with ``fork()``, see `Issue 6721 <http://bugs.python.org/issue6721>`__
313+
in the Python Issue Tracker.
314+
248315
API Documentation
249316
-----------------
250317

0 commit comments

Comments
 (0)