GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-44050: Extension modules can share state when they don't support sub-interpreters. by shihai1991 · Pull Request #27794 · python/cpython

Conversation

@shihai1991

@shihai1991 shihai1991 commented Aug 17, 2021 •
edited by miss-islington
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

https://bugs.python.org/issue44050

Automerge-Triggered-By: GH:encukou

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@shihai1991
Extension modules created from PyModule_Create() can share state
4ffadb4
@shihai1991
shihai1991 requested review from brettcannon, encukou, ericsnowcurrently, ncoghlan and warsaw as code owners August 17, 2021 12:23
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Aug 17, 2021
@bedevere-bot bedevere-bot added the awaiting review label Aug 17, 2021
@shihai1991 shihai1991 added the skip news label Aug 17, 2021
@shihai1991 shihai1991 changed the title Extension modules created from PyModule_Create() can share state. bpo-44050: Extension modules created from PyModule_Create() can share state. Aug 17, 2021
@encukou

encukou commented Aug 17, 2021

Copy link
Copy Markdown
Member

I don't understand this PR. It allows Python objects to be shared between interpreters, doesn't it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shihai1991

shihai1991 commented Aug 17, 2021

Copy link
Copy Markdown
Member Author

I don't understand this PR. It allows Python objects to be shared between interpreters, doesn't it?

Hi, petr. The details in this PR: 82c83bd
Only the main interp can update the extension and def.m_base.m_copy now. So I use the def->m_slots to identify the extension module created from PyModule_Create() or not. If extension module created from the PyModule_Create(), keep the old behavior. cc @vstinner

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented Aug 17, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

The relevant check is done by the def->m_size == -1. Extensions that use multi-phase init must set a non-negative m_size.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shihai1991

shihai1991 commented Aug 17, 2021 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

The relevant check is done by the def->m_size == -1. Extensions that use multi-phase init must set a non-negative m_size.

Hm. I agree with you. But the user of bpo-44050 got the error in v3.9.6. And I found some module created from PyModule_Create() use m_size. For example: https://github.com/python/cpython/blob/v3.9.6/Modules/_posixsubprocess.c#L986

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shihai1991

shihai1991 commented Aug 17, 2021

Copy link
Copy Markdown
Member Author

This PR can be removed when all the extension modules convert to multi-phase init.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented Aug 17, 2021

Copy link
Copy Markdown
Member

Why is it important to ask whether the module was created by PyModule_Create?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented Aug 17, 2021

Copy link
Copy Markdown
Member

This PR can be removed when all the extension modules convert to multi-phase init.

That includes third-party modules, so: this PR can be removed when the API for single-phase init is removed.

shihai1991 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shihai1991

shihai1991 commented Aug 17, 2021

Copy link
Copy Markdown
Member Author

Why is it important to ask whether the module was created by PyModule_Create?

Make sure only the single extension module shared between interpreters. The extension module from multi-phase init can be created seperately in subinterpreter, right?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented Aug 17, 2021

Copy link
Copy Markdown
Member

All modules with non-negative m_size shouldn't be shared across interpreters, regardless of how they're created.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shihai1991

shihai1991 commented Aug 17, 2021

Copy link
Copy Markdown
Member Author

All modules with non-negative m_size shouldn't be shared across interpreters, regardless of how they're created.

Make sense. Looks your idea would be better. Thanks:) I will update it soon.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shihai1991
apply petr's comment
2d2165c
@shihai1991
shihai1991 force-pushed the bpo_44050 branch from 63c1b4c to 2d2165c Compare August 17, 2021 14:42
@vstinner

vstinner commented Aug 17, 2021

Copy link
Copy Markdown
Member

_PyImport_FixupExtensionObject() copies the namespace from the first instance of an extension, to newly created instances of the extension. It's better than using exactly the same module object with the same dict dictionary object. In a perfect world, all extensions would use multi-phase init and so don't go through _PyImport_FixupExtensionObject().

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner approved these changes Aug 17, 2021
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but can you try to write an unit test?

Would it be possible to write an unit test for this? https://bugs.python.org/issue44050 is a regression. It would be better to not reintroduce it by mistake tomorrow.

Maybe using the ssl module: if if it's already imported, skip the test. Otherwise, spawn a subinterpreter to import it, and then import it in the main interpreter, and compare if you get the same objects or not.

See wee-slack/wee-slack#812 (comment) for a more complete example.

To make sure that the ssl module is not imported yet, you can run the test in a subprocess, eg. using assert_python_ok(), or subprocess.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Aug 17, 2021
@shihai1991

shihai1991 commented Aug 17, 2021

Copy link
Copy Markdown
Member Author

LGTM, but can you try to write an unit test?

Would it be possible to write an unit test for this? https://bugs.python.org/issue44050 is a regression. It would be better to not reintroduce it by mistake tomorrow.

Maybe using the ssl module: if if it's already imported, skip the test. Otherwise, spawn a subinterpreter to import it, and then import it in the main interpreter, and compare if you get the same objects or not.

See wee-slack/wee-slack#812 (comment) for a more complete example.

To make sure that the ssl module is not imported yet, you can run the test in a subprocess, eg. using assert_python_ok(), or subprocess.

OK, victor. I will update it.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shihai1991 shihai1991 changed the title bpo-44050: Extension modules created from PyModule_Create() can share state. bpo-44050: Extension modules doesn't support sub-interpreters can share state. Aug 17, 2021
@encukou

encukou commented Aug 18, 2021

Copy link
Copy Markdown
Member

Maybe using the ssl module

That won't do; ssl uses multi-phase init (as should all other stdlib modules, one day). This would need to be a new module dedicated for the test.
You can look at Modules/_testmultiphase.c and Lib/test/test_capi.py for an example of adding a new module without adding a new shared library.

shihai1991 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shihai1991 shihai1991 closed this Aug 21, 2021
@encukou

encukou commented Sep 7, 2021

Copy link
Copy Markdown
Member

Looks good, although it looks like it'll only be backported to 3.10.1.
Could you add a NEWS entry, though? We should have those for bugfixes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou encukou removed the skip news label Sep 7, 2021
@trygveaa

trygveaa commented Sep 7, 2021

Copy link
Copy Markdown

Looks good, although it looks like it'll only be backported to 3.10.1.

What do you mean? The regression is in 3.9, the issue is fixed in 3.10, and 3.10.1 doesn't exist yet. Did you mean 3.9.1?

jonorthwash reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@shihai1991
add a NEWS entry
d53e2dd
@shihai1991

shihai1991 commented Sep 7, 2021

Copy link
Copy Markdown
Member Author

Looks good, although it looks like it'll only be backported to 3.10.1.

What do you mean? The regression is in 3.9, the issue is fixed in 3.10, and 3.10.1 doesn't exist yet. Did you mean 3.9.1?

Maybe we cloud backport to 3.9 and 3.10?

jonorthwash reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jonorthwash jonorthwash mentioned this pull request Oct 1, 2021
Error while doing SSL handshake: Unknown error poljar/weechat-matrix#248
Open
encukou
encukou reviewed Oct 5, 2021
View reviewed changes
Comment thread Misc/NEWS.d/next/Core and Builtins/2021-09-08-00-30-09.bpo-44050.mFI15u.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@encukou

encukou commented Oct 5, 2021

Copy link
Copy Markdown
Member

What do you mean? The regression is in 3.9, the issue is fixed in 3.10, and 3.10.1 doesn't exist yet. Did you mean 3.9.1?

The "fix" in 3.10 is in the _ssl module only – an unrelated change that made the module avoid this bug.

Backporting to 3.10.1 & the next 3.9 sounds fine.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@encukou encukou added needs backport to 3.9 labels Oct 5, 2021
@encukou
Update Misc/NEWS.d/next/Core and Builtins/2021-09-08-00-30-09.bpo-440…
eb9b40d
…50.mFI15u.rst
@encukou encukou changed the title bpo-44050: Extension modules can share state when it doesn't support sub-interpreters. bpo-44050: Extension modules can share state when they don't support sub-interpreters. Oct 5, 2021
encukou
encukou approved these changes Oct 5, 2021
View reviewed changes
@encukou encukou added the 🤖 automerge label Oct 5, 2021
@miss-islington

miss-islington commented Oct 5, 2021

Copy link
Copy Markdown
Contributor

@shihai1991: Status check is done, and it's a success ✅ .

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington
miss-islington merged commit b9bb748 into python:main Oct 5, 2021
@miss-islington

miss-islington commented Oct 5, 2021

Copy link
Copy Markdown
Contributor

Thanks @shihai1991 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the awaiting merge label Oct 5, 2021
@miss-islington

miss-islington commented Oct 5, 2021

Copy link
Copy Markdown
Contributor

Sorry, @shihai1991, I could not cleanly backport this to 3.9 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker b9bb74871b27d9226df2dd3fce9d42bda8b43c2b 3.9

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 5, 2021
@shihai1991 @miss-islington
bpo-44050: Extension modules can share state when they don't support …
a4cd7e9
…sub-interpreters. (pythonGH-27794)

Automerge-Triggered-By: GH:encukou
(cherry picked from commit b9bb748)

Co-authored-by: Hai Shi <[email protected]>
@miss-islington miss-islington self-assigned this Oct 5, 2021
@bedevere-bot

bedevere-bot commented Oct 5, 2021

Copy link
Copy Markdown

GH-28738 is a backport of this pull request to the 3.10 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 label Oct 5, 2021
@shihai1991

shihai1991 commented Oct 5, 2021

Copy link
Copy Markdown
Member Author

Thanks Petr, Victor for your review and merge.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ambv pushed a commit to ambv/cpython that referenced this pull request Oct 5, 2021
@shihai1991 @ambv
[3.9] bpo-44050: Extension modules can share state when they don't su…
b83a4ff
…pport sub-interpreters. (pythonGH-27794)

Automerge-Triggered-By: GH:encukou.
(cherry picked from commit b9bb748)

Co-authored-by: Hai Shi <[email protected]>
@bedevere-bot

bedevere-bot commented Oct 5, 2021

Copy link
Copy Markdown

GH-28741 is a backport of this pull request to the 3.9 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 label Oct 5, 2021
ambv pushed a commit that referenced this pull request Oct 5, 2021
@miss-islington @shihai1991
bpo-44050: Extension modules can share state when they don't support …
d0d2965
…sub-interpreters. (GH-27794) (GH-28738)

Automerge-Triggered-By: GH:encukou
(cherry picked from commit b9bb748)

Co-authored-by: Hai Shi <[email protected]>
ambv added a commit that referenced this pull request Oct 5, 2021
@ambv @shihai1991
[3.9] bpo-44050: Extension modules can share state when they don't su…
52d9d3b
…pport sub-interpreters. (GH-27794) (GH-28741)

(cherry picked from commit b9bb748)

Co-authored-by: Hai Shi <[email protected]>
@encukou

encukou commented Oct 12, 2021

Copy link
Copy Markdown
Member

And thanks for your patience; this certainly waited for longer than it should have.

shihai1991 reacted with laugh emoji
All reactions
  • 😄 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner
@vstinner vstinner Awaiting requested review from vstinner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@miss-islington miss-islington

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

7 participants

@shihai1991 @encukou @vstinner @bedevere-bot @trygveaa @miss-islington @the-knights-who-say-ni
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.