GH-83065: Fix import deadlock by implementing hierarchical module locking#137196
Draft
gpshead wants to merge 1 commit intopython:mainfrom
Draft
GH-83065: Fix import deadlock by implementing hierarchical module locking#137196gpshead wants to merge 1 commit intopython:mainfrom
gpshead wants to merge 1 commit intopython:mainfrom
Conversation
…le locking Fix a deadlock in the import system that occurs when concurrent threads import modules at different levels of the same package hierarchy while `__init__.py` files have circular imports. The deadlock scenario (correctly analyzed by @emmatyping): - Thread 1 imports `package.subpackage`, which imports `package.subpackage.module` in its `__init__.py` - Thread 2 imports `package.subpackage.module`, which needs to ensure `package.subpackage` is loaded first - Each thread holds a lock the other needs, causing deadlock The fix introduces _HierarchicalLockManager that acquires all necessary module locks in a consistent order (parent before child) for nested modules. This ensures all threads acquire locks in the same order, preventing circular wait conditions. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
gpshead
commented
Jul 29, 2025
| # Only acquire lock if module is not already fully loaded | ||
| module = sys.modules.get(module_name) | ||
| if (module is None or | ||
| getattr(getattr(module, "__spec__", None), "_initializing", False)): |
Member
Author
There was a problem hiding this comment.
is this the right way to do this?
Member
There was a problem hiding this comment.
In _get_module_lock a sentinel object is the default for sys.modules.get(), so that might be better. Though I think using None as a sentinel should be fine?
| def _get_module_chain(self, name): | ||
| """Get all modules in the hierarchy from root to leaf. | ||
|
|
||
| For example: 'a.b.c' -> ['a', 'a.b', 'a.b.c'] |
Member
Author
There was a problem hiding this comment.
do we already have a function to do this? also - it's a function and does not need to be a method.
Member
|
I don't feel qualified to say this PR is correct, as import logic has a lot of nasty reentrant behavior I haven't dove into, but the PR does look right, based on what I imagined an implementation of a solution might be. |
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.
Summary
Fix a deadlock in the import system that occurs when concurrent threads import modules at different levels of the same package hierarchy while
__init__.pyfiles have circular imports.The Problem
As correctly analyzed by @emmatyping in the issue, the deadlock occurs when:
package.subpackage, which importspackage.subpackage.modulein its__init__.pypackage.subpackage.module, which needs to ensurepackage.subpackageis loaded first_DeadlockErrorThe Solution
This PR introduces
_HierarchicalLockManagerthat acquires all necessary module locks in a consistent order (parent modules before child modules) for nested module imports. This ensures all threads acquire locks in the same hierarchical order, preventing the circular wait condition that causes deadlocks.Key Changes:
_HierarchicalLockManagerclass inLib/importlib/_bootstrap.py_find_and_load()to use hierarchical locking for nested modules (those containing '.')test_hierarchical_import_deadlockto prevent regression(human edit: Please take Claude's performance claims with a grain of salt here... it made some of its own but we need to run real ones.)
Performance Impact
Benchmarking shows minimal performance impact:
Test Plan
Fixes #83065
🤖 Generated with Claude Code