FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-158820: Add null checks to avoid segfaults with lazy imports at shutdown by brittanyrey · Pull Request #158821 · python/cpython · GitHub

Repository navigation

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 commented Oct 5, 2026 •
edited by bedevere-app Bot
Loading

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 Python/import.c

Copy link
Copy Markdown
Contributor Author

Addressed comments in new commit

brettcannon removed their request for review October 5, 2026 21:18
encukou merged commit a6e6361 into python:main Oct 9, 2026
86 of 104 checks passed

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 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 Bot removed the needs backport to 3.15 bugs and security fixes label Oct 9, 2026
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>
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL