Skip to content

gh-158820: Add null checks to avoid segfaults with lazy imports at shutdown - #158821

Merged
encukou merged 4 commits into
python:mainfrom
brittanyrey:b-lazy-fini-null-state
Oct 9, 2026
Merged

encukou merged 4 commits into
python:mainfrom
brittanyrey:b-lazy-fini-null-state

Conversation

@brittanyrey

@brittanyrey brittanyrey commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Additional finalizers can run after finalize_modules() has cleared the interpreter's lazy_modules set and lazy_pending_submodules dict. In the perfect storm, this can lead to a seg fault when accessing either container without checking if the object is still allocated.

  • Add null checks to call sites interacting with possibly cleared objects.
  • Adds tests repro-ing the issue

Destructors can run after finalization has cleared the interpreter's
lazy_modules set and lazy_pending_submodules dict. Declaring or
resolving a lazy import from one of them passed NULL to PySet_Add(),
PySet_Discard() or the pending-submodules lookup.

Treat the bookkeeping as a no-op once the state is gone, as the other
readers of this state already do.
Comment thread Python/import.c
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-10-05-01-59-22.gh-issue-158820.3KLyOT.rst Outdated
Comment thread Python/import.c
@brittanyrey

Copy link
Copy Markdown
Contributor Author

Addressed comments in new commit

@brettcannon
brettcannon removed their request for review October 5, 2026 21:18
@encukou
encukou merged commit a6e6361 into python:main Oct 9, 2026
86 of 104 checks passed
@encukou encukou added awaiting merge needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Oct 9, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @brittanyrey for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Oct 9, 2026

Copy link
Copy Markdown

GH-159062 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Oct 9, 2026
@brittanyrey
brittanyrey deleted the b-lazy-fini-null-state branch October 9, 2026 13:38
JelleZijlstra pushed a commit that referenced this pull request Oct 9, 2026
…s at shutdown (GH-158821) (#159062)

gh-158820: Add null checks to avoid segfaults with lazy imports at shutdown (GH-158821)

Destructors can run after finalization has cleared the interpreter's
lazy_modules set and lazy_pending_submodules dict. Declaring or
resolving a lazy import from one of them passed NULL to PySet_Add(),
PySet_Discard() or the pending-submodules lookup.

Treat the bookkeeping as a no-op once the state is gone, as the other
readers of this state already do.
(cherry picked from commit a6e6361)

Co-authored-by: Brittany Reynoso <breynoso@meta.com>
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.

4 participants