Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-125245: Fix race condition when importing collections.abc #125415

Merged
merged 1 commit into from
Oct 24, 2024

Conversation

colesbury
Copy link
Contributor

@colesbury colesbury commented Oct 13, 2024

If multiple threads concurrently imported collections.abc, some of the threads might incorrectly see the "shim" Lib/collections/abc.py module instead of the correct Lib/_collections_abc.py module. This was observed in the free threading build, but could, in theory, occur in the default GIL-enabled build as well.

If multiple threads concurrently imported `collections.abc`, some of the
threads might incorrectly see the "shim" `Lib/collections/abc.py` module
instead of the correct `Lib/_collections_abc.py` module.  This was
observed in the free threading build, but could, in theory, occur in the
default GIL-enabled build as well.
@colesbury
Copy link
Contributor Author

This strategy won't work for decimal / _pydecimal. I'm not sure how to handle that, but I'm less concerned about that case because in normal circumstances decimal uses the C _decimal module instead.

@colesbury colesbury marked this pull request as ready for review October 13, 2024 20:52
@colesbury colesbury requested a review from rhettinger as a code owner October 13, 2024 20:52
@colesbury
Copy link
Contributor Author

cc @davidhewitt @ngoldbaum

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

The side effect is that user no longer needs to import collections.abc explicitly if they imported collections. It will be hard to change this back because users will write code that depends on this. Is it fine? cc @rhettinger

@ngoldbaum
Copy link
Contributor

I applied this patch onto the 3.13 branch and I'm no longer able to reproduce the error I reported in #125245 by running the PyO3 tests repeatedly.

@Yhg1s
Copy link
Member

Yhg1s commented Oct 24, 2024

We have other modules that do this (e.g. multiprocessing.context, not to mention the weird case of os.path). Using modules in packages without explicitly importing them is a bigger problem, because something else might have imported it for you. I think for collections.abc I think it's fine for collections to do the importing. It's a special case anyway because they're effectively builtin types and collections is already importing the module anyway, just not the shim. In the unlikely event we really want to decouple collections.abc from collections (which requires that collections not use anything from collections.abc, which seems extremely unlikely), we can always use __getattr__ in collections to load it lazily.

@colesbury colesbury merged commit fed501d into python:main Oct 24, 2024
38 checks passed
@miss-islington-app
Copy link

Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

@colesbury colesbury deleted the gh-125245-collections-abc branch October 24, 2024 22:12
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 24, 2024
…ythonGH-125415)

If multiple threads concurrently imported `collections.abc`, some of the
threads might incorrectly see the "shim" `Lib/collections/abc.py` module
instead of the correct `Lib/_collections_abc.py` module.  This affected
both the free threading build and the default GIL-enabled build.
(cherry picked from commit fed501d)

Co-authored-by: Sam Gross <colesbury@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Oct 24, 2024

GH-125944 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Oct 24, 2024
colesbury added a commit that referenced this pull request Oct 24, 2024
…GH-125415) (GH-125944)

If multiple threads concurrently imported `collections.abc`, some of the
threads might incorrectly see the "shim" `Lib/collections/abc.py` module
instead of the correct `Lib/_collections_abc.py` module.  This affected
both the free threading build and the default GIL-enabled build.
(cherry picked from commit fed501d)

Co-authored-by: Sam Gross <colesbury@gmail.com>
jacobtylerwalls added a commit to pylint-dev/astroid that referenced this pull request Dec 7, 2024
jacobtylerwalls added a commit to pylint-dev/astroid that referenced this pull request Dec 8, 2024
Pierre-Sassoulas pushed a commit to pylint-dev/astroid that referenced this pull request Dec 8, 2024
jacobtylerwalls added a commit to jacobtylerwalls/astroid that referenced this pull request Dec 8, 2024
jacobtylerwalls added a commit to pylint-dev/astroid that referenced this pull request Dec 8, 2024
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
…ython#125415)

If multiple threads concurrently imported `collections.abc`, some of the
threads might incorrectly see the "shim" `Lib/collections/abc.py` module
instead of the correct `Lib/_collections_abc.py` module.  This affected
both the free threading build and the default GIL-enabled build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants