GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-100228: Warn from os.fork() if other threads exist. by gpshead · Pull Request #100229 · python/cpython

Conversation

@gpshead

@gpshead gpshead commented Dec 14, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Not comprehensive, best effort warning. There are cases when threads exist that this code currently does not detect.

Starting with a DeprecationWarning for now, it is less disruptive than something like RuntimeWarning and most likely to only be seen in people's CI tests - a good place to start with this messaging.

TODO:

  • doctests for multiprocessing failed with an assertion crash in 2854473 about the error being set unexpectedly on one later commit, why? did I fix that? I need to figure out how to run those and try to reproduce it. (could not reproduce, no way it could be related seen upon code inspection)
  • Issue: raise a Warning when os.fork() is called and the process has multiple threads #100228

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gpshead
Warn from os.fork() if other threads exist.
b5a53af
Not comprehensive, best effort.
@gpshead gpshead assigned Yhg1s Dec 14, 2022
@bedevere-bot bedevere-bot mentioned this pull request Dec 14, 2022
raise a Warning when os.fork() is called and the process has multiple threads #100228
Closed
@netlify

netlify Bot commented Dec 14, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

✅ Deploy Preview for python-cpython-preview canceled.

Name Link
🔨 Latest commit a91a4d1
🔍 Latest deploy log https://app.netlify.com/sites/python-cpython-preview/deploys/639aae96df45c9000879b05a
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot added the awaiting core review label Dec 14, 2022
gpshead added 7 commits December 14, 2022 06:14
@gpshead
fix existing tests for the new warning.
f4a1dda
@gpshead
static
bca33f9
@gpshead
&_Py_ID() for _active and _limbo.
d0eb2f6
@gpshead
news wording tweak
fb098f1
@gpshead
Add platform API calls on Linux & Mac, polish up.
67e2b75
* Only warn in the parent process.
* Add a test to test_os that a non-Python thread triggers the warning.
* Avoid the system calls to count threads if we've already warned once.
* The macOS API calls were written blind based on docs, will fix later
  if CI fails.
@gpshead
Merge branch 'warn/os.fork/threads' of github.com:gpshead/cpython int…
e418723
…o warn/os.fork/threads
@gpshead
Probably fix the macOS crashes.
2031888
I don't want the array of thread info but it may insist?
Yhg1s
Yhg1s reviewed Dec 14, 2022
View reviewed changes
Comment thread Modules/posixmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/posixmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/posixmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_os.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Library/2022-12-13-17-29-09.gh-issue-100228.bgtzMV.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_testcapimodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/posixmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/posixmodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

gpshead and others added 12 commits December 14, 2022 19:16
@gpshead
Fix test_os race condition.
546fac1
Let the thread free and clear things as it ends after we unblock it.
@gpshead
remove the already_warned logic.
a2e753e
It could hide more than we intend and wouldn't do what I wanted in child
processes that inherit the parents warnings state anyways.
@gpshead
Improve the warning message.
5655d59
@gpshead @Yhg1s
Update Misc/NEWS.d/next/Library/2022-12-13-17-29-09.gh-issue-100228.b…
7c9205e
…gtzMV.rst

Co-authored-by: T. Wouters <[email protected]>
@gpshead @Yhg1s
Update a comment
2854473
Co-authored-by: T. Wouters <[email protected]>
@gpshead
Clean up comments; add a final PyErr_Clear warning
1d3ec86
@gpshead
Improve test_os robustness.
0d95679
@gpshead
ignore expected warnings.
f6a7b6e
@gpshead
improve error message.
0ca9fc7
@gpshead
silence the warning in more tests.
a91a4d1
@gpshead
line length fixes, comments++, DECREF vs XDECREF
dc8fe08
@gpshead
Merge branch 'main' into warn/os.fork/threads
f10ece0
@gpshead
gpshead merged commit 894f2c3 into python:main Dec 29, 2022
@bedevere-bot bedevere-bot removed the awaiting core review label Dec 29, 2022
@gpshead
gpshead deleted the warn/os.fork/threads branch December 29, 2022 22:41
@gpshead gpshead mentioned this pull request Feb 6, 2023
Add an API to indicate if the process might have multiple threads #90651
Closed
@hroncok hroncok mentioned this pull request Jul 4, 2023
Python 3.12 DeprecationWarnings make test_main_thread_renamed_no_warning fail joblib/joblib#1478
Closed
@pmisik pmisik mentioned this pull request Dec 18, 2023
Python 3.12 deprecated os.fork, replace it with safer alternative buildbot/buildbot#7276
Open
@neutrinoceros neutrinoceros mentioned this pull request Feb 1, 2024
TST: yt/tests/test_load_archive.py::test_load_archive is failing yt-project/yt#4799
Closed
Xavier-Do added a commit to odoo-dev/odoo that referenced this pull request Apr 26, 2024
@Xavier-Do
[FIX] tests: avoid to fork in multithread process
f3e1c25
Since python 3.12 python/cpython#100229, fork
inside a multithreaded process will raise a warning.

We actually have another solution to get rid of the fork that was
implemented in more recent versions.
@Xavier-Do Xavier-Do mentioned this pull request Apr 26, 2024
[FIX] tests: avoid to fork in multithread process odoo/odoo#163562
Closed
robodoo pushed a commit to odoo/odoo that referenced this pull request Apr 26, 2024
@Xavier-Do
[FIX] tests: avoid to fork in multithread process
e356d18
Since python 3.12 python/cpython#100229, fork
inside a multithreaded process will raise a warning.

We actually have another solution to get rid of the fork that was
implemented in more recent versions.

closes #163562

Signed-off-by: Christophe Monniez (moc) <[email protected]>
@andersle andersle mentioned this pull request Sep 5, 2024
DeprecationWarning on Python 3.12 from os.fork() infretis/infretis#105
Closed
@ruro ruro mentioned this pull request Oct 25, 2024
DataLoader probably shouldn't use fork by default pytorch/pytorch#138957
Open
@TingDaoK TingDaoK mentioned this pull request Feb 25, 2025
Uploading S3 with boto3 (crt) fails silently in multiprocess scenario boto/boto3#4443
Closed
1 task
@zzzeek

zzzeek commented Mar 6, 2025

Copy link
Copy Markdown

is the use of fork() with a threaded parent going to be disallowed in a future release? That's what DeprecationWarning means. We have test code which makes use of fork to run some test cases in a separate process. There is no interaction with threads from the parent and the code is safe. We cannot use spawn or forkserver because we are running a Python function defined in the test itself and it cannot be serialized. We can silence this warning but it is not clear here, or even in the changelog given, if this functionality is going to be actually disallowed and I think it's critical to make that clear (also that the answer should be: "no" :) )

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gpshead

gpshead commented Mar 6, 2025

Copy link
Copy Markdown
Member Author

we can't disable it because people depend on it whether that's wise or not, but it is always the wrong thing to do. we're not going to create a "YouWillSufferMysteriousProblemsWarning" for such purposes. DeprecationWarning is accurate because it is something people used to do a lot but underlying platforms have significantly moved on such that things that relied on what they could get away with 15-20 years ago are no longer feasible in many normal environments today.

the deadlocks can come from the platform level and other low level libraries including the C stdlib and malloc and whatnot. it does not matter if you think you interact with the threads at all, just that they exist and are running is enough to lead to scenarios outside your control where problems happen randomly when forking.

for functions defined in the test themselves, the normal advise is to refactor the code so it is something pickle can handle (generally: not being defined on the fly)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@zzzeek

zzzeek commented Mar 6, 2025

Copy link
Copy Markdown

Where can I get your input on an issue I'm seeing where I dont actually see why this warning is being emitted? This has to do with the use of the pytest-xdist plugin which uses a tool called execnet. When using this plugin, if our own test uses fork, we get the warning. however, I've created a suite that runs through threading.enumerate() in the parent process, and in the child process - there is no additional thread spawned in either, there is only a single thread, the "main" thread.

output can be seen as follows - as you can see I'm not able to find any additional threads that are known to the Python interpreter. So....execnet is spawning threads outside of the interpreter? is that what has to be happening? is that a bug for them?

initialized: 1/1 workerINFO:main:THREAD FROM MAIN PROCESS 14974: <_MainThread(MainThread, started 140612028988288)>

1 worker [1 item]      
INFO:main:THREAD FROM CHILD PROCESS 14975 (parent: 14974): <_MainThread(MainThread, started 139972663720832)>

.
=========================================================================================== warnings summary ============================================================================================
test.py::TestWhatever::test_thing
  /usr/lib64/python3.12/multiprocessing/popen_fork.py:66: DeprecationWarning: This process (pid=14975) is multi-threaded, use of fork() may lead to deadlocks in the child.
    self.pid = os.fork()

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
===================================================================================== 1 passed, 1 warning in 10.17s =====================================================================================

I've looked at execnet a few times and so far it's opaque to me what it's doing or how pytest-xdist uses it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@zzzeek zzzeek mentioned this pull request Mar 6, 2025
pytest-xdist causes warnings to be emitted when a unit test uses os.fork() pytest-dev/pytest-xdist#1186
Open
@zzzeek

zzzeek commented Mar 6, 2025

Copy link
Copy Markdown

i've opened an issue at pytest-dev/pytest-xdist#1186

gpshead reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Apr 11, 2025
fix: added check for FriCAS version sagemath/sage#39796
Merged
Adds limit(expr, v, a) syntax sagemath/sage#39812
Merged
python3.12 : Warn from os.fork() if other threads exist (what to do?) pexpect/pexpect#817
Open
@ZeroIntensity ZeroIntensity mentioned this pull request Jun 12, 2025
-Werror doesn't error out on DeprecationWarning for os.fork() #135427
Closed
@Red-M Red-M mentioned this pull request Jul 16, 2025
Use and prefer os.posix_spawn() when available pexpect/ptyprocess#54
Open
@potiuk potiuk mentioned this pull request Jan 4, 2026
Suppress Warning in Executor for Supervisor on os.fork() apache/airflow#60087
Closed
@crusaderky crusaderky mentioned this pull request Jan 20, 2026
Threads+fork test fails on 3.15t h5py/h5py#2777
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Aug 1, 2026
Harden bounded evidence contracts for v0.3.4 yui-stingray/agent-guard#31
Merged
@coderabbitai coderabbitai Bot mentioned this pull request Aug 17, 2026
fix(lcm): reconcile lock remediation onto merged main batumilove/hermes-lcm#11
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Yhg1s Yhg1s Yhg1s left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@Yhg1s Yhg1s

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@gpshead @zzzeek @Yhg1s @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.