From 84d34c037c16689a81e6899f08bfc914b6874d89 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Fri, 26 Jul 2024 21:19:53 -0400 Subject: [PATCH 1/4] Add missing NULL check to Task.get_coro() --- Modules/_asynciomodule.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 1a223f9bd0cbaeb..68ba6a816aa3aec 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -2500,7 +2500,12 @@ static PyObject * _asyncio_Task_get_coro_impl(TaskObj *self) /*[clinic end generated code: output=bcac27c8cc6c8073 input=d2e8606c42a7b403]*/ { - return Py_NewRef(self->task_coro); + if (self->task_coro) + { + return Py_NewRef(self->task_coro); + } + + Py_RETURN_NONE; } /*[clinic input] From 63ef74c2ba70bc7e4f0f059e228d32ae31226472 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Fri, 26 Jul 2024 21:21:19 -0400 Subject: [PATCH 2/4] Add NEWS entry. --- .../next/Library/2024-07-26-21-21-13.gh-issue-122332.fvw88r.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-07-26-21-21-13.gh-issue-122332.fvw88r.rst diff --git a/Misc/NEWS.d/next/Library/2024-07-26-21-21-13.gh-issue-122332.fvw88r.rst b/Misc/NEWS.d/next/Library/2024-07-26-21-21-13.gh-issue-122332.fvw88r.rst new file mode 100644 index 000000000000000..55bb1dc44add1b1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-07-26-21-21-13.gh-issue-122332.fvw88r.rst @@ -0,0 +1,2 @@ +Fixed segfault with :meth:`asyncio.Task.get_coro` when using an eager task +factory. From fd37c91af78ed70481868fb130a536d5f408a53c Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Fri, 26 Jul 2024 23:44:39 -0400 Subject: [PATCH 3/4] Update Modules/_asynciomodule.c Co-authored-by: Jelle Zijlstra --- Modules/_asynciomodule.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 68ba6a816aa3aec..873c17cd78709d4 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -2500,8 +2500,7 @@ static PyObject * _asyncio_Task_get_coro_impl(TaskObj *self) /*[clinic end generated code: output=bcac27c8cc6c8073 input=d2e8606c42a7b403]*/ { - if (self->task_coro) - { + if (self->task_coro) { return Py_NewRef(self->task_coro); } From a9de9fbbc2f181ced92b6b850c9cdcb4c7071a72 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sat, 27 Jul 2024 00:11:33 -0400 Subject: [PATCH 4/4] Add unit test. --- Lib/test/test_asyncio/test_eager_task_factory.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Lib/test/test_asyncio/test_eager_task_factory.py b/Lib/test/test_asyncio/test_eager_task_factory.py index 0f8212dbec47be5..0777f39b5724860 100644 --- a/Lib/test/test_asyncio/test_eager_task_factory.py +++ b/Lib/test/test_asyncio/test_eager_task_factory.py @@ -241,6 +241,18 @@ class DummyLoop: _, out, err = assert_python_ok("-c", code) self.assertFalse(err) + def test_issue122332(self): + async def coro(): + pass + + async def run(): + task = self.loop.create_task(coro()) + await task + self.assertIsNone(task.get_coro()) + + self.run_coro(run()) + + class AsyncTaskCounter: def __init__(self, loop, *, task_class, eager): self.suspense_count = 0