Title: Single-Phase Init Extension Module Init Functions Still Run in Isolated Interpreters · Issue #117953 · python/cpython · GitHub
Open Graph Title: Single-Phase Init Extension Module Init Functions Still Run in Isolated Interpreters · Issue #117953 · python/cpython
X Title: Single-Phase Init Extension Module Init Functions Still Run in Isolated Interpreters · Issue #117953 · python/cpython
Description: Bug report Bug description: When an extension module is imported the first time, we load the shared-object file and get the module's init function from it. We then run that init function and use the returned object to decide if the modul...
Open Graph Description: Bug report Bug description: When an extension module is imported the first time, we load the shared-object file and get the module's init function from it. We then run that init function and use th...
X Description: Bug report Bug description: When an extension module is imported the first time, we load the shared-object file and get the module's init function from it. We then run that init function and us...
Opengraph URL: https://github.com/python/cpython/issues/117953
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Single-Phase Init Extension Module Init Functions Still Run in Isolated Interpreters","articleBody":"# Bug report\n\n### Bug description:\n\nWhen an extension module is imported the first time, we load the shared-object file and get the module's init function from it. We then run that init function and use the returned object to decide if the module is single-phase init or multi-phase init.\n\nFor isolated subinterpreters, where `PyInterpreterConfig.check_multi_interp_extensions` (AKA `Py_RTFLAGS_MULTI_INTERP_EXTENSIONS`) is True, we immediately fail single-phase init modules. The problem is that at that point the init function has already run, which all sorts of potential side effects and process-global state (including registered callbacks) that we mostly can't clean up.\n\nThis has come up before, for example with the readline module. It's potentially a bigger problem than I thought at first, so I'd like to get it sorted out for 3.13.\n\n----\n\nFWIW, the simplest solution I can think of is to always call the module init func from the main interpreter (without necessarily doing all the other import steps there). That would look something like this:\n\n1. start a normal import in an isolated subinterpreter\n2. get the init function\n3. switch to the main interpreter\n4. call the init function\n5. switch back\n6. fail if it is single-phase init (and remember that fact)\n\nFor the main interpreter and non-isolated subinterpreters, nothing different would happen from now; there would be no switching. Also, if the first attempt was in an isolated interpreter (which would fail), a subsequent import of that module in the main interpreter (or a non-isolated one) would succeed.\n\nThe only tricky part is, when the init function raises an exception, how to an propagate it from the main interpreter to the subinterpreter. For multi-phase init (if known) we would just call the init func again after switching back. For single-phase init (or unknown) we'd have preserve the exception somehow. This is something I had to deal with for `_interpreters.exec()`, but I'm not sure the same thing will work here.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-117487\n* gh-118116\n* gh-118156\n* gh-118157\n* gh-118167\n* gh-118192\n* gh-118193\n* gh-118194\n* gh-118203\n* gh-118204\n* gh-118205\n* gh-118206\n* gh-118250\n* gh-118532\n* gh-118684\n* gh-120689\n* gh-120707\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/ericsnowcurrently","@type":"Person","name":"ericsnowcurrently"},"datePublished":"2024-04-16T23:47:11.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/117953/cpython/issues/117953"}
| 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:75fc1b16-79df-6366-3fc5-09afd5c19934 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A82E:E3F0D:19FC340:21C26F0:696B30ED |
| html-safe-nonce | bf9bc33ec633d735970d6a281eb096acefea3fcee0af3645af1e807b6170c192 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODJFOkUzRjBEOjE5RkMzNDA6MjFDMjZGMDo2OTZCMzBFRCIsInZpc2l0b3JfaWQiOiI0MzAxMTA4ODAwMzU0MjAxODM3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a20d148cf5211e56e11a91154234139adc9b978ace54b19b8f7c8b541cd6a929 |
| hovercard-subject-tag | issue:2247045160 |
| 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/117953/issue_layout |
| twitter:image | https://opengraph.githubassets.com/988ad9518f815605742fcc564ddf672177015ca0e2eea6fe77229c16ab31fac6/python/cpython/issues/117953 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/988ad9518f815605742fcc564ddf672177015ca0e2eea6fe77229c16ab31fac6/python/cpython/issues/117953 |
| og:image:alt | Bug report Bug description: When an extension module is imported the first time, we load the shared-object file and get the module's init function from it. We then run that init function and use th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ericsnowcurrently |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width