Title: `sys.remote_exec()` can fail when subinterpreters exist · Issue #159194 · python/cpython · GitHub
Open Graph Title: `sys.remote_exec()` can fail when subinterpreters exist · Issue #159194 · python/cpython
X Title: `sys.remote_exec()` can fail when subinterpreters exist · Issue #159194 · python/cpython
Description: Bug report When there's a subinterpreters, sys.remote_exec() can fail to find the main thread or run the script in the subinterpreter. Reproduction An idle subinterpreter makes a previously successful call fail: import os, sys, tempfile ...
Open Graph Description: Bug report When there's a subinterpreters, sys.remote_exec() can fail to find the main thread or run the script in the subinterpreter. Reproduction An idle subinterpreter makes a previously success...
X Description: Bug report When there's a subinterpreters, sys.remote_exec() can fail to find the main thread or run the script in the subinterpreter. Reproduction An idle subinterpreter makes a previously suc...
Opengraph URL: https://github.com/python/cpython/issues/159194
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`sys.remote_exec()` can fail when subinterpreters exist","articleBody":"### Bug report\n\nWhen there's a subinterpreters, `sys.remote_exec()` can fail to find the main thread or run the script in the subinterpreter.\n\n## Reproduction\n\nAn idle subinterpreter makes a previously successful call fail:\n\n```python\nimport os, sys, tempfile\nfrom pathlib import Path\nfrom concurrent import interpreters\n\nwith tempfile.TemporaryDirectory() as directory:\n script = Path(directory) / \"remote.py\"\n script.write_text(\"print('script executed')\")\n sys.remote_exec(os.getpid(), str(script))\n interp = interpreters.create()\n try:\n sys.remote_exec(os.getpid(), str(script))\n except RuntimeError as error:\n print(type(error).__name__ + \": \" + str(error))\n finally:\n interp.close()\n```\n\nOutput on `9028df36bb9`:\n\n```bash\nmaurycy@gimel cpython (main 9028df3) % ./python.exe /tmp/re1_repro.py\nscript executed\nRuntimeError: Can't find the main thread in the remote process\nmaurycy@gimel cpython (main 9028df3) %\n```\n\nBoth `sys.remote_exec` calls should execute the script.\n\nAlso, a running subinterpreter receives the script instead of the main interpreter.\n\nThis example calls `remote_exec()` from the subinterpreter itself:\n\n```python\nimport os, sys, tempfile\nfrom pathlib import Path\nfrom concurrent import interpreters\n\nwith tempfile.TemporaryDirectory() as directory:\n script = Path(directory) / \"remote.py\"\n script.write_text(\"from concurrent import interpreters; \"\n \"print('executed in interpreter', interpreters.get_current().id)\")\n interp = interpreters.create()\n try:\n interp.exec(f\"import os, sys; sys.remote_exec(os.getpid(), {str(script)!r})\")\n finally:\n interp.close()\n```\n\nOutput on `9028df36bb9`:\n\n```text\nmaurycy@gimel cpython (main 9028df3) % ./python.exe /tmp/re2_repro.py\nexecuted in interpreter 1\nmaurycy@gimel cpython (main 9028df3) %\n```\n\nI believe the expected is `0`:\n\n\n\n## Cause\n\nNew interpreters enter the start of `_PyRuntime.interpreters`. The list head is the newest interpreter, not the main interpreter:\n\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Python/pystate.c#L740\n\nAns `send_exec_to_proc_handle()` reads only the head:\n\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Python/remote_debugging.c#L125-L133\n\nFor `tid == 0`, which `sys.remote_exec()` passes, it uses that interpreter's `threads_main`:\n\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Python/remote_debugging.c#L202-L216\n\nAn idle subinterpreter has no main thread state. A running subinterpreter has one, but I think (TBD) it'd belong to the wrong interpreter.\n\nThe HOWTO repeats the same lookup for the main thread:\n\nhttps://github.com/python/cpython/blob/9028df36bb9e2942b19164f6708f9cf8a55c5ce9/Doc/howto/remote_debugging.rst#L498-L532\n\nI'm working on the fix.\n\n### CPython versions tested on:\n\nCPython main branch (likely backport to 3.14 and 3.15)\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-159259\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/maurycy","@type":"Person","name":"maurycy"},"datePublished":"2026-10-11T19:55:37Z","url":"https://github.com/python/cpython/issues/159194"}
| 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:e5ad70f4-980e-1694-597c-c46cd266cee4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D4AE:A71F9:3C2631A:4C057CB:6B023C3D |
| html-safe-nonce | 5cbb879ec4a965319c5c50ca9b841694267e7005a52c3af891166b92de9d3c03 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENEFFOkE3MUY5OjNDMjYzMUE6NEMwNTdDQjo2QjAyM0MzRCIsInZpc2l0b3JfaWQiOiIxNjE5NDM3OTQyMTA1ODQ4ODkzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c4f3da3d3cf696a2268ce64bb7341d3c671d66a0e4d0bb3d09294f82bf2dd003 |
| hovercard-subject-tag | issue:5804619269 |
| 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/159194/issue_layout |
| twitter:image | https://opengraph.githubassets.com/28160251ab6539deb28d5496be9722403f6534bc704c5b5d1fee4ebf81309d14/python/cpython/issues/159194 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/28160251ab6539deb28d5496be9722403f6534bc704c5b5d1fee4ebf81309d14/python/cpython/issues/159194 |
| og:image:alt | Bug report When there's a subinterpreters, sys.remote_exec() can fail to find the main thread or run the script in the subinterpreter. Reproduction An idle subinterpreter makes a previously success... |
| 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