You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 43ca27d
Browse filesBrowse the repository at this point in the historyBrowse files
[mypyc] Fix missing module during concurrent circular import (python#22027)
The test `testConcurrentCircularNativeImports` occasionally fails by
raising a `KeyError` exception when looking up a module in
`sys.modules`.
This appears to be caused by looking up the module in the middle of a
reinsert operation done by importlib where the module is popped from
`sys.modules` and inserted into it right after to reorder it -
[Example](https://github.com/python/cpython/blob/3.14/Lib/importlib/_bootstrap.py#L949).
During a circular import a thread participating in the cycle may not
obtain the module lock because of deadlock detection. On a free-threaded
build the thread holding the lock may concurrently perform the importlib
reinsert and a thread without the lock may perform the look-up.
To fix this, skip the look-up when possible after a native import and
use the module object returned from the native import function. This
also has the benefit of speeding up imports using `from native import x`
statements by removing one module look-up.
The module is also looked up after failing to acquire the module lock
due to deadlock to obtain the partial module. If the module is missing
here due to the race, for a module that has already done executing we
can retry because its top-level statements cannot block other threads.
The importlib reinsert happens only after a module has done executing,
so otherwise the race shouldn't happen and the partial module missing
from `sys.modules` is some other error.
Also report an error when the `sys.modules` entry of a given native
module is replaced/removed inside its own top-level function as this was
inconsistent before.
0 commit comments