Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-126644: Fix various thread safety issues in _interpreters#126696
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
Closed
ZeroIntensity wants to merge 24 commits into python:main from ZeroIntensity:interp-running-thread-safety
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
24 commits Select commit Hold shift + click to select a range
f18939a Initial fix.
ZeroIntensity 756bf41 Remove junk file.
ZeroIntensity 45c4561 Merge branch 'main' of https://github.com/python/cpython into interp-…
ZeroIntensity a94eaef Don't overwrite the main thread.
ZeroIntensity 206b581 Small changes.
ZeroIntensity 286e536 Put lock around exception capturing.
ZeroIntensity 2fab7af Add a comment.
ZeroIntensity 446abc1 Add blurb.
ZeroIntensity cc36e8d Add a test.
ZeroIntensity e25587e Skip test on the default build.
ZeroIntensity e4b2c79 Fix and move tests.
ZeroIntensity aeb483e Somehow, the locks are re-entrant.
ZeroIntensity 92b3ea7 Don't rely on the interpreter state for _PyXI_ERR_ALREADY_RUNNING
ZeroIntensity 804c41e Throw an exception if there's a remaining thread state.
ZeroIntensity 2edfda3 Remove newline.
ZeroIntensity bb8feee Remove another newline.
ZeroIntensity 5968448 Add docstrings.
ZeroIntensity 9ab979d Protect _PyIndexPool_AllocIndex with the runtime lock.
ZeroIntensity b478375 Bump the thread count.
ZeroIntensity f393ab6 Add comments to the test.
ZeroIntensity 35323f8 Re-enable subinterpreter tests for the free-threaded build.
ZeroIntensity 67df772 Fix tests.
ZeroIntensity 13e96de Lock runtime instead of using pyatomic
ZeroIntensity c9e7b08 Fix failing builds.
ZeroIntensity 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
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
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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import threading | ||
| import unittest | ||
| from test import support | ||
| from test.support import import_helper | ||
| from test.support import threading_helper | ||
| _interpreters = import_helper.import_module('_interpreters') | ||
| @threading_helper.requires_working_threading() | ||
| class StressTests(unittest.TestCase): | ||
| @support.requires_resource('cpu') | ||
| def test_subinterpreter_thread_safety(self): | ||
| # GH-126644: _interpreters had thread safety problems on the free-threaded build | ||
| interp = _interpreters.create() | ||
| threads = [threading.Thread(target=_interpreters.run_string, args=(interp, "1")) for _ in range(1000)] | ||
| threads.extend([threading.Thread(target=_interpreters.destroy, args=(interp,)) for _ in range(1000)]) | ||
| # This will spam all kinds of subinterpreter errors, but we don't care. | ||
| # We just want to make sure that it doesn't crash. | ||
| with threading_helper.start_threads(threads): | ||
| pass | ||
| if __name__ == '__main__': | ||
| # Test needs to be a package, so we can do relative imports. | ||
| unittest.main() |
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 |
|---|---|---|
| @@ -1,9 +1,5 @@ | ||
| import os | ||
| from test.support import load_package_tests, Py_GIL_DISABLED | ||
| import unittest | ||
| if Py_GIL_DISABLED: | ||
| raise unittest.SkipTest("GIL disabled") | ||
| from test.support import load_package_tests | ||
| def load_tests(*args): | ||
| return load_package_tests(os.path.dirname(__file__), *args) |
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
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2024-11-10-13-46-21.gh-issue-126644.mnjYQT.rst
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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix crash when using subinterpreters in multiple threads on the | ||
| :term:`free-threaded <free threading>` build. |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem right. _PyIndexPool_AllocIndex locks internally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that's what I thought too. I'm not too sure what's going on here, but this segfaults without this lock.