Skip to content

Commit c883012

Browse files
authored
PYTHON-4703 MongoClient should default to connect=False on FaaS environments (mongodb#1844)
1 parent f2cd655 commit c883012

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

doc/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,20 @@ PyMongo 4.9 brings a number of improvements including:
4242
- Fixed a bug where PyMongo would raise ``InvalidBSON: date value out of range``
4343
when using :attr:`~bson.codec_options.DatetimeConversion.DATETIME_CLAMP` or
4444
:attr:`~bson.codec_options.DatetimeConversion.DATETIME_AUTO` with a non-UTC timezone.
45+
- The default value for ``connect`` in ``MongoClient`` is changed to ``False`` when running on
46+
unction-as-a-service (FaaS) like AWS Lambda, Google Cloud Functions, and Microsoft Azure Functions.
47+
On some FaaS systems, there is a ``fork()`` operation at function
48+
startup. By delaying the connection to the first operation, we avoid a deadlock. See
49+
`Is PyMongo Fork-Safe`_ for more information.
50+
4551

4652
Issues Resolved
4753
...............
4854

4955
See the `PyMongo 4.9 release notes in JIRA`_ for the list of resolved issues
5056
in this release.
5157

58+
.. _Is PyMongo Fork-Safe : https://www.mongodb.com/docs/languages/python/pymongo-driver/current/faq/#is-pymongo-fork-safe-
5259
.. _PyMongo 4.9 release notes in JIRA: https://jira.mongodb.org/secure/ReleaseNote.jspa?projectId=10004&version=39940
5360

5461

pymongo/asynchronous/mongo_client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@ def __init__(
720720
721721
.. versionchanged:: 4.7
722722
Deprecated parameter ``wTimeoutMS``, use :meth:`~pymongo.timeout`.
723+
724+
.. versionchanged:: 4.9
725+
The default value of ``connect`` is changed to ``False`` when running in a
726+
Function-as-a-service environment.
723727
"""
724728
doc_class = document_class or dict
725729
self._init_kwargs: dict[str, Any] = {
@@ -803,7 +807,10 @@ def __init__(
803807
if tz_aware is None:
804808
tz_aware = opts.get("tz_aware", False)
805809
if connect is None:
806-
connect = opts.get("connect", True)
810+
# Default to connect=True unless on a FaaS system, which might use fork.
811+
from pymongo.pool_options import _is_faas
812+
813+
connect = opts.get("connect", not _is_faas())
807814
keyword_opts["tz_aware"] = tz_aware
808815
keyword_opts["connect"] = connect
809816

pymongo/synchronous/mongo_client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ def __init__(
263263
aware (otherwise they will be naive)
264264
:param connect: If ``True`` (the default), immediately
265265
begin connecting to MongoDB in the background. Otherwise connect
266-
on the first operation.
266+
on the first operation. The default value is ``False`` when
267+
running in a Function-as-a-service environment.
267268
:param type_registry: instance of
268269
:class:`~bson.codec_options.TypeRegistry` to enable encoding
269270
and decoding of custom types.
@@ -719,6 +720,10 @@ def __init__(
719720
720721
.. versionchanged:: 4.7
721722
Deprecated parameter ``wTimeoutMS``, use :meth:`~pymongo.timeout`.
723+
724+
.. versionchanged:: 4.9
725+
The default value of ``connect`` is changed to ``False`` when running in a
726+
Function-as-a-service environment.
722727
"""
723728
doc_class = document_class or dict
724729
self._init_kwargs: dict[str, Any] = {
@@ -802,7 +807,10 @@ def __init__(
802807
if tz_aware is None:
803808
tz_aware = opts.get("tz_aware", False)
804809
if connect is None:
805-
connect = opts.get("connect", True)
810+
# Default to connect=True unless on a FaaS system, which might use fork.
811+
from pymongo.pool_options import _is_faas
812+
813+
connect = opts.get("connect", not _is_faas())
806814
keyword_opts["tz_aware"] = tz_aware
807815
keyword_opts["connect"] = connect
808816

test/lambda/mongodb/app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import json
1010
import os
11+
import warnings
1112

1213
from bson import has_c as has_bson_c
1314
from pymongo import MongoClient
@@ -18,6 +19,9 @@
1819
ServerHeartbeatListener,
1920
)
2021

22+
# Ensure there are no warnings raised in normal operation.
23+
warnings.simplefilter("error")
24+
2125
open_connections = 0
2226
heartbeat_count = 0
2327
streaming_heartbeat_count = 0

tools/synchro.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@
108108
docstring_replacements: dict[tuple[str, str], str] = {
109109
("MongoClient", "connect"): """If ``True`` (the default), immediately
110110
begin connecting to MongoDB in the background. Otherwise connect
111-
on the first operation.""",
111+
on the first operation. The default value is ``False`` when
112+
running in a Function-as-a-service environment.""",
112113
("Collection", "create"): """If ``True``, force collection
113114
creation even without options being set.""",
114115
("Collection", "session"): """A

0 commit comments

Comments
 (0)