8000 gh-116522: Stop the world before fork() and during shutdown by colesbury · Pull Request #116607 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-116522: Stop the world before fork() and during shutdown #116607

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

Merged
merged 3 commits into from
Mar 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add comment and assertion to warn_about_fork_with_threads
  • Loading branch information
colesbury committed Mar 20, 2024
commit 4772a69439a484bb1c6c2f3a485c57427839aeb1
16 changes: 12 additions & 4 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7737,10 +7737,15 @@ os_register_at_fork_impl(PyObject *module, PyObject *before,
// running in the process. Best effort, silent if unable to count threads.
// Constraint: Quick. Never overcounts. Never leaves an error set.
//
// This code might do an import, thus acquiring the import lock, which
// PyOS_BeforeFork() also does. As this should only be called from
// the parent process, it is in the same thread so that works.
static void warn_about_fork_with_threads(const char* name) {
// This should only be called from the parent process after
// PyOS_AfterFork_Parent().
static void
warn_about_fork_with_threads(const char* name)
{
// It's not safe to issue the warning while the world is stopped, because
// other threads might be holding locks that we need, which would deadlock.
assert(!_PyRuntime.stoptheworld.world_stopped);

// TODO: Consider making an `os` module API to return the current number
// of threads in the process. That'd presumably use this platform code but
// raise an error rather than using the inaccurate fallback.
Expand Down Expand Up @@ -7866,6 +7871,7 @@ os_fork1_impl(PyObject *module)
} else {
/* parent: release the import lock. */
PyOS_AfterFork_Parent();
// After PyOS_AfterFork_Parent() starts the world to avoid deadlock.
warn_about_fork_with_threads("fork1");
}
if (pid == -1) {
Expand Down Expand Up @@ -7914,6 +7920,7 @@ os_fork_impl(PyObject *module)
} else {
/* parent: release the import lock. */
PyOS_AfterFork_Parent();
// After PyOS_AfterFork_Parent() starts the world to avoid deadlock.
warn_about_fork_with_threads("fork");
}
if (pid == -1) {
Expand Down Expand Up @@ -8745,6 +8752,7 @@ os_forkpty_impl(PyObject *module)
} else {
/* parent: release the import lock. */
PyOS_AfterFork_Parent();
// After PyOS_AfterFork_Parent() starts the world to avoid deadlock.
warn_about_fork_with_threads("forkpty");
}
if (pid == -1) {
Expand Down
0