Title: _remote_debugging frame cache can reuse stale frame anchors · Issue #151613 · python/cpython · GitHub
Open Graph Title: _remote_debugging frame cache can reuse stale frame anchors · Issue #151613 · python/cpython
X Title: _remote_debugging frame cache can reuse stale frame anchors · Issue #151613 · python/cpython
Description: The remote debugging frame cache uses last_profiled_frame as an address anchor, so it can hit an ABA case when a frame address is reused after the old frame has returned. A profiler sample can then validate against the same address but a...
Open Graph Description: The remote debugging frame cache uses last_profiled_frame as an address anchor, so it can hit an ABA case when a frame address is reused after the old frame has returned. A profiler sample can then...
X Description: The remote debugging frame cache uses last_profiled_frame as an address anchor, so it can hit an ABA case when a frame address is reused after the old frame has returned. A profiler sample can then...
Opengraph URL: https://github.com/python/cpython/issues/151613
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_remote_debugging frame cache can reuse stale frame anchors","articleBody":"The remote debugging frame cache uses `last_profiled_frame` as an address anchor, so it can hit an ABA case when a frame address is reused after the old frame has returned. A profiler sample can then validate against the same address but append cached parent frames from a previous stack, producing impossible mixed stacks such as a `b_leaf` frame with an `a_parent` caller. PR #151437 fixes missing anchor updates, but the cache should also distinguish reused frame addresses, for example by pairing the anchor with a monotonic sequence, so cached continuations are only reused for the exact frame instance previously sampled.\n\nI confirmed the following standalone reproducer. It repeatedly samples a process that alternates between two separate call chains. Seeing names from both chains in one sampled stack is impossible unless cached parent frames were spliced onto the wrong live frame.\n\n```python\nimport contextlib\nimport os\nimport socket\nimport subprocess\nimport sys\nimport tempfile\nimport textwrap\n\nfrom _remote_debugging import PROCESS_VM_READV_SUPPORTED, RemoteUnwinder\n\nTRANSIENT_ERRORS = (OSError, RuntimeError, UnicodeDecodeError)\n\nif sys.platform != \"linux\" or not PROCESS_VM_READV_SUPPORTED:\n raise SystemExit(\"requires Linux with process_vm_readv support\")\n\n\ndef find_free_port():\n with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:\n sock.bind((\"localhost\", 0))\n return sock.getsockname()[1]\n\n\ndef wait_for(sock, expected):\n sock.settimeout(10.0)\n data = b\"\"\n while expected not in data:\n chunk = sock.recv(4096)\n if not chunk:\n raise RuntimeError(f\"target exited before {expected!r}\")\n data += chunk\n\n\nport = find_free_port()\ntarget = f\"\"\"\\\nimport socket\n\nsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nsock.connect((\"localhost\", {port}))\nsock.sendall(b\"ready\")\n\ndef burn_a():\n total = 0\n for i in range(20000):\n total += i\n return total\n\ndef burn_b():\n total = 0\n for i in range(20000):\n total += i\n return total\n\ndef a_leaf():\n return burn_a()\n\ndef b_leaf():\n return burn_b()\n\ndef a_parent():\n return a_leaf()\n\ndef b_parent():\n return b_leaf()\n\nwhile True:\n a_parent()\n b_parent()\n\"\"\"\n\nwith tempfile.TemporaryDirectory() as tmp:\n script = os.path.join(tmp, \"target.py\")\n with open(script, \"w\", encoding=\"utf-8\") as f:\n f.write(textwrap.dedent(target))\n\n server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n server.bind((\"localhost\", port))\n server.listen(1)\n server.settimeout(10.0)\n\n proc = subprocess.Popen([sys.executable, script])\n conn = None\n try:\n conn, _ = server.accept()\n wait_for(conn, b\"ready\")\n unwinder = RemoteUnwinder(\n proc.pid, all_threads=True, cache_frames=True, stats=True\n )\n branch_a = {\"a_parent\", \"a_leaf\", \"burn_a\"}\n branch_b = {\"b_parent\", \"b_leaf\", \"burn_b\"}\n\n for sample in range(1, 8001):\n with contextlib.suppress(*TRANSIENT_ERRORS):\n traces = unwinder.get_stack_trace()\n for interp in traces:\n for thread in interp.threads:\n funcs = [frame.funcname for frame in thread.frame_info]\n names = set(funcs)\n if branch_a & names and branch_b & names:\n print(f\"mixed stack found at sample {sample}\")\n print(funcs)\n print(unwinder.get_stats())\n raise SystemExit(1)\n\n print(\"no mixed stack found\")\n print(unwinder.get_stats())\n finally:\n if conn is not None:\n conn.close()\n server.close()\n proc.kill()\n proc.wait(timeout=10.0)\n```\n\nOn the unfixed build, this reproduced for me with this output:\n\n```text\nmixed stack found at sample 5904\n['b_leaf', 'a_parent', '\u003cmodule>']\n{'total_samples': 5904, 'frame_cache_hits': 5887, 'frame_cache_misses': 13, 'frame_cache_partial_hits': 3, 'frames_read_from_cache': 17666, 'frames_read_from_memory': 5942, 'memory_reads': 11889, 'memory_bytes_read': 5722200, 'code_object_cache_hits': 5947, 'code_object_cache_misses': 7, 'stale_cache_invalidations': 0, 'batched_read_attempts': 5903, 'batched_read_successes': 5903, 'batched_read_misses': 0, 'batched_read_segments_requested': 17709, 'batched_read_segments_completed': 17709, 'frame_cache_hit_rate': 99.7797729967813, 'code_object_cache_hit_rate': 99.88243197850184, 'batched_read_success_rate': 100.0, 'batched_read_segment_completion_rate': 100.0}\n```\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-151614\n* gh-152448\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/pablogsal","@type":"Person","name":"pablogsal"},"datePublished":"2026-06-17T21:14:14Z","url":"https://github.com/python/cpython/issues/151613"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d5b9b239-71d6-6ea7-6d8b-0abeea6b633d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BD94:EAD6D:82224F:A34997:6B0222EA |
| html-safe-nonce | 43039bffe1db8c0619e181f9a3979ee34f1b494c93ca6faf3f73e7ae11d36db6 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRDk0OkVBRDZEOjgyMjI0RjpBMzQ5OTc6NkIwMjIyRUEiLCJ2aXNpdG9yX2lkIjoiNTk0MzI4MjgxOTA2NTUzNTc4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | ccd122afdc036d694c9558a5510af98edf75f59a6edb08f0c82337a50fcfd2cf |
| hovercard-subject-tag | issue:4687112824 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/151613/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a25b970c6b4b1b822441ec844854a0d16727586c7f50b15e15463621ce425675/python/cpython/issues/151613 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a25b970c6b4b1b822441ec844854a0d16727586c7f50b15e15463621ce425675/python/cpython/issues/151613 |
| og:image:alt | The remote debugging frame cache uses last_profiled_frame as an address anchor, so it can hit an ABA case when a frame address is reused after the old frame has returned. A profiler sample can then... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pablogsal |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width