Skip to content

Commit 7c9f267

Browse files
authored
gh-111926: Update _PyWeakref_IS_DEAD to be thread-safe (gh-112267)
1 parent f812914 commit 7c9f267

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Include/internal/pycore_weakref.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
12+
1113
static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) {
1214
assert(PyWeakref_Check(ref_obj));
1315
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
@@ -35,15 +37,20 @@ static inline PyObject* _PyWeakref_GET_REF(PyObject *ref_obj) {
3537

3638
static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj) {
3739
assert(PyWeakref_Check(ref_obj));
40+
int is_dead;
41+
Py_BEGIN_CRITICAL_SECTION(ref_obj);
3842
PyWeakReference *ref = _Py_CAST(PyWeakReference*, ref_obj);
3943
PyObject *obj = ref->wr_object;
4044
if (obj == Py_None) {
4145
// clear_weakref() was called
42-
return 1;
46+
is_dead = 1;
4347
}
44-
45-
// See _PyWeakref_GET_REF() for the rationale of this test
46-
return (Py_REFCNT(obj) == 0);
48+
else {
49+
// See _PyWeakref_GET_REF() for the rationale of this test
50+
is_dead = (Py_REFCNT(obj) == 0);
51+
}
52+
Py_END_CRITICAL_SECTION();
53+
return is_dead;
4754
}
4855

4956
extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyWeakReference *head);

0 commit comments

Comments
 (0)