-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
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-118789: Add PyUnstable_Object_ClearWeakRefsNoCallbacks
#118807
gh-118789: Add PyUnstable_Object_ClearWeakRefsNoCallbacks
#118807
Conversation
This exposes `_PyWeakref_ClearWeakRefsExceptCallbacks` as an unstable C-API function to provide a thread-safe mechanism for clearing weakrefs without executing callbacks. Some C-API extensions need to clear weakrefs without calling callbacks, such as after running finalizers like we do in subtype_dealloc. Previously they could use `_PyWeakref_ClearRef` on each weakref, but that's not thread-safe in the free-threaded build.
I suggest to rename the function to:
|
Objects/weakrefobject.c
Outdated
@@ -1090,6 +1090,12 @@ _PyWeakref_ClearWeakRefsExceptCallbacks(PyObject *obj) | |||
UNLOCK_WEAKREFS(obj); | |||
} | |||
|
|||
void | |||
PyUnstable_Weakref_ClearWeakRefsExceptCallbacks(PyObject *obj) | |||
{ |
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.
I suggest to check if _PyType_SUPPORTS_WEAKREFS(Py_TYPE(op))
and do nothing (return) if it's not the case.
We already have The use case for both functions is that types with user finalizers (i.e.,
The third step avoids leaking memory in case the finalizer added new weakrefs. We do this internally in Previously, the third step was implemented as:
But that's not thread-safe without the GIL.
I'll adjust the name and add the check you suggest. |
Why do you want to put it in the PyUnstable API? |
That's what I understood from @Yhg1s's comments on Discord (#c-api) as the appropriate name/stability for 3.13. I'd certainly prefer something outside of the PyUnstable API as the end state for this (i.e., in 3.14). |
I suggest to only do the revert in 3.13, and add the function only in 3.14. |
If we only add the function in 3.14, then there will be no way for C-API extensions that need this functionality to be made thread-safe in 3.13. |
I'll open a https://github.com/capi-workgroup/decisions issue for 3.14 |
PyUnstable_Weakref_ClearWeakRefsExceptCallbacks
PyUnstable_Object_ClearWeakRefsExceptCallbacks
PyUnstable_Object_ClearWeakRefsExceptCallbacks
PyUnstable_Object_ClearWeakRefsNoCallbacks
@encukou, would you please review this? |
|
||
Clears the weakrefs for *object* without calling the callbacks. | ||
|
||
.. versionadded:: 3.13 |
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.
IMO it's too late for 3.13, we're past beta1.
.. versionadded:: 3.13 | |
.. versionadded:: 3.14 |
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.
@Yhg1s - is it okay to make this function public in 3.13? From our discussion on Discord in May, it sounded like you were okay with this before the RC, but I'd like to make sure.
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.
Yes, considering the circumstances, this is reasonable to add in beta 3.
Unstable API needs tests; see colesbury#2. Otherwise, this looks good to me. @Yhg1s, do you want this in 3.13? As mentioned in the issue, projects that reimplement |
…RefsExceptCallbacks
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.
LGTM. For the version number (.. versionadded::
in the doc), it depends if you backport the change to 3.13. If no, you should use 3.14.
Thanks @colesbury for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
…thonGH-118807) This exposes `PyUnstable_Object_ClearWeakRefsNoCallbacks` as an unstable C-API function to provide a thread-safe mechanism for clearing weakrefs without executing callbacks. Some C-API extensions need to clear weakrefs without calling callbacks, such as after running finalizers like we do in subtype_dealloc. Previously they could use `_PyWeakref_ClearRef` on each weakref, but that's not thread-safe in the free-threaded build. (cherry picked from commit e8752d7) Co-authored-by: Sam Gross <colesbury@gmail.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
GH-120695 is a backport of this pull request to the 3.13 branch. |
Congrats @colesbury :-) PyTorch should be happy with this new API if I understood correctly. |
…H-118807) (#120695) This exposes `PyUnstable_Object_ClearWeakRefsNoCallbacks` as an unstable C-API function to provide a thread-safe mechanism for clearing weakrefs without executing callbacks. Some C-API extensions need to clear weakrefs without calling callbacks, such as after running finalizers like we do in subtype_dealloc. Previously they could use `_PyWeakref_ClearRef` on each weakref, but that's not thread-safe in the free-threaded build. (cherry picked from commit e8752d7) Co-authored-by: Sam Gross <colesbury@gmail.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
…thon#118807) This exposes `PyUnstable_Object_ClearWeakRefsNoCallbacks` as an unstable C-API function to provide a thread-safe mechanism for clearing weakrefs without executing callbacks. Some C-API extensions need to clear weakrefs without calling callbacks, such as after running finalizers like we do in subtype_dealloc. Previously they could use `_PyWeakref_ClearRef` on each weakref, but that's not thread-safe in the free-threaded build. Co-authored-by: Petr Viktorin <encukou@gmail.com>
…thon#118807) This exposes `PyUnstable_Object_ClearWeakRefsNoCallbacks` as an unstable C-API function to provide a thread-safe mechanism for clearing weakrefs without executing callbacks. Some C-API extensions need to clear weakrefs without calling callbacks, such as after running finalizers like we do in subtype_dealloc. Previously they could use `_PyWeakref_ClearRef` on each weakref, but that's not thread-safe in the free-threaded build. Co-authored-by: Petr Viktorin <encukou@gmail.com>
…thon#118807) This exposes `PyUnstable_Object_ClearWeakRefsNoCallbacks` as an unstable C-API function to provide a thread-safe mechanism for clearing weakrefs without executing callbacks. Some C-API extensions need to clear weakrefs without calling callbacks, such as after running finalizers like we do in subtype_dealloc. Previously they could use `_PyWeakref_ClearRef` on each weakref, but that's not thread-safe in the free-threaded build. Co-authored-by: Petr Viktorin <encukou@gmail.com>
…thon#118807) This exposes `PyUnstable_Object_ClearWeakRefsNoCallbacks` as an unstable C-API function to provide a thread-safe mechanism for clearing weakrefs without executing callbacks. Some C-API extensions need to clear weakrefs without calling callbacks, such as after running finalizers like we do in subtype_dealloc. Previously they could use `_PyWeakref_ClearRef` on each weakref, but that's not thread-safe in the free-threaded build. Co-authored-by: Petr Viktorin <encukou@gmail.com>
This exposes
_PyWeakref_ClearWeakRefsNoCallbacks
as an unstable C-API function to provide a thread-safe mechanism for clearing weakrefs without executing callbacks.Some C-API extensions need to clear weakrefs without calling callbacks, such as after running finalizers like we do in subtype_dealloc. Previously they could use
_PyWeakref_ClearRef
on each weakref, but that's not thread-safe in the free-threaded build._PyWeakref_ClearRef
and_PyWeakref_ClearWeakRefsExceptCallbacks
should be exposed in 3.13 #118789📚 Documentation preview 📚: https://cpython-previews--118807.org.readthedocs.build/