-
-
Notifications
You must be signed in to change notification settings - Fork 34k
gh-144446: Fix some frame object thread-safety issues #144479
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
colesbury
wants to merge
3
commits into
python:main
Choose a base branch
from
colesbury:gh-144446-frame-free-threading
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.
+164
−7
Open
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import functools | ||
| import sys | ||
| import threading | ||
| import unittest | ||
|
|
||
| from test.support import threading_helper | ||
|
|
||
| threading_helper.requires_working_threading(module=True) | ||
|
|
||
|
|
||
| def run_with_frame(funcs, runner=None, iters=10): | ||
| """Run funcs with a frame from another thread that is currently executing. | ||
| Args: | ||
| funcs: A function or list of functions that take a frame argument | ||
| runner: Optional function to run in the executor thread. If provided, | ||
| it will be called and should return eventually. The frame | ||
| passed to funcs will be the runner's frame. | ||
| iters: Number of iterations each func should run | ||
| """ | ||
| if not isinstance(funcs, list): | ||
| funcs = [funcs] | ||
|
|
||
| frame_var = None | ||
| e = threading.Event() | ||
| b = threading.Barrier(len(funcs) + 1) | ||
|
|
||
| if runner is None: | ||
| def runner(): | ||
| j = 0 | ||
| for i in range(100): | ||
| j += i | ||
|
|
||
| def executor(): | ||
| nonlocal frame_var | ||
| frame_var = sys._getframe() | ||
| e.set() | ||
| b.wait() | ||
| runner() | ||
|
|
||
| def func_wrapper(func): | ||
| e.wait() | ||
| frame = frame_var | ||
| b.wait() | ||
| for _ in range(iters): | ||
| func(frame) | ||
|
|
||
| test_funcs = [functools.partial(func_wrapper, f) for f in funcs] | ||
| threading_helper.run_concurrently([executor] + test_funcs) | ||
|
|
||
|
|
||
| class TestFrameRaces(unittest.TestCase): | ||
| def test_concurrent_f_lasti(self): | ||
| run_with_frame(lambda frame: frame.f_lasti) | ||
|
|
||
| def test_concurrent_f_lineno(self): | ||
| run_with_frame(lambda frame: frame.f_lineno) | ||
|
|
||
| def test_concurrent_f_code(self): | ||
| run_with_frame(lambda frame: frame.f_code) | ||
|
|
||
| def test_concurrent_f_back(self): | ||
| run_with_frame(lambda frame: frame.f_back) | ||
|
|
||
| def test_concurrent_f_globals(self): | ||
| run_with_frame(lambda frame: frame.f_globals) | ||
|
|
||
| def test_concurrent_f_builtins(self): | ||
| run_with_frame(lambda frame: frame.f_builtins) | ||
|
|
||
| def test_concurrent_f_locals(self): | ||
| run_with_frame(lambda frame: frame.f_locals) | ||
|
|
||
| def test_concurrent_f_trace_read(self): | ||
| run_with_frame(lambda frame: frame.f_trace) | ||
|
|
||
| def test_concurrent_f_trace_opcodes_read(self): | ||
| run_with_frame(lambda frame: frame.f_trace_opcodes) | ||
|
|
||
| def test_concurrent_repr(self): | ||
| run_with_frame(lambda frame: repr(frame)) | ||
|
|
||
| def test_concurrent_f_trace_write(self): | ||
| def trace_func(frame, event, arg): | ||
| return trace_func | ||
|
|
||
| def writer(frame): | ||
| frame.f_trace = trace_func | ||
| frame.f_trace = None | ||
|
|
||
| run_with_frame(writer) | ||
|
|
||
| def test_concurrent_f_trace_read_write(self): | ||
| # Test concurrent reads and writes of f_trace on a live frame. | ||
| def trace_func(frame, event, arg): | ||
| return trace_func | ||
|
|
||
| def reader(frame): | ||
| _ = frame.f_trace | ||
|
|
||
| def writer(frame): | ||
| frame.f_trace = trace_func | ||
| frame.f_trace = None | ||
|
|
||
| run_with_frame([reader, writer, reader, writer]) | ||
|
|
||
| def test_concurrent_f_trace_opcodes_write(self): | ||
| def writer(frame): | ||
| frame.f_trace_opcodes = True | ||
| frame.f_trace_opcodes = False | ||
|
|
||
| run_with_frame(writer) | ||
|
|
||
| def test_concurrent_f_trace_opcodes_read_write(self): | ||
| # Test concurrent reads and writes of f_trace_opcodes on a live frame. | ||
| def reader(frame): | ||
| _ = frame.f_trace_opcodes | ||
|
|
||
| def writer(frame): | ||
| frame.f_trace_opcodes = True | ||
| frame.f_trace_opcodes = False | ||
|
|
||
| run_with_frame([reader, writer, reader, writer]) | ||
|
|
||
| def test_concurrent_frame_clear(self): | ||
| # Test race between frame.clear() and attribute reads. | ||
| def create_frame(): | ||
| x = 1 | ||
| y = 2 | ||
| return sys._getframe() | ||
|
|
||
| frame = create_frame() | ||
|
|
||
| def reader(): | ||
| for _ in range(10): | ||
| try: | ||
| _ = frame.f_locals | ||
| _ = frame.f_code | ||
| _ = frame.f_lineno | ||
| except ValueError: | ||
| # Frame may be cleared | ||
| pass | ||
|
|
||
| def clearer(): | ||
| frame.clear() | ||
|
|
||
| threading_helper.run_concurrently([reader, reader, clearer]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2026-02-03-17-08-13.gh-issue-144446.db5619.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 data races in the free-threaded build when reading frame object attributes | ||
| while another thread is executing the frame. |
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
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.
Should we move this assignment after following if?
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.
Why should we consider moving it?