Skip to content

gh-159652: Fix import lock leak when the C stack is nearly exhausted - #159660

Open
cjames23 wants to merge 1 commit into
python:mainfrom
cjames23:gh-159652-import-lock-stack
Open

cjames23 wants to merge 1 commit into
python:mainfrom
cjames23:gh-159652-import-lock-stack

Conversation

@cjames23

@cjames23 cjames23 commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

When an import failed with a stack overflow RecursionError, releasing the import locks on the error path could itself raise RecursionError, because the cleanup runs in a new C frame. This left the global import lock or a module lock held forever, and later imports in other threads blocked indefinitely.

_imp.acquire_lock() now raises RecursionError when there is not enough stack left to release the lock. importlib calls it before taking any import lock, so this covers the per-module locks too. The module lock weakref callback skips cleanup in that case instead of reporting an unraisable exception.

…usted

When an import failed with a stack overflow RecursionError, releasing the import locks on the error path could itself raise RecursionError, because the cleanup runs in a new C frame. This left the global import lock or a module lock held forever, and later imports in other threads blocked indefinitely.

_imp.acquire_lock() now raises RecursionError when there is not enough stack left to release the lock. importlib calls it before taking any import lock, so this covers the per-module locks too. The module lock weakref callback skips cleanup in that case instead of
reporting an unraisable exception.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant