-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Race condition when importing collections.abc
#125245
Comments
Lock `ZoneInfoType` to protect accesses to `ZONEINFO_STRONG_CACHE`. Refactor the `tp_new` handler to use Argument Clinic so that we can just use `@critical_section` annotations on the relevant functions. Also use `PyDict_SetDefaultRef` instead of `PyDict_SetDefault` when inserting into the `TIMEDELTA_CACHE`.
EDIT: Commented on wrong issue |
There's a race condition that I believe was introduced in #123613. The cpython/Lib/importlib/_bootstrap.py Lines 1351 to 1355 in a726ce7
I think this may affect the default (with GIL) build as well depending on when the GIL-switch happens, but it definitely will happen more often on the free threading build. The problematic execution when two threads (T1, T2) run
T2 now incorrectly has the Python EDIT: |
I think we can do something like: --- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -29,6 +29,9 @@
import _collections_abc
import sys as _sys
+_sys.modules['collections.abc'] = _collections_abc
+abc = _collections_abc
+
from itertools import chain as _chain
from itertools import repeat as _repeat
from itertools import starmap as _starmap
--- a/Lib/collections/abc.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import _collections_abc
-import sys
-sys.modules[__name__] = _collections_abc |
collections.abc
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.
Indeed. We see this in Fedora with the default (with GIL) build as well. |
This is fixed now. |
There's an open PR, but it's not merged yet. I don't think the underlying issue is fixed. I'm waiting on more feedback regarding the issue that Serhiy raised on the PR: |
Oops, I'm sorry! |
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.
…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 <[email protected]>
…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 <[email protected]>
The PRs are merged now |
Bug report
Bug description:
Discovered alongside #125243 with similar steps to reproduce. I don't have a simpler way to trigger this than "run the PyO3 tests in a loop" because I think it requires many threads accessing the python runtime simulatenously.
To trigger it, have a rust toolchain and Python installed, clone the PyO3 repo and execute the following command:
You may also hit some other test failures related to ZoneInfo, see the other issue I opened about that.
You will eventually see a test failure with the following text:
I slightly modified PyO3 to get a traceback as well (hidden because it's a distractingly long diff):
And the traceback is:
So somehow during setup of the
socket
module,Mapping
isn't available yet, but only if many threads are simultaneously touching the Python runtime.(ping @davidhewitt, we probably want to disable the socket error tests on the free-threaded build as well)
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Linked PRs
collections.abc
#125415collections.abc
(GH-125415) #125944The text was updated successfully, but these errors were encountered: