Skip to content

Commit 22aea1c

Browse files
[3.13] gh-154871: Fix asyncio.Task.get_context() crash on uninitialized tasks (GH-154898) (#155021)
gh-154871: Fix `asyncio.Task.get_context()` crash on uninitialized tasks (GH-154898) (cherry picked from commit afd10f0) Co-authored-by: Bhuvansh <bhuvanshkataria@gmail.com>
1 parent 9b67007 commit 22aea1c

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

Lib/test/test_asyncio/test_tasks.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,16 @@ async def coro():
28482848
with self.assertRaises(AttributeError):
28492849
del task._log_destroy_pending
28502850

2851+
def test_get_context_uninitialized_segfault(self):
2852+
# https://github.com/python/cpython/issues/154871
2853+
2854+
class UninitializedTask(self.Task):
2855+
def __init__(self, *args, **kwargs):
2856+
pass
2857+
2858+
task = UninitializedTask()
2859+
self.assertIsNone(task.get_context())
2860+
28512861

28522862
@unittest.skipUnless(hasattr(futures, '_CFuture') and
28532863
hasattr(tasks, '_CTask'),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed a crash in :meth:`asyncio.Task.get_context`
2+
when called on an uninitialized task.

Modules/_asynciomodule.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2482,7 +2482,11 @@ static PyObject *
24822482
_asyncio_Task_get_context_impl(TaskObj *self)
24832483
/*[clinic end generated code: output=6996f53d3dc01aef input=87c0b209b8fceeeb]*/
24842484
{
2485-
return Py_NewRef(self->task_context);
2485+
if (self->task_context) {
2486+
return Py_NewRef(self->task_context);
2487+
}
2488+
2489+
Py_RETURN_NONE;
24862490
}
24872491

24882492
/*[clinic input]

0 commit comments

Comments
 (0)