Title: gh-157605: Accept empty Python frame stack in sampling frame chain parser by aidaodedjl · Pull Request #157657 · python/cpython · GitHub
Open Graph Title: gh-157605: Accept empty Python frame stack in sampling frame chain parser by aidaodedjl · Pull Request #157657 · python/cpython
X Title: gh-157605: Accept empty Python frame stack in sampling frame chain parser by aidaodedjl · Pull Request #157657 · python/cpython
Description: Description A native thread can retain a Python thread state while waiting in C code with no Python functions executing (e.g. after PyGILState_Ensure() + PyEval_SaveThread()). The frame chain of that thread state holds only the base_frame sentinel. process_frame_chain() treated the resulting NULL first frame as a broken chain, raised RuntimeError: Failed to parse initial frame in chain, and failed the entire sampling call — discarding valid stacks from all other threads. With this change, the unparseable-first-frame error is raised only when the first frame is not the base_frame sentinel. When it is the sentinel, the walk stops and the empty Python stack is accepted, so sampling returns results for the remaining threads. The walk stops at the sentinel, so last_frame_visited still equals base_frame_addr and the complete-stack validation in the frame cache keeps working for the empty-stack case. Reproduction / evidence Pre-fix (Linux, cross-process, issue reproducer): pin=1 (native thread retaining an empty-stack thread state) fails 40/40 samples with RuntimeError: Failed to parse initial frame in chain, with and without frame caching; pin=0 baseline succeeds 20/20. Post-fix (same environment): all four pin x cache_frames combinations succeed 20/20 with the target thread's Python frames visible. Test test_external_inspection.py gains a self-contained regression test (TestEmptyPythonStackSampling): compiles a small C helper at runtime that spawns a native thread retaining a Python thread state with an empty frame stack (loaded via ctypes); asserts same-process sampling succeeds and the target thread's Python frames stay visible, across pin x cache_frames combinations; skips gracefully when no C compiler is configured (including Windows, where CC is not set) and fails loudly if the helper itself fails to compile. On this machine (macOS, 3.16 debug build) the test reproduces the failure before the fix ({'failure': 5}) and passes after (1.1 s). Issue: gh-157605
Open Graph Description: Description A native thread can retain a Python thread state while waiting in C code with no Python functions executing (e.g. after PyGILState_Ensure() + PyEval_SaveThread()). The frame chain of th...
X Description: Description A native thread can retain a Python thread state while waiting in C code with no Python functions executing (e.g. after PyGILState_Ensure() + PyEval_SaveThread()). The frame chain of th...
Opengraph URL: https://github.com/python/cpython/pull/157657
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:1c70872a-57c7-8c59-4b55-12e1856171a1 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | C2E2:22DB35:466850C:59A51F8:6B022F7F |
| html-safe-nonce | 20d145c9c249cb633d15ab33e265d178a42f1b7f50295238a3faa9766d6f3fc0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMkUyOjIyREIzNTo0NjY4NTBDOjU5QTUxRjg6NkIwMjJGN0YiLCJ2aXNpdG9yX2lkIjoiNTg1Mjc3Mzk3MzUzMTcwMTExOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4f121e092f064aabe41bb59312df0c61bb7876241323e769c16815aff43dd67f |
| hovercard-subject-tag | pull_request:4554744201 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/python/cpython/pull/157657/files |
| twitter:image | https://avatars.githubusercontent.com/u/55816803?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/55816803?s=400&v=4 |
| og:image:alt | Description A native thread can retain a Python thread state while waiting in C code with no Python functions executing (e.g. after PyGILState_Ensure() + PyEval_SaveThread()). The frame chain of th... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| 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 |
Links:
Viewport: width=device-width