gh-149216: Notify type watchers on heap type deallocation#149236
Open
anujbharambe wants to merge 4 commits intopython:mainfrom
Open
gh-149216: Notify type watchers on heap type deallocation#149236anujbharambe wants to merge 4 commits intopython:mainfrom
anujbharambe wants to merge 4 commits intopython:mainfrom
Conversation
When a watched heap type is deallocated, type watcher callbacks were never invoked. The JIT optimizer relies on type watchers plus pointer comparisons on watched types; if a type is freed and a new type is allocated at the same address, stale JIT code could crash. Call the registered watcher callbacks from type_dealloc(), using the same _PyObject_ResurrectStart/_PyObject_ResurrectEnd pattern that dict_dealloc() already uses. The notification happens before any teardown, so callbacks can safely inspect the type object.
Documentation build overview
41 files changed ·
|
markshannon
reviewed
May 1, 2026
| i++; | ||
| bits >>= 1; | ||
| } | ||
| if (_PyObject_ResurrectEnd(self)) { |
Member
There was a problem hiding this comment.
Suggested change
| if (_PyObject_ResurrectEnd(self)) { | |
| assert(Py_REFCNT(self) == 0); |
Watchers aren't allowed to make changes like this.
Contributor
Author
There was a problem hiding this comment.
Done, replaced with the assert. Thanks for the review.
markshannon
reviewed
May 3, 2026
| i++; | ||
| bits >>= 1; | ||
| } | ||
| assert(Py_REFCNT(self) == 0); |
Member
There was a problem hiding this comment.
Sorry to keep changing my mind, but now I think about this, it might not be safe to pass type to the watcher with refcount == 0.
The docs say very little about what a watcher can or cannot do.
So I think we need to increase the refcount to 1 before calling the watchers, then reduce it by one (not using Py_DECREF to avoid recursion), then finally check if it has been resurrected, as you did before.
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.
Summary
type_dealloc()before teardown, using the_PyObject_ResurrectStart/_PyObject_ResurrectEndresurrection pattern (same asdict_dealloc())_testcapi/watchers.cthat records the type's name as a string (avoids resurrecting the type during dealloc testing)test_watch_type_deallocandtest_watch_type_dealloc_errortestsPyType_WatchCallbackmay be called during deallocationFixes #149216
CC: @markshannon
PyType_Watchdoes not report deallocations #149216