From 7228a19f7e395fb770826ed351cdd838c9798f62 Mon Sep 17 00:00:00 2001 From: Bhuvansh Kataria Date: Wed, 29 Jul 2026 19:49:37 +0000 Subject: [PATCH 1/3] gh-154871: Fix Task.get_context() crash on uninitialized Task --- Lib/test/test_asyncio/test_tasks.py | 4 ++++ .../Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst | 3 +++ Modules/_asynciomodule.c | 6 +++++- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 110cd0936c007c..cc74f49522cba6 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2924,6 +2924,10 @@ async def coro(): with self.assertRaises(AttributeError): del task._log_destroy_pending + def test_get_context_uninitialized(self): + task = self.Task.__new__(self.Task) + self.assertIsNone(task.get_context()) + @unittest.skipUnless(hasattr(futures, '_CFuture') and hasattr(tasks, '_CTask'), diff --git a/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst b/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst new file mode 100644 index 00000000000000..e6e4da35cd70e0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst @@ -0,0 +1,3 @@ +Fixed a crash in :meth:`_asyncio.Task.get_context` when called on an +uninitialized task. The method now returns ``None`` instead of crashing the +interpreter. diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 0cd41d0b4c4d0d..debdb66a2315bd 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -2787,7 +2787,11 @@ static PyObject * _asyncio_Task_get_context_impl(TaskObj *self) /*[clinic end generated code: output=6996f53d3dc01aef input=87c0b209b8fceeeb]*/ { - return Py_NewRef(self->task_context); + if (self->task_context) { + return Py_NewRef(self->task_context); + } + + Py_RETURN_NONE; } /*[clinic input] From 7891d3e72220d1aaabffa51ea5c47ff30d3200d1 Mon Sep 17 00:00:00 2001 From: Bhuvansh Date: Thu, 30 Jul 2026 01:25:45 +0530 Subject: [PATCH 2/3] Update 2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst --- .../Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst b/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst index e6e4da35cd70e0..b761c5b5d2a8ed 100644 --- a/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst +++ b/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst @@ -1,3 +1,3 @@ -Fixed a crash in :meth:`_asyncio.Task.get_context` when called on an -uninitialized task. The method now returns ``None`` instead of crashing the -interpreter. +Fixed a crash in ``_asyncio.Task.get_context()`` when called on an +uninitialized task. The method now returns ``None`` instead of crashing +the interpreter. From 6d980984d03c3bea6899fd2c5a1fc41b113244d6 Mon Sep 17 00:00:00 2001 From: Bhuvansh Kataria Date: Wed, 29 Jul 2026 20:27:16 +0000 Subject: [PATCH 3/3] gh-154871: Address review comments --- Lib/test/test_asyncio/test_tasks.py | 10 ++++++++-- .../2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst | 5 ++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index cc74f49522cba6..ad9b09857f8fd2 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2924,8 +2924,14 @@ async def coro(): with self.assertRaises(AttributeError): del task._log_destroy_pending - def test_get_context_uninitialized(self): - task = self.Task.__new__(self.Task) + def test_get_context_uninitialized_segfault(self): + # https://github.com/python/cpython/issues/154871 + + class UninitializedTask(self.Task): + def __init__(self, *args, **kwargs): + pass + + task = UninitializedTask() self.assertIsNone(task.get_context()) diff --git a/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst b/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst index b761c5b5d2a8ed..8f0f1096dfe634 100644 --- a/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst +++ b/Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst @@ -1,3 +1,2 @@ -Fixed a crash in ``_asyncio.Task.get_context()`` when called on an -uninitialized task. The method now returns ``None`` instead of crashing -the interpreter. +Fixed a crash in :meth:`asyncio.Task.get_context` +when called on an uninitialized task.