Skip to content

Commit 4778d41

Browse files
BHUVANSH855miss-islington
authored andcommitted
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 802a2c8 commit 4778d41

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
@@ -2967,6 +2967,16 @@ async def coro():
29672967
with self.assertRaises(AttributeError):
29682968
del task._log_destroy_pending
29692969

2970+
def test_get_context_uninitialized_segfault(self):
2971+
# https://github.com/python/cpython/issues/154871
2972+
2973+
class UninitializedTask(self.Task):
2974+
def __init__(self, *args, **kwargs):
2975+
pass
2976+
2977+
task = UninitializedTask()
2978+
self.assertIsNone(task.get_context())
2979+
29702980

29712981
@unittest.skipUnless(hasattr(futures, '_CFuture') and
29722982
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
@@ -2814,7 +2814,11 @@ static PyObject *
28142814
_asyncio_Task_get_context_impl(TaskObj *self)
28152815
/*[clinic end generated code: output=6996f53d3dc01aef input=87c0b209b8fceeeb]*/
28162816
{
2817-
return Py_NewRef(self->task_context);
2817+
if (self->task_context) {
2818+
return Py_NewRef(self->task_context);
2819+
}
2820+
2821+
Py_RETURN_NONE;
28182822
}
28192823

28202824
/*[clinic input]

0 commit comments

Comments
 (0)