From bff4bfeae1f428a815dc9a57b87f913217188fdd Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Mon, 10 Feb 2025 15:04:33 +0530 Subject: [PATCH] gh-128002: add fast path for native tasks in `asyncio.all_tasks` (#129943) --- Modules/_asynciomodule.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 4070076d756f..449c8a949926 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3991,6 +3991,19 @@ static inline int add_one_task(asyncio_state *state, PyObject *tasks, PyObject *task, PyObject *loop) { assert(PySet_CheckExact(tasks)); + if (Task_CheckExact(state, task)) { + int pending = 0; + Py_BEGIN_CRITICAL_SECTION(task); + pending = ((TaskObj *)task)->task_state == STATE_PENDING && ((TaskObj *)task)->task_loop == loop; + Py_END_CRITICAL_SECTION(); + if (pending) { + if (PySet_Add(tasks, task) < 0) { + return -1; + } + } + return 0; + } + PyObject *done = PyObject_CallMethodNoArgs(task, &_Py_ID(done)); if (done == NULL) { return -1; -- 2.47.3