Title: profiling.sampling incorrectly handles base_frame and native frame as the first frame · Issue #157605 · python/cpython · GitHub
Open Graph Title: profiling.sampling incorrectly handles base_frame and native frame as the first frame · Issue #157605 · python/cpython
X Title: profiling.sampling incorrectly handles base_frame and native frame as the first frame · Issue #157605 · python/cpython
Description: Bug description profiling.sampling does not correctly handle some frame chains whose first frame is not a Python frame. There are two cases: The first frame is base_frame A native thread can retain a Python thread state while waiting in ...
Open Graph Description: Bug description profiling.sampling does not correctly handle some frame chains whose first frame is not a Python frame. There are two cases: The first frame is base_frame A native thread can retain...
X Description: Bug description profiling.sampling does not correctly handle some frame chains whose first frame is not a Python frame. There are two cases: The first frame is base_frame A native thread can retain...
Opengraph URL: https://github.com/python/cpython/issues/157605
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"profiling.sampling incorrectly handles base_frame and native frame as the first frame","articleBody":"## Bug description\n\n`profiling.sampling` does not correctly handle some frame chains whose first frame is not a Python frame.\n\nThere are two cases:\n\n1. **The first frame is `base_frame`**\n\n A native thread can retain a Python thread state while waiting in C code, with no Python functions executing. Its frame chain contains only the `base_frame` sentinel.\n\n The sampler skips this sentinel and then raises:\n\n `RuntimeError: Failed to parse initial frame in chain`\n\n2. **The first frame is a native frame**\n\n When the first frame in the chain is a native frame, the sampler does not correctly handle it as the initial frame and may report the frame chain as invalid instead of adding a `\u003cnative>` frame.\n\nIn both cases, the failure can cause the entire sampling call to fail, so valid Python stacks from other threads are also lost.\n\n## Reproduction\n\n### Case 1: `base_frame` as the first frame\n\nCreate a pthread that registers and detaches a Python thread state:\n\n```c\nPyGILState_STATE state = PyGILState_Ensure();\nPyThreadState *saved = PyEval_SaveThread();\n\n/* Wait on a pthread condition variable while the parent samples. */\n\n/* Cleanup after sampling. */\nPyEval_RestoreThread(saved);\nPyGILState_Release(state);\n```\n\nKeep the main thread inside a Python function and sample the child process from its parent:\n\n```python\nunwinder = _remote_debugging.RemoteUnwinder(\n pid, all_threads=True, cache_frames=False,\n)\nunwinder.get_stack_trace()\n```\n\n[tachyon-empty-native-repro.zip](https://github.com/user-attachments/files/32418460/tachyon-empty-native-repro.zip)\n\n- Reproduces with frame caching both enabled and disabled.\n\n### Case 2: Native frame as the first frame\n\nConstruct a frame chain whose current frame is a native frame and whose `previous` frame leads to `base_frame`.\n\nWhen native-frame collection is enabled, sampling this thread currently fails to correctly handle the native frame as the first frame.\n\n## Expected behavior\n\nThe sampler should correctly handle both cases:\n\n1. If the first frame is `base_frame`, treat it as a valid empty Python stack.\n2. If the first frame is a native frame, add a `\u003cnative>` frame to the returned stack and continue walking the frame chain.\n\nA special frame state in one thread should not cause the entire sampling operation to fail or prevent valid stacks from other threads from being returned.\n\n## Suspected cause\n\n`process_frame_chain()` does not correctly handle cases where the first frame does not contain a parseable Python frame, including `base_frame` and native frames.\n\nThese cases can be incorrectly treated as a broken initial frame chain.\n\n## Environment\n\nLinux, CPython 3.15.0rc2+dev free-threaded build and current `main`.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-157817\n* gh-161784\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/liuzhijie-0614","@type":"Person","name":"liuzhijie-0614"},"datePublished":"2026-09-16T03:07:14Z","url":"https://github.com/python/cpython/issues/157605"}
| 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:910cc43d-fa70-0290-4b10-6bf4f851e016 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DCE2:14EB6E:1A2593B:22567F2:6B0211D6 |
| html-safe-nonce | 6decd9a796d7b37fe1cad2f59909dac29406ae9aa0b74c7925fd169eeb518d75 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQ0UyOjE0RUI2RToxQTI1OTNCOjIyNTY3RjI6NkIwMjExRDYiLCJ2aXNpdG9yX2lkIjoiMTA2NDQ4NTgzMTk3MDAwMTM2NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 93652b4c3167810d74900c78c252e6abbd0c50e8d533169b1a8789a738f3da96 |
| hovercard-subject-tag | issue:5469752374 |
| 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/157605/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0a61f249b69075842159b122f1127fa1e65cc51ed2d85db0841a48d039d0d60e/python/cpython/issues/157605 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0a61f249b69075842159b122f1127fa1e65cc51ed2d85db0841a48d039d0d60e/python/cpython/issues/157605 |
| og:image:alt | Bug description profiling.sampling does not correctly handle some frame chains whose first frame is not a Python frame. There are two cases: The first frame is base_frame A native thread can retain... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | liuzhijie-0614 |
| 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