René's URL Explorer Experiment


Title: Isolate PyModuleDef to Each Interpreter for Extension/Builtin Modules · Issue #101758 · python/cpython · GitHub

Open Graph Title: Isolate PyModuleDef to Each Interpreter for Extension/Builtin Modules · Issue #101758 · python/cpython

X Title: Isolate PyModuleDef to Each Interpreter for Extension/Builtin Modules · Issue #101758 · python/cpython

Description: Typically each PyModuleDef for a builtin/extension module is a static global variable. Currently it's shared between all interpreters, whereas we are working toward interpreter isolation (for a variety of reasons). Isolating each PyModul...

Open Graph Description: Typically each PyModuleDef for a builtin/extension module is a static global variable. Currently it's shared between all interpreters, whereas we are working toward interpreter isolation (for a var...

X Description: Typically each PyModuleDef for a builtin/extension module is a static global variable. Currently it's shared between all interpreters, whereas we are working toward interpreter isolation (for a...

Opengraph URL: https://github.com/python/cpython/issues/101758

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Isolate PyModuleDef to Each Interpreter for Extension/Builtin Modules","articleBody":"Typically each `PyModuleDef` for a builtin/extension module is a static global variable.  Currently it's shared between all interpreters, whereas we are working toward interpreter isolation (for a variety of reasons).  Isolating each `PyModuleDef` is worth doing, especially if you consider we've already run into problems[^1] because of `m_copy`.\r\n\r\nThe main focus here is on `PyModuleDef.m_base.m_copy`[^2] specifically.  It's the state that facilitates importing legacy (single-phase init) extension/builtin modules that do not support repeated initialization[^3] (likely the vast majority).\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003e(expand for more context)\u003c/summary\u003e\r\n\r\n----\r\n`PyModuleDef` for an extension/builtin module is usually stored in a static variable and (with immortal objects, see gh-101755) is mostly immutable.  The exception is `m_copy`, which is problematic in some cases for modules imported in multiple interpreters.\r\n\r\nNote that `m_copy` is only relevant for legacy (single-phase init) modules, whether builtin and an extension, and only if the module does *not* support repeated initialization[^3].  It is never relevant for multi-phase init (PEP 489) modules.\r\n\r\n* initialization\r\n   * `m_copy` is only set by `_PyImport_FixupExtensionObject()` (and thus indirectly `_PyImport_FixupBuiltin()` and `_imp.create_builtin()`)\r\n   * `_PyImport_FixupExtensionObject() is called by `_PyImport_LoadDynamicModuleWithSpec()` when a legacy (single-phase init) extension module is loaded\r\n* usage\r\n   * `m_copy` is only used in `import_find_extension()`, which is only called by `_imp.create_builtin()` and `_imp.create_dynamic()` (via the respective importers)\r\n\r\nWhen such a legacy module is imported for the first time, `m_copy` is set to a new copy of the just-imported module's `__dict__`, which is \"owned\" by the current interpreter (the one importing the module).  Whenever the module is loaded again (e.g. reloaded or deleted from `sys.modules` and then imported), a new empty module is created and `m_copy` is [shallow] copied into that object's `__dict__`.\r\n\r\nWhen `m_copy` is originally initialized, normally that will be the first time the module is imported.  However, that code can be triggered multiple times for that module if it is imported under a different name (an unlikely case but apparently a real one).  In that case the `m_copy` from the previous import is replaced with the new one right after it is released (decref'ed).  This isn't the ideal approach but it's also been the behavior for [quite a while](https://peps.python.org/pep-3121/).\r\n\r\nThe tricky problem here is that the same code is triggered for each interpreter that imports the legacy module.  Things are fine when a module is imported for the first time in any interpreter.  However, currently, any subsequent import of that module in another interpreter will trigger that replacing code.  The second interpreter decref's the old `m_copy`, but that object is \"owned\" by the first interpreter.  This is a problem[^1].\r\n\r\nFurthermore, even if the decref-in-the-wrong-interpreter problem was gone.  When `m_copy` is copied into the new module's `__dict__` on subsequent imports, it's only a shallow copy.  Thus such a legacy module, imported in other interpreters than the first one, would end up with its `__dict__` filled with objects not owned by the correct interpreter.\r\n\r\n----\r\n\u003c/details\u003e\r\n\r\nHere are some possible approaches to isolating each module's `PyModuleDef` to the interpreter that imports it:\r\n\r\n1. keep a copy of `PyModuleDef` for each interpreter (would `_PyRuntimeState.imports.extensions` need to move to the interpreter?)\r\n2. keep just `m_copy` for/on each interpreter\r\n3. fix `_PyImport_FixupExtensionObject()` some other way...\r\n\r\n\r\n[^1]: see https://github.com/python/cpython/pull/101660#issuecomment-1424507393\r\n[^2]: We should probably consider isolating `PyModuleDef.m_base.m_index`, but for now we simply sync the `modules_by_index` list of each interpreter.  (Also, `modules_by_index` and `m_index` are only used for single-phase init modules.)\r\n[^3]: specifically `def-\u003em_size == -1`; multi-phase init modules always have `def-\u003em_size \u003e= 0`; single-phase init modules can also have a non-negative `m_size`\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-101891\n* gh-101919\n* gh-101920\n* gh-101943\n* gh-101956\n* gh-101969\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/ericsnowcurrently","@type":"Person","name":"ericsnowcurrently"},"datePublished":"2023-02-09T20:54:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/101758/cpython/issues/101758"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:556f99f0-70f4-0542-1554-0e61bfb20ece
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD32C:11141E:F8A143:14BAB9F:696991EF
html-safe-nonce6783d0e47730e41e2190da65250d901f61fd5005dd9babcb55d88788af6d9a84
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzJDOjExMTQxRTpGOEExNDM6MTRCQUI5Rjo2OTY5OTFFRiIsInZpc2l0b3JfaWQiOiI1ODcyMTU1ODcyNDIxNTgxMjk2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmaca9d680af1aadf791c8f0b75c0959303f5c2d9e994168d173a74653716190a0a7
hovercard-subject-tagissue:1578607034
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/101758/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c8abda5f70a9462cedf39582768016ab53d50076ff3112ad881ee286c9a60436/python/cpython/issues/101758
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c8abda5f70a9462cedf39582768016ab53d50076ff3112ad881ee286c9a60436/python/cpython/issues/101758
og:image:altTypically each PyModuleDef for a builtin/extension module is a static global variable. Currently it's shared between all interpreters, whereas we are working toward interpreter isolation (for a var...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameericsnowcurrently
hostnamegithub.com
expected-hostnamegithub.com
None3542e147982176a7ebaa23dfb559c8af16f721c03ec560c68c56b64a0f35e751
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releaseaf80af7cc9e3de9c336f18b208a600950a3c187c
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/101758#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F101758
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F101758
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/101758
Reloadhttps://github.com/python/cpython/issues/101758
Reloadhttps://github.com/python/cpython/issues/101758
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/101758
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 33.9k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 71.1k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.1k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects 31 https://github.com/python/cpython/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/python/cpython/security
Please reload this pagehttps://github.com/python/cpython/issues/101758
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/101758
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/101758
Isolate PyModuleDef to Each Interpreter for Extension/Builtin Moduleshttps://github.com/python/cpython/issues/101758#top
https://github.com/ericsnowcurrently
3.12only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
topic-subinterpretershttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-subinterpreters%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
https://github.com/ericsnowcurrently
https://github.com/ericsnowcurrently
ericsnowcurrentlyhttps://github.com/ericsnowcurrently
on Feb 9, 2023https://github.com/python/cpython/issues/101758#issue-1578607034
1https://github.com/python/cpython/issues/101758#user-content-fn-1-e614da24d726743870dd38178b61814e
2https://github.com/python/cpython/issues/101758#user-content-fn-2-e614da24d726743870dd38178b61814e
3https://github.com/python/cpython/issues/101758#user-content-fn-3-e614da24d726743870dd38178b61814e
gh-101755https://github.com/python/cpython/issues/101755
3https://github.com/python/cpython/issues/101758#user-content-fn-3-e614da24d726743870dd38178b61814e
quite a whilehttps://peps.python.org/pep-3121/
1https://github.com/python/cpython/issues/101758#user-content-fn-1-e614da24d726743870dd38178b61814e
gh-101758: Add a Test For Single-Phase Init Module Variants #101891https://github.com/python/cpython/pull/101891
gh-101758: Clean Up Uses of Import State #101919https://github.com/python/cpython/pull/101919
gh-101758: Add a Test For Single-Phase Init Modules in Multiple Interpreters #101920https://github.com/python/cpython/pull/101920
gh-101758: Fix the wasm Builtbots #101943https://github.com/python/cpython/pull/101943
gh-101758: Add _PyState_AddModule() Back for the Stable ABI #101956https://github.com/python/cpython/pull/101956
gh-101758: Fix Refleak Testing With test_singlephase_variants #101969https://github.com/python/cpython/pull/101969
https://github.com/python/cpython/pull/101660#issuecomment-1424507393https://github.com/python/cpython/pull/101660#issuecomment-1424507393
https://github.com/python/cpython/issues/101758#user-content-fnref-1-e614da24d726743870dd38178b61814e
↩2https://github.com/python/cpython/issues/101758#user-content-fnref-1-2-e614da24d726743870dd38178b61814e
https://github.com/python/cpython/issues/101758#user-content-fnref-2-e614da24d726743870dd38178b61814e
https://github.com/python/cpython/issues/101758#user-content-fnref-3-e614da24d726743870dd38178b61814e
↩2https://github.com/python/cpython/issues/101758#user-content-fnref-3-2-e614da24d726743870dd38178b61814e
ericsnowcurrentlyhttps://github.com/ericsnowcurrently
3.12only security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.12%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
topic-subinterpretershttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-subinterpreters%22
type-featureA feature request or enhancementhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-feature%22
Subinterpretershttps://github.com/orgs/python/projects/3
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.