-
-
Notifications
You must be signed in to change notification settings - Fork 35k
gh-152330: Make perf_trampoline thread safe. #152331
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
Open
YukaSadaoka
wants to merge
7
commits into
python:main
Choose a base branch
from
YukaSadaoka:trampoline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−84
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d6446cb
Initial commit to make perf_trampoline.c thread safe
YukaSadaoka db0fedd
Fix formatting
YukaSadaoka 9a9cf87
Fix more formatting
YukaSadaoka 4d8b0c6
Remove a white space
YukaSadaoka d719fc2
Fix lint errors
YukaSadaoka 3b54a77
Merge branch 'main' into trampoline
YukaSadaoka 1c34d62
📜🤖 Added by blurb_it.
blurb-it[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """Tests perf trampoline in a multi-threaded environment to verify thread safety in free-threaded builds.""" | ||
|
|
||
| import gc | ||
| import sys | ||
| import threading | ||
| import unittest | ||
| from test.support import threading_helper | ||
|
|
||
| NTHREADS = 10 | ||
| ITERATIONS_PER_THREAD = 50 | ||
|
|
||
|
|
||
| @threading_helper.requires_working_threading() | ||
| class TestPerfTrampolineThreadSafety(unittest.TestCase): | ||
|
|
||
| def test_concurrent_code_compilation(self): | ||
| """Stress test simultaneous allocation of code arenas and tracking hooks.""" | ||
| if not hasattr(sys, "_perf_trampoline_init") or not hasattr(sys, "_perf_trampoline_fini"): | ||
| self.skipTest("perf trampoline APIs are not supported on this platform") | ||
|
|
||
| try: | ||
| sys._perf_trampoline_init(1) | ||
| except ValueError as exc: | ||
| self.skipTest(f"perf trampoline activation failed: {exc}") | ||
| self.addCleanup(sys._perf_trampoline_fini) | ||
|
|
||
| barrier = threading.Barrier(NTHREADS) | ||
|
|
||
| def worker(): | ||
| barrier.wait() | ||
| for i in range(ITERATIONS_PER_THREAD): | ||
| ns = {} | ||
| tid = threading.get_ident() | ||
| exec( | ||
| f"def func_{tid}_{i}(): return {i}\n" | ||
| f"result = func_{tid}_{i}()", | ||
| ns, | ||
| ) | ||
| self.assertEqual(ns["result"], i) | ||
| del ns # Immediate drop to prevent delaying cleanup, no internal gc.collect() | ||
|
|
||
| threading_helper.run_concurrently( | ||
| nthreads=NTHREADS, worker_func=worker | ||
| ) | ||
| # Single final sweep to ensure code objects and extra tracking clear cleanly | ||
| gc.collect() | ||
|
|
||
| def test_concurrent_shared_code_execution(self): | ||
| """Verify multiple threads safely handle entering evaluation loops for the same code object.""" | ||
| if not hasattr(sys, "_perf_trampoline_init") or not hasattr(sys, "_perf_trampoline_fini"): | ||
| self.skipTest("perf trampoline APIs are not supported on this platform") | ||
|
|
||
| try: | ||
| sys._perf_trampoline_init(1) | ||
| except ValueError as exc: | ||
| self.skipTest(f"perf trampoline activation failed: {exc}") | ||
| self.addCleanup(sys._perf_trampoline_fini) | ||
|
|
||
| barrier = threading.Barrier(NTHREADS) | ||
|
|
||
| # Pre-compile a single function instance to share across all threads | ||
| shared_ns = {} | ||
| exec("def shared_func(): return 42", shared_ns) | ||
| shared_func = shared_ns["shared_func"] | ||
|
|
||
| def worker(): | ||
| barrier.wait() | ||
| for _ in range(ITERATIONS_PER_THREAD): | ||
| self.assertEqual(shared_func(), 42) | ||
|
|
||
| threading_helper.run_concurrently( | ||
| nthreads=NTHREADS, worker_func=worker | ||
| ) | ||
|
|
||
| del shared_func | ||
| del shared_ns | ||
| # Single final sweep to clean up shared resources safely | ||
| gc.collect() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2026-06-29-09-08-32.gh-issue-152330.QS46b4.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 @@ | ||
| Make perf_trampoline thread safe by fixing critical data races, post-fork deadlocks, memory leaks, and edge-case exception handling issues. | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Nit: this is a change in
Python/perf_trampoline.c, so the NEWS entry should go inCore and Builtins, notLibrary.