gh-154871: Fix asyncio.Task.get_context() crash - #154898
Open
BHUVANSH855 wants to merge 3 commits into
Open
Conversation
BHUVANSH855
requested review from
1st1,
asvetlov,
kumaraditya303 and
willingc
as code owners
July 29, 2026 19:51
johnslavik
requested changes
Jul 29, 2026
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. |
asyncio.Task.get_context() crash
johnslavik
approved these changes
Jul 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix a crash in
_asyncio.Task.get_context()when called on an uninitialized task.Taskinstances created via__new__()(or subclasses that override__init__()without callingsuper().__init__()) leavetask_contextasNULL. Previously,get_context()unconditionally calledPy_NewRef(self->task_context), which dereferenced a null pointer and crashed the interpreter.This change checks whether
task_contexthas been initialized before returning it. If it isNULL,get_context()now returnsNone, matching the behavior of the neighboringget_coro()andget_name()methods.Changes
task_contextbefore callingPy_NewRef().Nonewhentask_contextis uninitialized.Task.__new__(Task).get_context()for the C implementation.Testing
./python -m test test_asyncio.test_tasks -v -m test_get_context_uninitialized./python -m test test_asyncio.test_tasksAll tests passed successfully.
Closes issue gh-154871