Repository navigation
gh-162395: Fix awaited-by links for asyncio.shield() #162396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -993,13 +993,10 @@ def shield(arg): | |
| loop = futures._get_loop(inner) | ||
| outer = loop.create_future() | ||
|
|
||
| if loop is not None and (cur_task := current_task(loop)) is not None: | ||
| futures.future_add_to_awaited_by(inner, cur_task) | ||
| else: | ||
| cur_task = None | ||
| futures.future_add_to_awaited_by(inner, outer) | ||
|
|
||
| def _clear_awaited_by_callback(inner): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: since |
||
| futures.future_discard_from_awaited_by(inner, cur_task) | ||
| futures.future_discard_from_awaited_by(inner, outer) | ||
|
|
||
| def _inner_done_callback(inner): | ||
| if outer.cancelled(): | ||
|
|
@@ -1017,14 +1014,10 @@ def _inner_done_callback(inner): | |
| def _outer_done_callback(outer): | ||
| if not inner.done(): | ||
| inner.remove_done_callback(_inner_done_callback) | ||
| if cur_task is not None: | ||
| inner.remove_done_callback(_clear_awaited_by_callback) | ||
| futures.future_discard_from_awaited_by(inner, cur_task) | ||
|
|
||
| if cur_task is not None: | ||
| inner.add_done_callback(_clear_awaited_by_callback) | ||
|
|
||
| inner.remove_done_callback(_clear_awaited_by_callback) | ||
| futures.future_discard_from_awaited_by(inner, outer) | ||
|
|
||
| inner.add_done_callback(_clear_awaited_by_callback) | ||
| inner.add_done_callback(_inner_done_callback) | ||
| outer.add_done_callback(_outer_done_callback) | ||
| return outer | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -575,10 +575,31 @@ async def main(): | |
| 'T<anon>', | ||
| ['s capture_test_stack', 'a deep', 'a c1'], | ||
| [ | ||
| ['T<anon>', ['a main', 'a test_stack_shield'], []] | ||
| ['F', [], [ | ||
| ['T<anon>', ['a main', 'a test_stack_shield'], []], | ||
| ]], | ||
| ] | ||
| ]) | ||
|
|
||
| async def test_shield_awaited_by_different_task(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test where the consumer is cancelled while |
||
| async def consume(fut): | ||
| await fut | ||
|
|
||
| release = asyncio.Event() | ||
| child = asyncio.create_task(release.wait()) | ||
| shielded = asyncio.shield(child) | ||
| consumer = asyncio.create_task(consume(shielded), eager_start=True) | ||
| graph = asyncio.capture_call_graph(child, limit=0) | ||
| release.set() | ||
| await consumer | ||
| self.assertFalse(asyncio.capture_call_graph(child, limit=0).awaited_by) | ||
|
|
||
| waiter, = graph.awaited_by | ||
| self.assertIs(waiter.future, shielded) | ||
| parent, = waiter.awaited_by | ||
| self.assertIs(parent.future, consumer) | ||
| self.assertFalse(parent.awaited_by) | ||
|
|
||
| async def test_stack_timeout(self): | ||
|
|
||
| stack_for_inner = None | ||
|
|
@@ -602,7 +623,9 @@ async def main(): | |
| 'T<anon>', | ||
| ['s capture_test_stack', 'a inner', 'a c1'], | ||
| [ | ||
| ['T<anon>', ['a main', 'a test_stack_timeout'], []] | ||
| ['F', [], [ | ||
| ['T<anon>', ['a main', 'a test_stack_timeout'], []], | ||
| ]], | ||
| ] | ||
| ]) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix the asyncio call graph to show which task awaits the future returned by | ||
| :func:`asyncio.shield`. Patch by Timofei Ivankov. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
outeris consumed bywait(),gather()oradd_done_callback(), nothing records an awaiter for it. The graph now ends at an orphanF, where before it pointed at the task that calledshield(). Example:await asyncio.wait([asyncio.shield(t)]). Could we keep acurrent_task()fallback for that case, and add a test for it?