Skip to content

gh-162395: Fix awaited-by links for asyncio.shield() - #162396

Open
deadlovelll wants to merge 2 commits into
python:mainfrom
deadlovelll:gh-162395-shield
Open

deadlovelll wants to merge 2 commits into
python:mainfrom
deadlovelll:gh-162395-shield

Conversation

@deadlovelll

Copy link
Copy Markdown
Contributor

Fix awaited-by links for asyncio.shield()

See gh-162395

@asvetlov asvetlov added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 22, 2026

@asvetlov asvetlov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Please address comments

Comment thread Lib/asyncio/tasks.py
futures.future_add_to_awaited_by(inner, cur_task)
else:
cur_task = None
futures.future_add_to_awaited_by(inner, outer)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When outer is consumed by wait(), gather() or add_done_callback(), nothing records an awaiter for it. The graph now ends at an orphan F, where before it pointed at the task that called shield(). Example: await asyncio.wait([asyncio.shield(t)]). Could we keep a current_task() fallback for that case, and add a test for it?

Comment thread Lib/asyncio/tasks.py
cur_task = None
futures.future_add_to_awaited_by(inner, outer)

def _clear_awaited_by_callback(inner):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: since outer is always set now, this can be a single discard call at the top of _inner_done_callback.

]
])

async def test_shield_awaited_by_different_task(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test where the consumer is cancelled while inner is still pending, then check that capture_call_graph(child).awaited_by is empty.

@bedevere-app

bedevere-app Bot commented Nov 22, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants