Skip to content

Commit aef265f

Browse files
author
Ahmed Magdy Ali
committed
gh-150505: document that Pickler and Unpickler are not thread-safe
Sharing Pickler or Unpickler instances across threads has no defined semantics. The internal state (memo, stream position, stack) forms a single coherent parse/serialize state that cannot be meaningfully used across threads simultaneously.
1 parent 6f465f9 commit aef265f

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

Doc/library/pickle.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ The :mod:`!pickle` module exports three classes, :class:`Pickler`,
316316
It is an error if *buffer_callback* is not ``None`` and *protocol* is
317317
``None`` or smaller than 5.
318318

319+
.. note::
320+
:class:`Pickler` objects are not thread-safe. Sharing a single
321+
:class:`Pickler` instance across multiple threads without external
322+
synchronization leads to undefined behavior. Use a separate instance
323+
per thread.
324+
319325
.. versionchanged:: 3.8
320326
The *buffer_callback* argument was added.
321327

@@ -430,6 +436,12 @@ The :mod:`!pickle` module exports three classes, :class:`Pickler`,
430436
an :ref:`out-of-band <pickle-oob>` buffer view. Such buffers have been
431437
given in order to the *buffer_callback* of a Pickler object.
432438

439+
.. note::
440+
:class:`Unpickler` objects are not thread-safe. Sharing a single
441+
:class:`Unpickler` instance across multiple threads without external
442+
synchronization leads to undefined behavior. Use a separate instance
443+
per thread.
444+
433445
.. versionchanged:: 3.8
434446
The *buffers* argument was added.
435447

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Document that :class:`pickle.Pickler` and :class:`pickle.Unpickler`
2+
instances are not thread-safe. Sharing instances across threads without
3+
external synchronization leads to undefined behavior.

0 commit comments

Comments
 (0)