From d7b118b8d65ac220ad92cc8498bdca62eb7cb314 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 25 Jun 2024 17:17:37 +0000 Subject: [PATCH 1/5] fix thread safety issue --- Modules/_asynciomodule.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 4672fc7f60efd6..7f1e75f30c3aad 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3706,6 +3706,7 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop) } Py_DECREF(eager_iter); TaskObj *head = state->asyncio_tasks.head; + Py_INCREF(head); assert(head != NULL); assert(head->prev == NULL); TaskObj *tail = &state->asyncio_tasks.tail; @@ -3714,10 +3715,11 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop) if (add_one_task(state, tasks, (PyObject *)head, loop) < 0) { Py_DECREF(tasks); Py_DECREF(loop); + Py_DECREF(head); return NULL; } - head = head->next; - assert(head != NULL); + Py_INCREF(head->next); + Py_SETREF(head, head->next); } PyObject *scheduled_iter = PyObject_GetIter(state->non_asyncio_tasks); if (scheduled_iter == NULL) { @@ -3944,6 +3946,7 @@ static int module_exec(PyObject *mod) { asyncio_state *state = get_asyncio_state(mod); + Py_SET_REFCNT(&state->asyncio_tasks.tail, _Py_IMMORTAL_REFCNT); state->asyncio_tasks.head = &state->asyncio_tasks.tail; #define CREATE_TYPE(m, tp, spec, base) \ From 11fc4163a082cb86c5de1ccf344f95f04d93d09c Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 25 Jun 2024 18:00:20 +0000 Subject: [PATCH 2/5] code review --- Modules/_asynciomodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 7f1e75f30c3aad..b758714248ba98 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3946,7 +3946,8 @@ static int module_exec(PyObject *mod) { asyncio_state *state = get_asyncio_state(mod); - Py_SET_REFCNT(&state->asyncio_tasks.tail, _Py_IMMORTAL_REFCNT); + + _Py_SetImmortal((PyObject *)&state->asyncio_tasks.tail); state->asyncio_tasks.head = &state->asyncio_tasks.tail; #define CREATE_TYPE(m, tp, spec, base) \ From cf096143d74dcada1c59f9d556ee8ea26d6477cf Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Tue, 25 Jun 2024 18:04:00 +0000 Subject: [PATCH 3/5] fix headers --- Modules/_asynciomodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index b758714248ba98..a4084dbefe9bde 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -6,6 +6,7 @@ #include "pycore_dict.h" // _PyDict_GetItem_KnownHash() #include "pycore_modsupport.h" // _PyArg_CheckPositional() #include "pycore_moduleobject.h" // _PyModule_GetState() +#include "pycore_object.h" // _Py_SetImmortal #include "pycore_pyerrors.h" // _PyErr_ClearExcState() #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() #include "pycore_pystate.h" // _PyThreadState_GET() From 8b77f8dc1e3b35d0286ba22f3e32174fd6b9d47b Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Wed, 26 Jun 2024 09:58:04 +0530 Subject: [PATCH 4/5] fix setting of type --- Include/internal/pycore_object.h | 4 ++-- Modules/_asynciomodule.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Include/internal/pycore_object.h b/Include/internal/pycore_object.h index 311f295685f361..ebb0f30b5f7377 100644 --- a/Include/internal/pycore_object.h +++ b/Include/internal/pycore_object.h @@ -126,8 +126,8 @@ static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n) } #define _Py_RefcntAdd(op, n) _Py_RefcntAdd(_PyObject_CAST(op), n) -extern void _Py_SetImmortal(PyObject *op); -extern void _Py_SetImmortalUntracked(PyObject *op); +PyAPI_FUNC(void) _Py_SetImmortal(PyObject *op); +PyAPI_FUNC(void) _Py_SetImmortalUntracked(PyObject *op); // Makes an immortal object mortal again with the specified refcnt. Should only // be used during runtime finalization. diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index a4084dbefe9bde..cbea5d467f2be5 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3947,8 +3947,8 @@ static int module_exec(PyObject *mod) { asyncio_state *state = get_asyncio_state(mod); - - _Py_SetImmortal((PyObject *)&state->asyncio_tasks.tail); + Py_SET_TYPE(&state->asyncio_tasks.tail, state->TaskType); + _Py_SetImmortalUntracked((PyObject *)&state->asyncio_tasks.tail); state->asyncio_tasks.head = &state->asyncio_tasks.tail; #define CREATE_TYPE(m, tp, spec, base) \ From 0915dd65a9baeee44c24b7ec89e4a07880bd9f8c Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Wed, 26 Jun 2024 10:16:20 +0530 Subject: [PATCH 5/5] Update Modules/_asynciomodule.c --- Modules/_asynciomodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index cbea5d467f2be5..87ad236cdbb39f 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -6,7 +6,7 @@ #include "pycore_dict.h" // _PyDict_GetItem_KnownHash() #include "pycore_modsupport.h" // _PyArg_CheckPositional() #include "pycore_moduleobject.h" // _PyModule_GetState() -#include "pycore_object.h" // _Py_SetImmortal +#include "pycore_object.h" // _Py_SetImmortalUntracked #include "pycore_pyerrors.h" // _PyErr_ClearExcState() #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() #include "pycore_pystate.h" // _PyThreadState_GET()