8000 GH-120974: make _asyncio_all_tasks_impl thread safe by kumaraditya303 · Pull Request #122801 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-120974: make _asyncio_all_tasks_impl thread safe #122801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix it
  • Loading branch information
kumaraditya303 committed Aug 10, 2024
commit 1d03dd1a545cfe65aa5bb779e14347b5cecc084c
13 changes: 8 additions & 5 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#endif

#include "Python.h"
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION_MUT()
#include "pycore_dict.h" // _PyDict_GetItem_KnownHash()
#include "pycore_freelist.h" // _Py_FREELIST_POP()
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
Expand Down Expand Up @@ -78,7 +79,7 @@ typedef struct {

#ifdef Py_GIL_DISABLED
# define ASYNCIO_STATE_LOCK(state) Py_BEGIN_CRITICAL_SECTION_MUT(&state->mutex)
# define ASYNCIO_STATE_UNLOCK(state) Py_END_CRITICAL_SECTION(&state->mutex)
# define ASYNCIO_STATE_UNLOCK(state) Py_END_CRITICAL_SECTION()
#else
# define ASYNCIO_STATE_LOCK(state) ((void)state)
# define ASYNCIO_STATE_UNLOCK(state) ((void)state)
Expand Down Expand Up @@ -3628,6 +3629,7 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
Py_DECREF(item);
}
Py_DECREF(eager_iter);
int err = 0;
ASYNCIO_STATE_LOCK(state);
TaskObj *head = state->asyncio_tasks.head;
Py_INCREF(head);
Expand All @@ -3640,12 +3642,16 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
Py_DECREF(tasks);
Py_DECREF(loop);
Py_DECREF(head);
goto error;
err = 1;
break;
}
Py_INCREF(head->next);
Py_SETREF(head, head->next);
}
ASYNCIO_STATE_UNLOCK(state);
if (err) {
return NULL;
}
PyObject *scheduled_iter = PyObject_GetIter(state->non_asyncio_tasks);
if (scheduled_iter == NULL) {
Py_DECREF(tasks);
Expand All @@ -3665,9 +3671,6 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
Py_DECREF(scheduled_iter);
Py_DECREF(loop);
return tasks;
error:
ASYNCIO_STATE_UNLOCK(state);
return NULL;
}

static int
Expand Down
Loading
0