-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
gh-151613: Fix remote debugging frame cache ABA #151614
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
pablogsal
wants to merge
1
commit into
python:main
Choose a base branch
from
pablogsal:gh-151613-remote-debugging-frame-cache-aba
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.
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
|
|
@@ -3451,6 +3451,90 @@ def level1(): | |
| # Parent frames: must match exactly | ||
| self.assertEqual(loc_cached, loc_no_cache) | ||
|
|
||
| @skip_if_not_supported | ||
| @unittest.skipIf( | ||
| sys.platform != "linux" or not PROCESS_VM_READV_SUPPORTED, | ||
| "Test only runs on Linux with process_vm_readv support", | ||
| ) | ||
| def test_cache_rejects_reused_frame_address_aba(self): | ||
| """Test that frame address reuse cannot splice cached parent frames.""" | ||
| script_body = """\ | ||
| sock.sendall(b"ready") | ||
|
|
||
| def burn_a(): | ||
| total = 0 | ||
| for i in range(20000): | ||
| total += i | ||
| return total | ||
|
|
||
| def burn_b(): | ||
| total = 0 | ||
| for i in range(20000): | ||
| total += i | ||
| return total | ||
|
|
||
| def a_leaf(): | ||
| return burn_a() | ||
|
|
||
| def b_leaf(): | ||
| return burn_b() | ||
|
|
||
| def a_parent(): | ||
| return a_leaf() | ||
|
|
||
| def b_parent(): | ||
| return b_leaf() | ||
|
|
||
| while True: | ||
| a_parent() | ||
| b_parent() | ||
| """ | ||
|
|
||
| with self._target_process(script_body) as (p, client_socket, _): | ||
| _wait_for_signal(client_socket, b"ready") | ||
| unwinder = RemoteUnwinder( | ||
| p.pid, cache_frames=True, stats=True | ||
| ) | ||
| bad_stacks = [] | ||
| seen = 0 | ||
| branch_a = {"a_parent", "a_leaf", "burn_a"} | ||
| branch_b = {"b_parent", "b_leaf", "burn_b"} | ||
|
|
||
| for _ in range(8000): | ||
| try: | ||
| traces = unwinder.get_stack_trace() | ||
| except TRANSIENT_ERRORS: | ||
| continue | ||
| for interp in traces: | ||
| for thread in interp.threads: | ||
| in_a = False | ||
| in_b = False | ||
| for frame in thread.frame_info: | ||
| name = frame.funcname | ||
| if name in branch_a: | ||
| in_a = True | ||
| elif name in branch_b: | ||
| in_b = True | ||
| if in_a and in_b: | ||
| break | ||
| if not (in_a or in_b): | ||
| continue | ||
| seen += 1 | ||
| if in_a and in_b: | ||
| funcs = [f.funcname for f in thread.frame_info] | ||
| bad_stacks.append(funcs) | ||
| if len(bad_stacks) >= 3: | ||
| break | ||
|
|
||
| stats = unwinder.get_stats() | ||
|
|
||
| self.assertGreater(seen, 0) | ||
| self.assertGreater( | ||
| stats["frame_cache_hits"] + stats["frame_cache_partial_hits"], 0 | ||
| ) | ||
| self.assertGreater(stats["frames_read_from_cache"], 0) | ||
| self.assertEqual(bad_stacks, []) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's flaky by design? I'd skip it now, and go with a separate harness (later?). |
||
|
|
||
| @skip_if_not_supported | ||
| @unittest.skipIf( | ||
| sys.platform == "linux" and not PROCESS_VM_READV_SUPPORTED, | ||
|
|
||
2 changes: 1 addition & 1 deletion
2
Misc/NEWS.d/next/Library/2026-06-13-11-57-48.gh-issue-151436.UEDowO.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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| Fix skewed stack trackes in the Tachyon profiler when caching is enabled and | ||
| Fix skewed stack traces in the Tachyon profiler when caching is enabled and | ||
| when generators and coroutines are profiled, by updating | ||
| ``tstate->last_profiled_frame`` at every frame-removal site. The issue resulted | ||
| in total erasure of some callers. Patch by Maurycy Pawłowski-Wieroński. |
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2026-06-17-22-31-57.gh-issue-151613.n0nua1.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,3 @@ | ||
| Fix another way the Tachyon profiler frame cache could produce impossible | ||
| mixed stack traces when ``_PyInterpreterFrame`` addresses are reused, by | ||
| validating cached frame anchors with a sequence counter. |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
The churn can happen at any point:
#define _PyThreadState_UpdateLastProfiledFrame(tstate, frame, previous) \ do { \ PyThreadState *tstate_ = (tstate); \ _PyInterpreterFrame *frame_ = (frame); \ - if (tstate_->last_profiled_frame == frame_) { \ - tstate_->last_profiled_frame = (previous); \ + _PyInterpreterFrame *last_profiled_frame_ = tstate_->last_profiled_frame; \ + if (last_profiled_frame_ != NULL) { \ tstate_->last_profiled_frame_seq++; \ + if (last_profiled_frame_ == frame_) { \ + tstate_->last_profiled_frame = (previous); \ + } \ } \ } while (0)maurycy#5 (comment)