Title: RemoteUnwinder.get_async_stack_trace() crashes with a C stack overflow on a deeply nested awaited_by graph instead of raising · Issue #151535 · python/cpython · GitHub
Open Graph Title: RemoteUnwinder.get_async_stack_trace() crashes with a C stack overflow on a deeply nested awaited_by graph instead of raising · Issue #151535 · python/cpython
X Title: RemoteUnwinder.get_async_stack_trace() crashes with a C stack overflow on a deeply nested awaited_by graph instead of raising · Issue #151535 · python/cpython
Description: Bug description _remote_debugging.RemoteUnwinder.get_async_stack_trace() reconstructs the async call graph of a target process by recursing up the awaited_by relation. The recursion is a three-function cycle with no depth limit, cycle de...
Open Graph Description: Bug description _remote_debugging.RemoteUnwinder.get_async_stack_trace() reconstructs the async call graph of a target process by recursing up the awaited_by relation. The recursion is a three-func...
X Description: Bug description _remote_debugging.RemoteUnwinder.get_async_stack_trace() reconstructs the async call graph of a target process by recursing up the awaited_by relation. The recursion is a three-func...
Opengraph URL: https://github.com/python/cpython/issues/151535
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"RemoteUnwinder.get_async_stack_trace() crashes with a C stack overflow on a deeply nested awaited_by graph instead of raising","articleBody":"## Bug description\n\n`_remote_debugging.RemoteUnwinder.get_async_stack_trace()` reconstructs the async\ncall graph of a target process by recursing **up** the `awaited_by` relation. The\nrecursion is a three-function cycle with no depth limit, cycle detection, or\n`_Py_EnterRecursiveCall` guard:\n\n`process_task_and_waiters` → `process_task_awaited_by` → `process_waiter_task` →\n`process_task_and_waiters` (…)\n\n(On main these are in `Modules/_remote_debugging/asyncio.c`; on 3.14 they are in\nthe single-file `Modules/_remote_debugging_module.c`.) Each level also stack-allocates\na `char task_obj[SIZEOF_TASK_OBJ]` (`SIZEOF_TASK_OBJ == 4096`), so ~1900 levels\nexhaust a default 8 MiB stack. When the target's running task sits at the bottom of\na sufficiently deep `awaited_by` chain, the **debugger process** (the one calling\n`get_async_stack_trace()`) overflows its C stack and dies with SIGSEGV.\n\nThis is asymmetric with the iterative sibling path: `get_all_awaited_by` /\n`append_awaited_by_for_thread` bounds its walk with `MAX_ITERATIONS = 2 \u003c\u003c 15`.\nOnly the recursive `get_async_stack_trace` path is unbounded. The module already\ntreats the target's tables as untrusted input (`debug_offsets_validation.h`) and the\nthread-list walk already has explicit \"corrupted remote memory\" cycle detection, so\nbounding this traversal is consistent with the module's existing invariants.\n\nThe same pattern (C recursion converted to `RecursionError` instead of a segfault)\nwas treated as a bug in https://github.com/python/cpython/issues/137894.\n\n### Reproducer\n\nA target with a deep linear `awaited_by` chain whose leaf is the running task; a\nsecond process attaches and calls `get_async_stack_trace()`:\n\n```python\n# target.py \u003cN>: tN await t(N-1) await ... await leaf; leaf busy-spins (= running task)\n# attacker.py \u003cpid>:\nfrom _remote_debugging import RemoteUnwinder\nRemoteUnwinder(int(pid)).get_async_stack_trace()\n```\n\n- `N = 10` → returns a stack trace cleanly (exit 0).\n- `N >= ~2000` → attacker process **SIGSEGV** (exit 139). gdb shows ~1884 stacked\n `process_task_awaited_by` frames terminating at a guard-page fault.\n\n(Full PoC scripts available on request.)\n\n### Reproduced on\n\n- CPython **3.14.6** (GA, `python:3.14` image, aarch64 Linux) — crashes.\n- CPython 3.16 `main` (local `--with-pydebug` build) — same unguarded recursion in source.\n\nCross-process attach uses the normal Linux ptrace/`process_vm_readv` path\n(`--cap-add=SYS_PTRACE`); the crash is in the **debugger**, driven by the target's\ngraph shape. A privileged profiler/observability tool attaching to an untrusted (or\njust legitimately deeply nested) workload is the realistic setting.\n\n### Expected behavior\n\nA bounded traversal — raise/propagate an error (as the iterative path does on hitting\nits limit), not crash the debugger process.\n\n## Proposed fix\n\nBound the `process_task_and_waiters` ↔ `process_waiter_task` recursion, matching the\niterative sibling. I have a PR ready that adds an explicit recursion-depth cap\n(`MAX_TASK_AWAITED_BY_DEPTH`, mirroring the existing `MAX_ITERATIONS` /\n`MAX_SET_TABLE_SIZE` constants in the module) and raises a `RuntimeError` on\noverflow, which also handles a cyclic `awaited_by` graph from corrupted remote\nmemory. Happy to open it.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-151536\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-06-16T08:48:12Z","url":"https://github.com/python/cpython/issues/151535"}
| 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:32ef83f3-e8e3-42dc-b323-d7fbb9795a8e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A74A:2F543F:1B94402:246C845:6B02201C |
| html-safe-nonce | 44046e36a937d8a844da29ae9c8797611076cbd9fc56b6e389094ccceaaee939 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzRBOjJGNTQzRjoxQjk0NDAyOjI0NkM4NDU6NkIwMjIwMUMiLCJ2aXNpdG9yX2lkIjoiMTY1NzQ5MDAwMjcwNjUxNDIwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 7bb9e5225f96f07b1198effa96b9e6b0b89252ba137e2df32ba1a7f37a8f036b |
| hovercard-subject-tag | issue:4672582416 |
| 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/151535/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b6502fc5385611eb47ba179af3f3685661fee8d1c7f984c426c3e3a21753b451/python/cpython/issues/151535 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b6502fc5385611eb47ba179af3f3685661fee8d1c7f984c426c3e3a21753b451/python/cpython/issues/151535 |
| og:image:alt | Bug description _remote_debugging.RemoteUnwinder.get_async_stack_trace() reconstructs the async call graph of a target process by recursing up the awaited_by relation. The recursion is a three-func... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonghuaroot |
| 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