8000 Make _PyWeakref_GetWeakrefCount thread-safe · python/cpython@c9b3bf9 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9b3bf9

Browse files
committed
Make _PyWeakref_GetWeakrefCount thread-safe
1 parent 363a1a6 commit c9b3bf9

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Include/internal/pycore_weakref.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ static inline int _PyWeakref_IS_DEAD(PyObject *ref_obj)
7171
return ret;
7272
}
7373

74+
// NB: In free-threaded builds the referenced object must be live for the
75+
// duration of the call.
7476
extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyWeakReference *head);
7577

7678
extern void _PyWeakref_ClearRef(PyWeakReference *self);
@@ -79,4 +81,3 @@ extern void _PyWeakref_ClearRef(PyWeakReference *self);
7981
}
8082
#endif
8183
#endif /* !Py_INTERNAL_WEAKREF_H */
82-

Objects/weakrefobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@ Py_ssize_t
108108
_PyWeakref_GetWeakrefCount(PyWeakReference *head)
109109
{
110110
Py_ssize_t count = 0;
111-
111+
if (head == NULL) {
112+
return 0;
113+
}
114+
Py_BEGIN_CRITICAL_SECTION(head->wr_object);
112115
while (head != NULL) {
113116
++count;
114117
head = head->wr_next;
115118
}
119+
Py_END_CRITICAL_SECTION();
116120
return count;
117121
}
118122

0 commit comments

Comments
 (0)
0