Skip to content

gh-154871: Fix asyncio.Task.get_context() crash - #154898

Open
BHUVANSH855 wants to merge 3 commits into
python:mainfrom
BHUVANSH855:fix-asyncio-task-get-context-null
Open

gh-154871: Fix asyncio.Task.get_context() crash#154898
BHUVANSH855 wants to merge 3 commits into
python:mainfrom
BHUVANSH855:fix-asyncio-task-get-context-null

Conversation

@BHUVANSH855

@BHUVANSH855 BHUVANSH855 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix a crash in _asyncio.Task.get_context() when called on an uninitialized task.

Task instances created via __new__() (or subclasses that override __init__() without calling super().__init__()) leave task_context as NULL. Previously, get_context() unconditionally called Py_NewRef(self->task_context), which dereferenced a null pointer and crashed the interpreter.

This change checks whether task_context has been initialized before returning it. If it is NULL, get_context() now returns None, matching the behavior of the neighboring get_coro() and get_name() methods.

Changes

  • Guard task_context before calling Py_NewRef().
  • Return None when task_context is uninitialized.
  • Add a regression test covering Task.__new__(Task).get_context() for the C implementation.
  • Add a NEWS entry under the Library section.

Testing

  • Built CPython successfully.
  • Ran the new regression test:
    • ./python -m test test_asyncio.test_tasks -v -m test_get_context_uninitialized
  • Ran the full asyncio task test module:
    • ./python -m test test_asyncio.test_tasks

All tests passed successfully.

Closes issue gh-154871

Comment thread Misc/NEWS.d/next/Library/2026-07-29-19-48-59.gh-issue-154871.tgZILS.rst Outdated
Comment thread Lib/test/test_asyncio/test_tasks.py Outdated
@johnslavik

johnslavik commented Jul 29, 2026

Copy link
Copy Markdown
Member

I'll change "Closes gh-154871" in your PR description to "Closes issue gh-154871", because the use of "closes" keyword would close the issue immediately after this PR is merged and before 3 other backport PRs of your change to our maintenance branches are merged. We want to close the issue after the backport PRs are merged.

@johnslavik johnslavik added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jul 29, 2026
@johnslavik johnslavik changed the title gh-154871: Fix Task.get_context() crash on uninitialized Task gh-154871: Fix asyncio.Task.get_context() crash Jul 29, 2026
@BHUVANSH855
BHUVANSH855 requested a review from johnslavik July 29, 2026 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants