Skip to content

Commit 3c1dd61

Browse files
authored
PYTHON-2256 Document that a ClientSession cannot be used for multiple operations concurrently (#460)
1 parent e3f40c3 commit 3c1dd61

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pymongo/client_session.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,17 @@ def _within_time_limit(start_time):
336336

337337

338338
class ClientSession(object):
339-
"""A session for ordering sequential operations."""
339+
"""A session for ordering sequential operations.
340+
341+
:class:`ClientSession` instances are **not thread-safe or fork-safe**.
342+
They can only be used by one thread or process at a time. A single
343+
:class:`ClientSession` cannot be used to run multiple operations
344+
concurrently.
345+
346+
Should not be initialized directly by application developers - to create a
347+
:class:`ClientSession`, call
348+
:meth:`~pymongo.mongo_client.MongoClient.start_session`.
349+
"""
340350
def __init__(self, client, server_session, options, authset, implicit):
341351
# A MongoClient, a _ServerSession, a SessionOptions, and a set.
342352
self._client = client
@@ -461,6 +471,10 @@ def callback(session, custom_arg, custom_kwarg=None):
461471
however, ``with_transaction`` will return without taking further
462472
action.
463473
474+
:class:`ClientSession` instances are **not thread-safe or fork-safe**.
475+
Consequently, the ``callback`` must not attempt to execute multiple
476+
operations concurrently.
477+
464478
When ``callback`` raises an exception, ``with_transaction``
465479
automatically aborts the current transaction. When ``callback`` or
466480
:meth:`~ClientSession.commit_transaction` raises an exception that

pymongo/mongo_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,10 @@ def start_session(self,
17791779
deprecated method :meth:`~pymongo.database.Database.authenticate`.
17801780
17811781
A :class:`~pymongo.client_session.ClientSession` may only be used with
1782-
the MongoClient that started it.
1782+
the MongoClient that started it. :class:`ClientSession` instances are
1783+
**not thread-safe or fork-safe**. They can only be used by one thread
1784+
or process at a time. A single :class:`ClientSession` cannot be used
1785+
to run multiple operations concurrently.
17831786
17841787
:Returns:
17851788
An instance of :class:`~pymongo.client_session.ClientSession`.

0 commit comments

Comments
 (0)