From c929bc0c3091163d8df079c98c0c3bf8230281f1 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 17 Nov 2023 14:26:42 +0900 Subject: [PATCH 1/6] gh-111926 Update _weakref to be threadsafe in --disable-gil build --- Modules/_weakref.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Modules/_weakref.c b/Modules/_weakref.c index 4e2862e7467c3d..68e098c0c61d2e 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -1,8 +1,8 @@ #include "Python.h" -#include "pycore_dict.h" // _PyDict_DelItemIf() -#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR -#include "pycore_weakref.h" // _PyWeakref_IS_DEAD() - +#include "pycore_dict.h" // _PyDict_DelItemIf() +#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR +#include "pycore_weakref.h" // _PyWeakref_IS_DEAD() +#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION #define GET_WEAKREFS_LISTPTR(o) \ ((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o)) @@ -32,9 +32,11 @@ _weakref_getweakrefcount_impl(PyObject *module, PyObject *object) if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) return 0; - + Py_BEGIN_CRITICAL_SECTION(object); list = GET_WEAKREFS_LISTPTR(object); - return _PyWeakref_GetWeakrefCount(*list); + Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); + Py_END_CRITICAL_SECTION(); + return count; } @@ -94,6 +96,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object) return PyList_New(0); } + Py_BEGIN_CRITICAL_SECTION(object); PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); @@ -107,6 +110,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object) PyList_SET_ITEM(result, i, Py_NewRef(current)); current = current->wr_next; } + Py_END_CRITICAL_SECTION(); return result; } From 8f30ab95883cd7f9ae2028220665d87c12850d77 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 17 Nov 2023 14:41:40 +0900 Subject: [PATCH 2/6] fix --- Modules/_weakref.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Modules/_weakref.c b/Modules/_weakref.c index 68e098c0c61d2e..6a321424f3f230 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -32,9 +32,10 @@ _weakref_getweakrefcount_impl(PyObject *module, PyObject *object) if (!_PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))) return 0; + Py_ssize_t count; Py_BEGIN_CRITICAL_SECTION(object); list = GET_WEAKREFS_LISTPTR(object); - Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); + count = _PyWeakref_GetWeakrefCount(*list); Py_END_CRITICAL_SECTION(); return count; } @@ -96,11 +97,12 @@ _weakref_getweakrefs(PyObject *module, PyObject *object) return PyList_New(0); } + PyObject *result; Py_BEGIN_CRITICAL_SECTION(object); PyWeakReference **list = GET_WEAKREFS_LISTPTR(object); Py_ssize_t count = _PyWeakref_GetWeakrefCount(*list); - PyObject *result = PyList_New(count); + result = PyList_New(count); if (result == NULL) { return NULL; } From 7c6ae8786aa2b668e9698371801eb8f4f7e7a1da Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 17 Nov 2023 15:04:47 +0900 Subject: [PATCH 3/6] Update order of headers --- Modules/_weakref.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_weakref.c b/Modules/_weakref.c index 6a321424f3f230..7f5c3efe596a20 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -1,8 +1,8 @@ #include "Python.h" +#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION #include "pycore_dict.h" // _PyDict_DelItemIf() #include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR #include "pycore_weakref.h" // _PyWeakref_IS_DEAD() -#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION #define GET_WEAKREFS_LISTPTR(o) \ ((PyWeakReference **) _PyObject_GET_WEAKREFS_LISTPTR(o)) From 043b4b1b5f321f253d1b5d77c07e461532174bf3 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 17 Nov 2023 16:52:16 +0900 Subject: [PATCH 4/6] Update is_dead_weakref --- Modules/_weakref.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Modules/_weakref.c b/Modules/_weakref.c index 7f5c3efe596a20..cc1c6eb24402e3 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -48,7 +48,11 @@ is_dead_weakref(PyObject *value) PyErr_SetString(PyExc_TypeError, "not a weakref"); return -1; } - return _PyWeakref_IS_DEAD(value); + int is_dead; + Py_BEGIN_CRITICAL_SECTION(value); + is_dead = _PyWeakref_IS_DEAD(value); + Py_END_CRITICAL_SECTION(); + return is_dead; } /*[clinic input] From 3d4fe34b608c019770b26c3ea2fcd1cec88700f1 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sat, 18 Nov 2023 10:31:48 +0900 Subject: [PATCH 5/6] Address code review --- Modules/_weakref.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_weakref.c b/Modules/_weakref.c index cc1c6eb24402e3..b1a5b7dd639939 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -108,7 +108,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object) result = PyList_New(count); if (result == NULL) { - return NULL; + goto exit; } PyWeakReference *current = *list; @@ -116,6 +116,7 @@ _weakref_getweakrefs(PyObject *module, PyObject *object) PyList_SET_ITEM(result, i, Py_NewRef(current)); current = current->wr_next; } +exit: Py_END_CRITICAL_SECTION(); return result; } From 995717912137cf6adae8fb9ed591ee8109ad78f7 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Sat, 18 Nov 2023 10:44:02 +0900 Subject: [PATCH 6/6] nit --- Modules/_weakref.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_weakref.c b/Modules/_weakref.c index b1a5b7dd639939..90c7afcc248ff3 100644 --- a/Modules/_weakref.c +++ b/Modules/_weakref.c @@ -1,7 +1,7 @@ #include "Python.h" -#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION +#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION() #include "pycore_dict.h" // _PyDict_DelItemIf() -#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR +#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR() #include "pycore_weakref.h" // _PyWeakref_IS_DEAD() #define GET_WEAKREFS_LISTPTR(o) \