mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #16602: When a weakref's target was part of a long deallocation chain, the object could remain reachable through its weakref even though its refcount had dropped to zero.
Thanks to Eugene Toder for diagnosing and reporting the issue.
This commit is contained in:
parent
bd5279ea24
commit
d38c990bb7
5 changed files with 39 additions and 4 deletions
|
@ -66,7 +66,17 @@ PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
|
|||
|
||||
PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
|
||||
|
||||
#define PyWeakref_GET_OBJECT(ref) (((PyWeakReference *)(ref))->wr_object)
|
||||
/* Explanation for the Py_REFCNT() check: when a weakref's target is part
|
||||
of a long chain of deallocations which triggers the trashcan mechanism,
|
||||
clearing the weakrefs can be delayed long after the target's refcount
|
||||
has dropped to zero. In the meantime, code accessing the weakref will
|
||||
be able to "see" the target object even though it is supposed to be
|
||||
unreachable. See issue #16602. */
|
||||
|
||||
#define PyWeakref_GET_OBJECT(ref) \
|
||||
(Py_REFCNT(((PyWeakReference *)(ref))->wr_object) > 0 \
|
||||
? ((PyWeakReference *)(ref))->wr_object \
|
||||
: Py_None)
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue