Skip to content

GH-91636: Clear weakrefs created by finalizers. #136401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

nascheme
Copy link
Member

@nascheme nascheme commented Jul 8, 2025

Weakrefs to unreachable garbage that are created during running of finalizers need to be cleared. This avoids exposing objects that have tp_clear called on them to Python-level code.

Note that this is part of the GH-136189 change, split out to make it easier for back-porting. This change is conceptually quite simple and is a candidate to backport to all older Python versions (this bug existed for a long time, perhaps in version 2.3). There is some duplicated code between handle_finalizers and clear_finalizers but I think it's simpler just to leave that duplication. In the "main" branch I'll clean that up.

Weakrefs to unreachable garbage that are created during running of
finalizers need to be cleared.  This avoids exposing objects that
have `tp_clear` called on them to Python-level code.
@nascheme nascheme added 3.13 bugs and security fixes 3.14 bugs and security fixes 3.15 new features, bugs and security fixes labels Jul 8, 2025
@nascheme nascheme marked this pull request as ready for review July 8, 2025 01:15
@nascheme nascheme requested review from colesbury and pablogsal July 8, 2025 01:15
@nascheme
Copy link
Member Author

nascheme commented Jul 8, 2025

@colesbury In case you want to look at the free_threading_gc changes.

@sergey-miryanov
Copy link
Contributor

Looks good to me.

Add a boolean flag to indicate that callbacks should be enabled.  This
allows the same function to be used before and after finalizer
execution.
if (!allow_callbacks) {
return 0;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is worth to add an assert here for old != NULL?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar question: The old and allow_callbacks arguments are orthogonal even in older versions?

@nascheme nascheme merged commit b6b99bf into python:main Jul 8, 2025
40 checks passed
AndPuQing pushed a commit to AndPuQing/cpython that referenced this pull request Jul 11, 2025
Weakrefs to unreachable garbage that are created during running of
finalizers need to be cleared.  This avoids exposing objects that
have `tp_clear` called on them to Python-level code.
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
Weakrefs to unreachable garbage that are created during running of
finalizers need to be cleared.  This avoids exposing objects that
have `tp_clear` called on them to Python-level code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes 3.15 new features, bugs and security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy