Skip to content

[3.15] gh-159963: Don't hold parent package import locks while a submodule executes (GH-159965) - #160630

Merged
gpshead merged 2 commits into
python:3.15from
miss-islington:backport-7159b15-3.15
Oct 25, 2026
Merged

gpshead merged 2 commits into
python:3.15from
miss-islington:backport-7159b15-3.15

Conversation

@miss-islington

@miss-islington miss-islington commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Since gh-83065 (3.15.0), the first import of pkg.mod kept pkg's module
lock until pkg/mod.py had finished executing. Threads that needed only
the finished pkg stayed blocked for that long, and concurrent first
imports could raise _DeadlockError or a spurious "partially initialized
module" ImportError, or hang, with no import cycle involved.

Parent packages are now imported, or waited for if another thread is
initializing them, before the submodule's module lock is taken, so only
that lock is held while the submodule executes. The lock ordering
deadlock fixed by gh-83065 stays fixed.

importlib.import_module() now returns the partially initialized module
in a circular import split across threads, as the import statement
already did, instead of raising _DeadlockError. That was long-standing
behavior rather than part of the 3.15.0 regression.
(cherry picked from commit 7159b15)

Co-authored-by: Gregory P. Smith 68491+gpshead@users.noreply.github.com

Fixes #159963

…dule executes (pythonGH-159965)

Since pythongh-83065 (3.15.0), the first import of pkg.mod kept pkg's module
lock until pkg/mod.py had finished executing.  Threads that needed only
the finished pkg stayed blocked for that long, and concurrent first
imports could raise _DeadlockError or a spurious "partially initialized
module" ImportError, or hang, with no import cycle involved.

Parent packages are now imported, or waited for if another thread is
initializing them, before the submodule's module lock is taken, so only
that lock is held while the submodule executes.  The lock ordering
deadlock fixed by pythongh-83065 stays fixed.

importlib.import_module() now returns the partially initialized module
in a circular import split across threads, as the import statement
already did, instead of raising _DeadlockError.  That was long-standing
behavior rather than part of the 3.15.0 regression.
(cherry picked from commit 7159b15)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
@gpshead
gpshead requested a review from AA-Turner as a code owner October 25, 2026 00:27
@gpshead gpshead removed the sprint label Oct 25, 2026
@read-the-docs-community

Copy link
Copy Markdown

@gpshead
gpshead merged commit 3255d5d into python:3.15 Oct 25, 2026
56 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Sprint Oct 25, 2026
@miss-islington
miss-islington deleted the backport-7159b15-3.15 branch October 25, 2026 00:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants