gh-116946: Implement the GC protocol for _tkinter tkapp and tktimertoken#152310
Open
serhiy-storchaka wants to merge 1 commit into
Open
gh-116946: Implement the GC protocol for _tkinter tkapp and tktimertoken#152310serhiy-storchaka wants to merge 1 commit into
serhiy-storchaka wants to merge 1 commit into
Conversation
…imertoken The _tkinter.tkapp and _tkinter.tktimertoken types never implemented the garbage collector protocol, so reference cycles through an interpreter's trace function or a timer handler's callback could not be collected. A pending timer is kept alive by the Tcl event loop, which fires it even after the Python token is dropped, so it is treated as a GC root (only its callback is traversed) and collecting it never cancels a live timer. The GC slots use a plain cast rather than the type-checking macro, since the collector may visit a surviving object at shutdown after module clearing has reset the global type pointers. Deallocation cancels any pending timer so its callback cannot run on freed memory. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.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
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.
The internal
_tkinter.tkappand_tkinter.tktimertokentypes are heap types implemented in C but never implemented the garbage collector protocol, so reference cycles through a Tcl interpreter's trace function or a timer handler's callback could not be collected.They now implement
tp_traverseandtp_clear, setPy_TPFLAGS_HAVE_GC, allocate through the type'stp_alloc, and untrack on deallocation.Two design points differ from the earlier, reverted attempt (gh-138331, reverted by gh-138807 over the gh-138789/gh-138791 regression):
test_timer_fires_after_gcguards this.test_pending_timer_at_shutdownguards this.This is independent of and complementary to gh-80937 / #152294: it does not traverse the command table, so it does not collect the
createcommandcycle, and that fix does not collect interpreter/timer cycles.