Tolerate concurrent sys.path_importer_cache races in the loader (#61734)#69790
Open
ggiesen wants to merge 1 commit into
Open
Tolerate concurrent sys.path_importer_cache races in the loader (#61734)#69790ggiesen wants to merge 1 commit into
ggiesen wants to merge 1 commit into
Conversation
…stack#61734) When several loaders run in one process and load modules concurrently -- e.g. a deltaproxy hosting many sub-proxy minions -- they share the global sys.path_importer_cache. importlib.invalidate_caches(), called by the loader's __clean_sys_path after each module load, walks that cache and deletes stale entries; a second loader that already removed an entry makes CPython raise KeyError (or RuntimeError if the mapping changed size mid-iteration), which propagated up and failed the module function that triggered the cleanup. Wrap invalidate_caches() so that race is tolerated (the entry was going to be invalidated regardless), and drop stale FileFinder entries from sys.path_importer_cache with pop() instead of del so a concurrent removal between the presence check and the delete cannot raise either.
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.
What does this PR do?
Makes the module loader tolerate a shared-cache race that fails module loads on
a busy deltaproxy.
When several loaders run in one process and load modules concurrently -- e.g. a
deltaproxy hosting many sub-proxy minions -- they share the global
sys.path_importer_cache. The loader's__clean_sys_pathruns after eachmodule load and calls
importlib.invalidate_caches(), which walks that cacheand deletes stale entries. If another loader already removed an entry that
CPython is walking,
invalidate_caches()raisesKeyError(orRuntimeErrorif the mapping changed size mid-iteration). That exception propagated up through
_load_moduleand failed the module function that happened to trigger thecleanup, e.g.:
The fix wraps
invalidate_caches()so the race is tolerated -- the entry wasgoing to be invalidated regardless -- and drops stale
FileFinderentries fromsys.path_importer_cachewithpop()instead ofdel, so the same concurrentremoval cannot raise between the presence check and the delete.
What issues does this PR fix or reference?
Fixes #61734
Previous Behavior
On a deltaproxy running many sub-proxy minions, a scheduled/custom module call
would intermittently fail with
KeyErrorfromimportlib.invalidate_caches.The reporter notes it scales with the number of sub-proxies per deltaproxy and
that catching the error in the loader resolves it.
New Behavior
The concurrent
sys.path_importer_cacherace is tolerated; the module loadcompletes.
Merge requirements satisfied?
tests/pytests/unit/loader/test_lazy.py::test_clean_sys_path_tolerates_shared_cache_racepatches
importlib.invalidate_cachesto raiseKeyError/RuntimeErrorandasserts
__clean_sys_pathdoes not propagate it. It fails against the currentcode. (The race itself is timing-dependent and hard to reproduce
deterministically -- matching the report -- so the test exercises the tolerance
directly.)
Independent of #69787 (both touch
salt/loader/lazy.pybut in differentfunctions --
__populate_sys_path/_load_modulethere vs__clean_sys_pathhere -- so they merge cleanly).
Commits signed with GPG?
No