Skip to content

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

Merged
kumaraditya303 merged 3 commits into
python:mainfrom
BHUVANSH855:fix-asyncio-task-get-context-null
Aug 1, 2026
Merged

gh-154871: Fix asyncio.Task.get_context() crash#154898
kumaraditya303 merged 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
@kumaraditya303
kumaraditya303 merged commit afd10f0 into python:main Aug 1, 2026
57 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @BHUVANSH855 for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Aug 1, 2026

Copy link
Copy Markdown

GH-155019 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Aug 1, 2026
@bedevere-app

bedevere-app Bot commented Aug 1, 2026

Copy link
Copy Markdown

GH-155020 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Aug 1, 2026
@bedevere-app

bedevere-app Bot commented Aug 1, 2026

Copy link
Copy Markdown

GH-155021 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Aug 1, 2026
@BHUVANSH855
BHUVANSH855 deleted the fix-asyncio-task-get-context-null branch August 1, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants