Skip to content

[3.14] gh-159652: Fix import lock leak when the C stack is nearly exhausted (GH-159660) - #160780

Merged
brettcannon merged 1 commit into
python:3.14from
miss-islington:backport-926b0bf-3.14
Oct 27, 2026
Merged

brettcannon merged 1 commit into
python:3.14from
miss-islington:backport-926b0bf-3.14

Conversation

@miss-islington

@miss-islington miss-islington commented Oct 27, 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.
(cherry picked from commit 926b0bf)

Co-authored-by: Cary Hawkins hawkinscary23@gmail.com

…usted (pythonGH-159660)

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.
(cherry picked from commit 926b0bf)

Co-authored-by: Cary Hawkins <hawkinscary23@gmail.com>
@brettcannon
brettcannon enabled auto-merge (squash) October 27, 2026 21:46
@brettcannon brettcannon self-assigned this Oct 27, 2026
@brettcannon
brettcannon merged commit 48b1e60 into python:3.14 Oct 27, 2026
55 checks passed
@miss-islington
miss-islington deleted the backport-926b0bf-3.14 branch October 27, 2026 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants