Title: Extensions that raise exceptions in their `PyInit*` cause a crash in subinterpreters · Issue #144601 · python/cpython · GitHub
Open Graph Title: Extensions that raise exceptions in their `PyInit*` cause a crash in subinterpreters · Issue #144601 · python/cpython
X Title: Extensions that raise exceptions in their `PyInit*` cause a crash in subinterpreters · Issue #144601 · python/cpython
Description: Crash report What happened? I found this while investigating #138045. If a PyInit_ function raises an exception when imported from a subinterpreter, the process will crash. For example, if you modify xxsubtyppe.c to raise an exception in...
Open Graph Description: Crash report What happened? I found this while investigating #138045. If a PyInit_ function raises an exception when imported from a subinterpreter, the process will crash. For example, if you modi...
X Description: Crash report What happened? I found this while investigating #138045. If a PyInit_ function raises an exception when imported from a subinterpreter, the process will crash. For example, if you modi...
Opengraph URL: https://github.com/python/cpython/issues/144601
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Extensions that raise exceptions in their `PyInit*` cause a crash in subinterpreters","articleBody":"# Crash report\n\n### What happened?\n\nI found this while investigating #138045.\n\nIf a `PyInit_` function raises an exception when imported from a subinterpreter, the process will crash. For example, if you modify `xxsubtyppe.c` to raise an exception in its `PyInit` and then import it from a subinterpreter, you'll see a crash:\n\n```diff\ndiff --git a/Modules/xxsubtype.c b/Modules/xxsubtype.c\nindex a8a1417f40e..57a5ef75eb7 100644\n--- a/Modules/xxsubtype.c\n+++ b/Modules/xxsubtype.c\n@@ -324,5 +324,6 @@ static struct PyModuleDef xxsubtypemodule = {\n PyMODINIT_FUNC\n PyInit_xxsubtype(void)\n {\n- return PyModuleDef_Init(&xxsubtypemodule);\n+ PyErr_SetString(PyExc_TypeError, \"whatever\");\n+ return NULL;\n }\n```\n\n```py\nfrom concurrent import interpreters\n\ninterp = interpreters.create()\ninterp.exec(\"import xxsubtype\") # Crash!\n```\n\nThis isn't usually a problem for multi-phase extensions, because `PyModuleDef_Init` doesn't fail with an exception (except [sometimes on the free-threaded build](https://github.com/python/cpython/blob/d73634935cb9ce00a57dcacbd2e56371e4c18451/Objects/moduleobject.c#L128-L133), but that's rare), but this does happen for single-phase extensions. Single-phase extension modules do not support loading in subinterpreters, but attempting to load them from a subinterpreter should raise an exception rather than crash the interpreter.\n\nI believe the problem lies here:\n\nhttps://github.com/python/cpython/blob/d73634935cb9ce00a57dcacbd2e56371e4c18451/Python/import.c#L2178-L2196\n\nThe exception object raised by the `PyInit_` function is always owned by the main interpreter, but the exception is raised from the subinterpreter, which breaks things. There's not a clean way to raise the exception in the calling interpreter, so I think the best solution is to print the exception as unraisable in the main interpreter, and then raise some sort of blanket exception in the calling interpreter. \n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-144602\n* gh-144633\n* gh-144769\n* gh-144877\n* gh-144880\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/ZeroIntensity","@type":"Person","name":"ZeroIntensity"},"datePublished":"2026-02-08T17:23:07Z","url":"https://github.com/python/cpython/issues/144601"}
| 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:b1a39092-ee8a-faac-33bf-e0a04c730e26 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D480:3FDF0C:587927:7638CB:6B003122 |
| html-safe-nonce | d6ee91670d2e9caf55a44ec156498f9d39b8f961d9c8a5db56b05f9daf8046c0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENDgwOjNGREYwQzo1ODc5Mjc6NzYzOENCOjZCMDAzMTIyIiwidmlzaXRvcl9pZCI6IjkzMjM4NDMyODg0NTg5ODAxOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 116fe310825ce0eb3d9e0e5a309c3d374271af27078a4ba151098442c4fc8c21 |
| hovercard-subject-tag | issue:3913152291 |
| 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/144601/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c8d8fe806a135bbd01107e4d949569769a40c972213499af44218bbcdb5783a6/python/cpython/issues/144601 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c8d8fe806a135bbd01107e4d949569769a40c972213499af44218bbcdb5783a6/python/cpython/issues/144601 |
| og:image:alt | Crash report What happened? I found this while investigating #138045. If a PyInit_ function raises an exception when imported from a subinterpreter, the process will crash. For example, if you modi... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ZeroIntensity |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width