-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-135552: Reset type cache after finalization to prevent possible segfault while garbage collecting #135728
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
base: main
Are you sure you want to change the base?
gh-135552: Reset type cache after finalization to prevent possible segfault while garbage collecting #135728
Conversation
@ZeroIntensity @sobolevn @Eclips4 @efimov-mikhail @fxeqxmulfx Please take a look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add a test case and a NEWS entry.
I think that there's a minimal repro available in the issue.
The solution seems reasonable!
Would you mind adding a benchmark for this commit by comment? |
Yeah, as I said, this seems to be a problem with the GIL-ful garbage collector, not the object model. Something like this would also cause problems on the free-threaded build, but FT is working perfectly. |
@ZeroIntensity The root cause of this error is the absence of subclasses for BaseNode class. Therefore, the I have the following code, and I believe the problem is common to both:
As you can see, the weakref is None, but self has not been destroyed yet. I believe the garbage collector has a mechanism to handle weakrefs, but it runs too early, before |
Ah, and there's no type cache under free-threading. Interesting! |
While trying to find the root cause of the linked issue, I observed that the type cache was holding an obsolete reference. Because the type cache holds borrowed references, this leads to a use-after-free error.
I'm resetting the cache in this PR. But I suspect that this may affect overall performance.