Skip to content
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

gh-128002: fix asyncio.all_tasks against concurrent deallocations of tasks #128541

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 2 additions & 2 deletions Include/internal/pycore_pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ extern void _PyEval_StartTheWorldAll(_PyRuntimeState *runtime);
// Perform a stop-the-world pause for threads in the specified interpreter.
//
// NOTE: This is a no-op outside of Py_GIL_DISABLED builds.
extern void _PyEval_StopTheWorld(PyInterpreterState *interp);
extern void _PyEval_StartTheWorld(PyInterpreterState *interp);
extern PyAPI_FUNC(void) _PyEval_StopTheWorld(PyInterpreterState *interp);
extern PyAPI_FUNC(void) _PyEval_StartTheWorld(PyInterpreterState *interp);


static inline void
Expand Down
16 changes: 13 additions & 3 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3767,9 +3767,18 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
return NULL;
}
int err = 0;
ASYNCIO_STATE_LOCK(state);
struct llist_node *node;

// The linked list holds borrowed references to the tasks
// so before reading from it, all other threads
// are stopped using stop the world event so that
// no task could be concurrently deallocated while being
// added to the list.
// The state critical section need not to be held as
// all other threads are paused.
PyInterpreterState *interp = PyInterpreterState_Get();
kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
_PyEval_StopTheWorld(interp);

struct llist_node *node;
llist_for_each_safe(node, &state->asyncio_tasks_head) {
TaskObj *task = llist_data(node, TaskObj, task_node);
if (PyList_Append(tasks, (PyObject *)task) < 0) {
Expand All @@ -3779,7 +3788,8 @@ _asyncio_all_tasks_impl(PyObject *module, PyObject *loop)
break;
}
}
ASYNCIO_STATE_UNLOCK(state);

_PyEval_StartTheWorld(interp);
if (err) {
return NULL;
}
Expand Down
Loading
  NODES
COMMUNITY 2
INTERN 3
Note 2
Project 4
USERS 1