Title: `_remote_debugging`: `RemoteUnwinder` caches the interpreters list head in `__init__` as the main interpreter · Issue #159262 · python/cpython · GitHub
Open Graph Title: `_remote_debugging`: `RemoteUnwinder` caches the interpreters list head in `__init__` as the main interpreter · Issue #159262 · python/cpython
X Title: `_remote_debugging`: `RemoteUnwinder` caches the interpreters list head in `__init__` as the main interpreter · Issue #159262 · python/cpython
Description: Bug report An existing RemoteUnwinder misses interpreters created after its constructor runs. If the saved interpreter closes, later samples can read freed memory. The default mode also selects the main thread of the newest interpreter a...
Open Graph Description: Bug report An existing RemoteUnwinder misses interpreters created after its constructor runs. If the saved interpreter closes, later samples can read freed memory. The default mode also selects the...
X Description: Bug report An existing RemoteUnwinder misses interpreters created after its constructor runs. If the saved interpreter closes, later samples can read freed memory. The default mode also selects the...
Opengraph URL: https://github.com/python/cpython/issues/159262
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`_remote_debugging`: `RemoteUnwinder` caches the interpreters list head in `__init__` as the main interpreter","articleBody":"### Bug report\n\n\nAn existing `RemoteUnwinder` misses interpreters created after its constructor runs. If the saved interpreter closes, later samples can read freed memory. The default mode also selects the main thread of the newest interpreter and can omit the main interpreter.\n\nFound while reviewing #158900, #158971 and working on #159259.\n\n## Reproductions\n\n### A new interpreter is missing from an existing unwinder\n\nThe existing unwinder returns only interpreter `0`. A new unwinder sees both interpreters:\n\n```python\nimport os\nfrom concurrent import interpreters\nfrom _remote_debugging import RemoteUnwinder\n\nunwinder = RemoteUnwinder(os.getpid(), all_threads=True)\ninterp = interpreters.create()\ntry:\n print([i.interpreter_id for i in unwinder.get_stack_trace()])\n fresh = RemoteUnwinder(os.getpid(), all_threads=True)\n print([i.interpreter_id for i in fresh.get_stack_trace()])\nfinally:\n interp.close()\n```\n\n```bash\n2026-10-12T22:43:31.492120000+0200 maurycy@gimel /Users/maurycy/work/cpython (main 5e049c3) % ./python.exe /tmp/aaa.py\n[0]\n[1, 0]\n```\n\n### The saved interpreter closes\n\n```bash\nmaurycy@gimel cpython (main 9028df3) % cat /tmp/repro_dangling.py\nimport os\nfrom concurrent import interpreters\nfrom _remote_debugging import RemoteUnwinder\n\ninterp = interpreters.create()\nunwinder = RemoteUnwinder(os.getpid(), all_threads=True)\ninterp.close()\nprint([i.interpreter_id for i in unwinder.get_stack_trace()])\nmaurycy@gimel cpython (main 9028df3) % ./python.exe /tmp/repro_dangling.py\nTraceback (most recent call last):\n File \"/tmp/repro_dangling.py\", line 8, in \u003cmodule>\n print([i.interpreter_id for i in unwinder.get_stack_trace()])\n ~~~~~~~~~~~~~~~~~~~~~~~~^^\nRuntimeError: mach_vm_read_overwrite failed for PID 32948 at address 0xdddddddddddddddd (size 888): kern_return_t 1\n```\n\n`0xdd` is the debug allocator's dead byte (with `PYTHONMALLOC=debug`)\n\n### The default mode omits the main interpreter\n\n```python\nfrom concurrent import interpreters\n\ninterp = interpreters.create()\ntry:\n interp.exec(\"\"\"\nimport os\nfrom _remote_debugging import RemoteUnwinder\nfor all_threads in (False, True):\n unwinder = RemoteUnwinder(os.getpid(), all_threads=all_threads)\n unwinder.get_stack_trace()\n print(all_threads, [i.interpreter_id for i in unwinder.get_stack_trace()])\n\"\"\")\nfinally:\n interp.close()\n```\n\n```bash\n2026-10-12T22:54:47.076383000+0200 maurycy@gimel /Users/maurycy/work/cpython (main 5e049c3) % ./python.exe /tmp/xoxo.py\nFalse [1]\nTrue [1, 0]\n```\n\n\u003cdetails>\n\u003csummary>Additional cases: an idle interpreter and a freed thread state\u003c/summary>\n\nIdle subinterpreter (main interpreter has two threads):\n\n```bash\n2026-10-12T22:54:49.923205000+0200 maurycy@gimel /Users/maurycy/work/cpython (main 5e049c3) % cat /tmp/repro_idle_default.py\nimport os, threading, time\nfrom concurrent import interpreters\nfrom _remote_debugging import RemoteUnwinder\n\nev = threading.Event()\nt = threading.Thread(target=ev.wait); t.start()\ninterp = interpreters.create()\nfor kwargs in ({}, {\"all_threads\": True}):\n u = RemoteUnwinder(os.getpid(), **kwargs); u.get_stack_trace()\n print(kwargs, [(i.interpreter_id, len(i.threads)) for i in u.get_stack_trace()])\nev.set(); t.join(); interp.close()\n2026-10-12T22:55:14.833746000+0200 maurycy@gimel /Users/maurycy/work/cpython (main 5e049c3) % ./python.exe /tmp/repro_idle_default.py\n{} [(1, 0), (0, 2)]\n{'all_threads': True} [(1, 0), (0, 2)]\n```\n\nThe subinterpreter goes idle after `__init__`, releasing the saved thread state. The sample still omits the main interpreter:\n\n```bash\nmaurycy@gimel cpython (main 9028df3) % cat /tmp/repro_stale_tstate.py\nimport os, threading, time\nfrom concurrent import interpreters\nfrom _remote_debugging import RemoteUnwinder\n\ninterp = interpreters.create()\nt = threading.Thread(target=interp.exec, args=(\"import time; time.sleep(0.5)\",)); t.start(); time.sleep(0.2)\nunwinder = RemoteUnwinder(os.getpid())\nt.join()\nprint([(i.interpreter_id, [f.funcname for t in i.threads for f in t.frame_info]) for i in unwinder.get_stack_trace()])\ninterp.close()\nmaurycy@gimel cpython (main 9028df3) % ./python.exe /tmp/repro_stale_tstate.py\n[(1, [])]\n```\n\n\u003c/details>\n\n## Cause\n\nThe constructor saves `runtime->interpreters.head` as `self->interpreter_addr`.\n\nWith `all_threads=False`, it also saves `head->threads_main` as `self->tstate_addr`:\n\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Modules/_remote_debugging/module.c#L422-L423\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Modules/_remote_debugging/threads.c#L92-L128\n\nWhere:\n\n- New interpreters are prepended. A saved head can omit new entries or point to freed memory.\n- The head provides the thread to sample. It is the newest interpreter, and `get_stack_trace()` stops after it when the saved thread address is non-zero.\n\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Python/pystate.c#L740\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Python/pystate.c#L1050-L1066\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Modules/_remote_debugging/module.c#L887-L890\n\nAn idle subinterpreter has `threads_main == NULL`. That sets `tstate_addr` to zero, which seems to select all threads. A running subinterpreter can also release its saved main thread state after `exec()` returns:\n\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Python/crossinterp.c#L2621\n\nTwo questions:\n\n- The CLI docs say \"only the main thread\". The method docstring says \"the main thread of each interpreter\". Sampling each interpreter's main thread would follow the method docstring.\n- Some embedders may not set `threads.main`. The proposed selection returns no default-mode threads for those interpreters, instead of accidentally returning all threads.\n\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Modules/_remote_debugging/module.c#L670-L674\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Doc/library/profiling.sampling.rst#L477\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Python/pystate.c#L1164-L1170\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nmacOS","author":{"url":"https://github.com/maurycy","@type":"Person","name":"maurycy"},"datePublished":"2026-10-12T21:03:08Z","url":"https://github.com/python/cpython/issues/159262"}
| 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:8f0813a0-330b-04f4-3506-7d27794b2ba8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9D78:A71F9:3A656CF:49D6F0F:6B0232E7 |
| html-safe-nonce | 7aa57e2147d9da580c0ae0f647a36d83add0955c47ec3ed67294d93eb8a0f2bd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDc4OkE3MUY5OjNBNjU2Q0Y6NDlENkYwRjo2QjAyMzJFNyIsInZpc2l0b3JfaWQiOiI4MDc5ODgzMjkxNTQyNTY1NjA3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 9defcf42b0c8d3c2b43577c65ac00e839f63a1cdcc5a882b4bff266bdb3a9cd3 |
| hovercard-subject-tag | issue:5820391401 |
| 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/159262/issue_layout |
| twitter:image | https://opengraph.githubassets.com/50c576b7b5654a585f098ba5460ef28b293d3af3094cb666d2be7b66364e96f9/python/cpython/issues/159262 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/50c576b7b5654a585f098ba5460ef28b293d3af3094cb666d2be7b66364e96f9/python/cpython/issues/159262 |
| og:image:alt | Bug report An existing RemoteUnwinder misses interpreters created after its constructor runs. If the saved interpreter closes, later samples can read freed memory. The default mode also selects the... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | maurycy |
| 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