[mypyc] Synchronize native-to-native imports - #21887
Open
p-sawicki wants to merge 4 commits into
Open
Conversation
This comment has been minimized.
This comment has been minimized.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
p-sawicki
marked this pull request as ready for review
August 24, 2026 15:06
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #21808
Native-to-native imports within the same compilation group were sped up in #21101 to improve mypy cold-start performance on macOS, since regular imports going through the shim significantly slowed it down. With this change a native-to-native import does not go through the regular Python import machinery.
This means that native-to-native imports are not synchronized by the module lock used by the regular Python import process, which guarantees that only one thread can import a given module at a time.
Missing this synchronization means that a thread importing a native module might see it in a partially-initialized state, leading to errors. More details about this are available in comments to the linked issue.
To add synchronization, we use the module lock from the Python import library. Writing a custom native lock might be possible, but it would be difficult because of edge cases such as circular imports, modules waiting at top-level on threads importing other native modules, or interpreted and native modules concurrently importing a native module. Using the Python module lock does not regress performance, as the mypy self-check comparison does not produce a significant difference (table below). mypy cold-start time on macOS is also pretty much the same before and after this change.
mypy self-check comparison:
The module lock API is obtained when initializing a group shared library by importing from
importlib._bootstrap. When importing a native module, this API is used to obtain the lock associated with the module. This is the same lock that would be used when importing the module in an interpreted module. To ensure compatibility in case the native module is imported both through Python and natively at the same time, the native import function now setsmodule.__spec__._initializingtoTruewhen the module is being imported.There's a fast path in case the native module is imported multiple times. Each native module defines a boolean flag that is set to
trueafter it has been imported, so future imports can immediately return and not take the module lock after reading the flag. The flag is set in a wrapper over the module exec function so it will be set even if the first import is not native-to-native.