Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-100228: Warn from os.fork() if other threads exist. #100229

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

gpshead
Copy link
Member

@gpshead gpshead commented Dec 14, 2022

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.

Not comprehensive, best effort.
@netlify
Copy link

netlify bot commented Dec 14, 2022

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

gpshead added 7 commits Dec 14, 2022
* 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.
I don't want the array of thread info but it may insist?
Py_XDECREF(threading_active);
return;
}
// Worst case if someone replaced threading._active or threading._limbo
Copy link
Member

@Yhg1s Yhg1s Dec 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not fatal that this happens, these shouldn't be asserts. Otherwise code that works fine will start aborting for the wrong reason when run with a debug build (or, for example, when imported in a build system that enables asserts when running tests cough).

Modules/posixmodule.c Outdated Show resolved Hide resolved
Modules/posixmodule.c Show resolved Hide resolved
Lib/test/test_os.py Outdated Show resolved Hide resolved
Modules/_testcapimodule.c Outdated Show resolved Hide resolved
Modules/posixmodule.c Show resolved Hide resolved
if (num_python_threads > 1) {
PyErr_WarnFormat(
PyExc_DeprecationWarning, 1,
"multi-threaded process, %s() may cause deadlocks.", name);
Copy link
Member

@Yhg1s Yhg1s Dec 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we point to more information to understand this issue somewhere, like a FAQ entry? I know we don't usually do this but this is somewhat of an esoteric issue, might be deep inside a library (e.g. something using multiprocessing), and we have been trying to improve error messages to be more helpful...

Copy link
Member Author

@gpshead gpshead Dec 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a good canonical link to propose right now. The other thing that often happens is that the error message winds up with a Google search result of a stackoverflow or similar live content user moderated question linking to the current best sources of info.

For multiprocessing, I intend to do the warning about start_method fork as the default being used. In that specific default-fork situation where multiprocessing will provide its own DeprecationWarning, it probably makes sense to have multiprocessing silence this one on its call.

gpshead and others added 10 commits Dec 15, 2022
Let the thread free and clear things as it ends after we unblock it.
It could hide more than we intend and wouldn't do what I wanted in child
processes that inherit the parents warnings state anyways.
…gtzMV.rst

Co-authored-by: T. Wouters <thomas@python.org>
Co-authored-by: T. Wouters <thomas@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants