-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
gh-118331: Don't raise an error if tuple allocation fails when clearing weakrefs #118338
Conversation
It's not safe to raise an exception in `PyObject_ClearWeakRefs()` if one is not already set, since it may be called by `_Py_Dealloc()` and hit https://github.com/python/cpython/blob/5a90de0d4cbc151a6deea36a27eb81b192410e56/Objects/object.c#L2843-L2860. Additionally, make sure we clear the weakrefs even when tuple allocation fails. This bug predates pythongh-111926. If it's getting tickled now, I suspect it's because we always allocate a tuple when clearing weakrefs that have callbacks.
Depending on finalization, we may encounter another allocation failure that causes the interpreter to exit with a non-zero status. That's OK, we just need to verify that we're not exiting because weakref clearing raised an exception.
Thanks, this fixes gh-118331 for me. |
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.
Thanks!
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase And if you don't make the requested changes, you will be poked with soft cushions! |
I have made the requested changes; please review again. |
Thanks for making the requested changes! @Fidget-Spinner, @colesbury: please review the changes made to this pull request. |
It's not safe to raise an exception in
PyObject_ClearWeakRefs()
if one is not already set.PyObject_ClearWeakRefs()
may be called (transitively) by_Py_Dealloc()
, which containscpython/Objects/object.c
Lines 2843 to 2860 in 5a90de0
Additionally, make sure we clear the weakrefs when tuple allocation fails.
This bug predates gh-111926. It exists in 3.12 and likely in earlier versions too. If it's getting tickled now, I suspect it's because we always allocate a tuple now when clearing weakrefs that have callbacks, whereas previously we only did so if there was more than one weakref with a callback, making it more likely that we fail to allocate a tuple when running
test_repl.TestInteractiveInterpreter.test_no_memory
.