Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -613,11 +613,16 @@ PyOS_BeforeFork(void) | ||
| run_at_forkers(interp->before_forkers, 1); | ||
| _PyImport_AcquireLock(interp); | ||
| _PyEval_StopTheWorldAll(&_PyRuntime); | ||
| HEAD_LOCK(&_PyRuntime); | ||
| } | ||
| void | ||
| PyOS_AfterFork_Parent(void) | ||
| { | ||
| HEAD_UNLOCK(&_PyRuntime); | ||
| _PyEval_StartTheWorldAll(&_PyRuntime); | ||
| PyInterpreterState *interp = _PyInterpreterState_GET(); | ||
| if (_PyImport_ReleaseLock(interp) <= 0) { | ||
| Py_FatalError("failed releasing import lock after fork"); | ||
| @@ -632,6 +637,7 @@ PyOS_AfterFork_Child(void) | ||
| PyStatus status; | ||
| _PyRuntimeState *runtime = &_PyRuntime; | ||
| // re-creates runtime->interpreters.mutex (HEAD_UNLOCK) | ||
| status = _PyRuntimeState_ReInitThreads(runtime); | ||
| if (_PyStatus_EXCEPTION(status)) { | ||
| goto fatal_error; | ||
| @@ -7731,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. | ||
| @@ -7858,9 +7869,10 @@ os_fork1_impl(PyObject *module) | ||
| /* child: this clobbers and resets the import lock. */ | ||
| PyOS_AfterFork_Child(); | ||
| } else { | ||
| warn_about_fork_with_threads("fork1"); | ||
| /* parent: release the import lock. */ | ||
| PyOS_AfterFork_Parent(); | ||
| // After PyOS_AfterFork_Parent() starts the world to avoid deadlock. | ||
| warn_about_fork_with_threads("fork1"); | ||
gpshead marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| if (pid == -1) { | ||
| errno = saved_errno; | ||
| @@ -7906,9 +7918,10 @@ os_fork_impl(PyObject *module) | ||
| /* child: this clobbers and resets the import lock. */ | ||
| PyOS_AfterFork_Child(); | ||
| } else { | ||
| warn_about_fork_with_threads("fork"); | ||
| /* 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) { | ||
| errno = saved_errno; | ||
| @@ -8737,9 +8750,10 @@ os_forkpty_impl(PyObject *module) | ||
| /* child: this clobbers and resets the import lock. */ | ||
| PyOS_AfterFork_Child(); | ||
| } else { | ||
| warn_about_fork_with_threads("forkpty"); | ||
| /* 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) { | ||
| return posix_error(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1911,6 +1911,9 @@ Py_FinalizeEx(void) | ||
| int malloc_stats = tstate->interp->config.malloc_stats; | ||
| #endif | ||
| /* Ensure that remaining threads are detached */ | ||
| _PyEval_StopTheWorldAll(runtime); | ||
gpshead marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /* Remaining daemon threads will automatically exit | ||
| when they attempt to take the GIL (ex: PyEval_RestoreThread()). */ | ||
| _PyInterpreterState_SetFinalizing(tstate->interp, tstate); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Uh oh!
There was an error while loading. Please reload this page.