Skip to content

gh-145921: Clarify return value of tp_traverse - #160455

Merged
encukou merged 3 commits into
python:mainfrom
encukou:tp_traverse-retval
Oct 28, 2026
Merged

encukou merged 3 commits into
python:mainfrom
encukou:tp_traverse-retval

Conversation

@encukou

@encukou encukou commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

@encukou
encukou requested a review from nascheme October 23, 2026 17:49
@bedevere-app bedevere-app Bot added awaiting core review type-feature A feature request or enhancement labels Oct 23, 2026
@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news labels Oct 23, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Docs PRs Oct 23, 2026
@read-the-docs-community

read-the-docs-community Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

@nascheme nascheme left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LTGM aside from the "unreachable" wording.

Comment thread Doc/c-api/gcsupport.rst Outdated

If an error occurs during traversal, ignore it and return 0.
If ``tp_traverse`` fails to visit some strong references, objects
may become unreachable and waste memory, but correctness won't be affected.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would say something like "garbage reference cycles will not be broken and the program may waste memory". If you want to use to word "unreachable", I would say something like "the GC will not reliability determine which objects are unreachable and are only alive due to circular references"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or maybe more precisely "garbage reference cycles involving the skipped strong references will not be reliably broken". I think that's accurate. The GC will still be able to break up cyclic garbage involving the working tp_traverse methods (i.e. one failing tp_traverse doesn't prevent the cyclic GC from functioning entirely, which could be wrongly inferred). OTOH, maybe we want to be careful about promising too much. As the GC is now, it's true that other garbage reference cycles can be broken, even in the face of some tp_traverse methods not revealing all strong references. But maybe that's too strong of a promise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we can guarantee that. If one incorrect tp_traverse could gum up the entire garbage collector, we'd have a big problem.
Also, “involving” isn't a very precise term...

@encukou encukou added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 28, 2026
@encukou
encukou merged commit cfe7e0a into python:main Oct 28, 2026
33 of 34 checks passed
@encukou
encukou deleted the tp_traverse-retval branch October 28, 2026 14:49
@miss-islington-app

Copy link
Copy Markdown

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

@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs Oct 28, 2026
@miss-islington-app

Copy link
Copy Markdown

Sorry, @encukou, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker cfe7e0a5ea5043991492ee71a8c6a322172b353a 3.14

@bedevere-app

bedevere-app Bot commented Oct 28, 2026

Copy link
Copy Markdown

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 28, 2026
encukou added a commit that referenced this pull request Oct 28, 2026
…-160826)

(cherry picked from commit cfe7e0a)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir needs backport to 3.14 bugs and security fixes skip news type-feature A feature request or enhancement

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants