GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-158820: Add null checks to avoid segfaults with lazy imports at shutdown by brittanyrey · Pull Request #158821 · python/cpython

Conversation

@brittanyrey

@brittanyrey brittanyrey commented Oct 5, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

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
  • Issue: Segfault when a finalizer declares or resolves a lazy import during shutdown #158820

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
brittanyrey added 2 commits October 4, 2026 18:41
@brittanyrey
Fix crash when a destructor uses lazy imports during shutdown
0b5852c
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.
@brittanyrey
Remove comments on the lazy import state NULL checks
cc628a4
@brittanyrey
brittanyrey requested review from DinoV, FFY00, Yhg1s, brettcannon, ericsnowcurrently, kumaraditya303, ncoghlan, pablogsal and warsaw as code owners October 5, 2026 01:57
@bedevere-app bedevere-app Bot added the awaiting review label Oct 5, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Oct 5, 2026
Segfault when a finalizer declares or resolves a lazy import during shutdown #158820
Closed
@blurb-it
📜🤖 Added by blurb_it.
a13e2d5
pablogsal
pablogsal approved these changes Oct 5, 2026
View reviewed changes
Comment thread Python/import.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-10-05-01-59-22.gh-issue-158820.3KLyOT.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Python/import.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Oct 5, 2026
@brittanyrey
Test _imp._set_lazy_attributes() at shutdown, add a comment and rewor…
b1651ea
…d NEWS
@brittanyrey

brittanyrey commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Addressed comments in new commit

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 9, 2026
@encukou encukou added awaiting merge needs backport to 3.15 bugs and security fixes labels Oct 9, 2026
@miss-islington-app

miss-islington-app Bot commented Oct 9, 2026

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.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 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
@miss-islington @brittanyrey
[3.15] gh-158820: Add null checks to avoid segfaults with lazy import…
f8375c2
…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 <[email protected]>
@pablogsal pablogsal mentioned this pull request Oct 9, 2026
Request for Python triage membership: Brittany Reynoso python/core-workflow#609
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@StanFromIreland StanFromIreland StanFromIreland left review comments
@pablogsal pablogsal pablogsal approved these changes
@Yhg1s Yhg1s Awaiting requested review from Yhg1s Yhg1s is a code owner
@DinoV DinoV Awaiting requested review from DinoV DinoV is a code owner
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner
@FFY00 FFY00 Awaiting requested review from FFY00 FFY00 is a code owner
@kumaraditya303 kumaraditya303 Awaiting requested review from kumaraditya303 kumaraditya303 is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting merge

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@brittanyrey @pablogsal @StanFromIreland @encukou
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.