Skip to content

Commit

Permalink
pythongh-111926: Update _PyWeakref_IS_DEAD to be thread-safe (pythong…
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 authored and aisk committed Feb 11, 2024
1 parent ad39eaf commit a062334
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Include/internal/pycore_weakref.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()

static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) {
assert(PyWeakref_Check(ref_obj));
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
Expand Down Expand Up @@ -35,15 +37,20 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) {

static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj) {
assert(PyWeakref_Check(ref_obj));
int is_dead;
Py_BEGIN_CRITICAL_SECTION(ref_obj);
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
PyObject *obj = ref->wr_object;
if (obj == Py_None) {
// clear_weakref() was called
return 1;
is_dead = 1;
}

// See _PyWeakref_GET_REF() for the rationale of this test
return (Py_REFCNT(obj) == 0);
else {
// See _PyWeakref_GET_REF() for the rationale of this test
is_dead = (Py_REFCNT(obj) == 0);
}
Py_END_CRITICAL_SECTION();
return is_dead;
}

extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyWeakReference *head);
Expand Down

0 comments on commit a062334

Please sign in to comment.