Skip to content

Commit 62df559

Browse files
miss-islingtongpsheadAA-Turnerhugovk
authored
[3.12] gh-100228: Document the os.fork threads DeprecationWarning. (GH-109767) (#109773)
* gh-100228: Document the os.fork threads DeprecationWarning. (GH-109767) Document the `os.fork` posix threads detected `DeprecationWarning` in 3.12 What's New, os, multiprocessing, and concurrent.futures docs. Many reviews and doc cleanup edits by Adam & Hugo. 🥳 (cherry picked from commit 5e7ea95) Co-authored-by: Gregory P. Smith <[email protected]> Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]> * link to the discussion thread from whatsnew Include the link to the discussion in the what's new text per @malemberg's comment on. #109767 (i'll follow up with a PR to main to include this edit there as well) --------- Co-authored-by: Gregory P. Smith <[email protected]> Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
1 parent 4620762 commit 62df559

File tree

4 files changed

+60
-4
lines changed

4 files changed

+60
-4
lines changed

Doc/library/concurrent.futures.rst

+8
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ to a :class:`ProcessPoolExecutor` will result in deadlock.
293293
The *max_tasks_per_child* argument was added to allow users to
294294
control the lifetime of workers in the pool.
295295

296+
.. versionchanged:: 3.12
297+
On POSIX systems, if your application has multiple threads and the
298+
:mod:`multiprocessing` context uses the ``"fork"`` start method:
299+
The :func:`os.fork` function called internally to spawn workers may raise a
300+
:exc:`DeprecationWarning`. Pass a *mp_context* configured to use a
301+
different start method. See the :func:`os.fork` documentation for
302+
further explanation.
303+
296304
.. _processpoolexecutor-example:
297305

298306
ProcessPoolExecutor Example

Doc/library/multiprocessing.rst

+6
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ to start a process. These *start methods* are
131131
Code that requires *fork* should explicitly specify that via
132132
:func:`get_context` or :func:`set_start_method`.
133133

134+
.. versionchanged:: 3.12
135+
If Python is able to detect that your process has multiple threads, the
136+
:func:`os.fork` function that this start method calls internally will
137+
raise a :exc:`DeprecationWarning`. Use a different start method.
138+
See the :func:`os.fork` documentation for further explanation.
139+
134140
*forkserver*
135141
When the program starts and selects the *forkserver* start method,
136142
a server process is spawned. From then on, whenever a new process

Doc/library/os.rst

+32-4
Original file line numberDiff line numberDiff line change
@@ -4157,15 +4157,38 @@ written in Python, such as a mail server's external command delivery program.
41574157

41584158
.. audit-event:: os.fork "" os.fork
41594159

4160+
.. warning::
4161+
4162+
If you use TLS sockets in an application calling ``fork()``, see
4163+
the warning in the :mod:`ssl` documentation.
4164+
41604165
.. versionchanged:: 3.8
41614166
Calling ``fork()`` in a subinterpreter is no longer supported
41624167
(:exc:`RuntimeError` is raised).
41634168

4164-
.. warning::
4165-
4166-
See :mod:`ssl` for applications that use the SSL module with fork().
4169+
.. versionchanged:: 3.12
4170+
If Python is able to detect that your process has multiple
4171+
threads, :func:`os.fork` now raises a :exc:`DeprecationWarning`.
4172+
4173+
We chose to surface this as a warning, when detectable, to better
4174+
inform developers of a design problem that the POSIX platform
4175+
specifically notes as not supported. Even in code that
4176+
*appears* to work, it has never been safe to mix threading with
4177+
:func:`os.fork` on POSIX platforms. The CPython runtime itself has
4178+
always made API calls that are not safe for use in the child
4179+
process when threads existed in the parent (such as ``malloc`` and
4180+
``free``).
4181+
4182+
Users of macOS or users of libc or malloc implementations other
4183+
than those typically found in glibc to date are among those
4184+
already more likely to experience deadlocks running such code.
4185+
4186+
See `this discussion on fork being incompatible with threads
4187+
<https://discuss.python.org/t/33555>`_
4188+
for technical details of why we're surfacing this longstanding
4189+
platform compatibility problem to developers.
41674190

4168-
.. availability:: Unix, not Emscripten, not WASI.
4191+
.. availability:: POSIX, not Emscripten, not WASI.
41694192

41704193

41714194
.. function:: forkpty()
@@ -4178,6 +4201,11 @@ written in Python, such as a mail server's external command delivery program.
41784201

41794202
.. audit-event:: os.forkpty "" os.forkpty
41804203

4204+
.. versionchanged:: 3.12
4205+
If Python is able to detect that your process has multiple
4206+
threads, this now raises a :exc:`DeprecationWarning`. See the
4207+
longer explanation on :func:`os.fork`.
4208+
41814209
.. versionchanged:: 3.8
41824210
Calling ``forkpty()`` in a subinterpreter is no longer supported
41834211
(:exc:`RuntimeError` is raised).

Doc/whatsnew/3.12.rst

+14
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,20 @@ Deprecated
10651065
contain the creation time, which is also available in the new ``st_birthtime``
10661066
field. (Contributed by Steve Dower in :gh:`99726`.)
10671067

1068+
* :mod:`os`: On POSIX platforms, :func:`os.fork` can now raise a
1069+
:exc:`DeprecationWarning` when it can detect being called from a
1070+
multithreaded process. There has always been a fundamental incompatibility
1071+
with the POSIX platform when doing so. Even if such code *appeared* to work.
1072+
We added the warning to to raise awareness as issues encounted by code doing
1073+
this are becoming more frequent. See the :func:`os.fork` documentation for
1074+
more details along with `this discussion on fork being incompatible with threads
1075+
<https://discuss.python.org/t/33555>`_ for *why* we're now surfacing this
1076+
longstanding platform compatibility problem to developers.
1077+
1078+
When this warning appears due to usage of :mod:`multiprocessing` or
1079+
:mod:`concurrent.futures` the fix is to use a different
1080+
:mod:`multiprocessing` start method such as ``"spawn"`` or ``"forkserver"``.
1081+
10681082
* :mod:`shutil`: The *onerror* argument of :func:`shutil.rmtree` is deprecated as will be removed
10691083
in Python 3.14. Use *onexc* instead. (Contributed by Irit Katriel in :gh:`102828`.)
10701084

0 commit comments

Comments
 (0)