-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-86802: Fix asyncio memory leak; shielded tasks where cancelled log… #134331
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
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 |
---|---|---|
|
@@ -908,6 +908,25 @@ def _done_callback(fut, cur_task=cur_task): | |
return outer | ||
|
||
|
||
def _log_on_exception(fut): | ||
if fut.cancelled(): | ||
return | ||
|
||
exc = fut.exception() | ||
if exc is None: | ||
return | ||
|
||
context = { | ||
'message': | ||
f'{exc.__class__.__name__} exception in shielded future', | ||
'exception': exc, | ||
'future': fut, | ||
} | ||
if fut._source_traceback: | ||
context['source_traceback'] = fut._source_traceback | ||
fut._loop.call_exception_handler(context) | ||
|
||
|
||
def shield(arg): | ||
"""Wait for a future, shielding it from cancellation. | ||
|
||
|
@@ -953,14 +972,11 @@ def shield(arg): | |
else: | ||
cur_task = None | ||
|
||
def _inner_done_callback(inner, cur_task=cur_task): | ||
if cur_task is not None: | ||
futures.future_discard_from_awaited_by(inner, cur_task) | ||
def _clear_awaited_by_callback(inner): | ||
futures.future_discard_from_awaited_by(inner, cur_task) | ||
|
||
def _inner_done_callback(inner): | ||
if outer.cancelled(): | ||
if not inner.cancelled(): | ||
# Mark inner's result as retrieved. | ||
inner.exception() | ||
return | ||
|
||
if inner.cancelled(): | ||
|
@@ -972,10 +988,16 @@ def _inner_done_callback(inner, cur_task=cur_task): | |
else: | ||
outer.set_result(inner.result()) | ||
|
||
|
||
def _outer_done_callback(outer): | ||
if not inner.done(): | ||
inner.remove_done_callback(_inner_done_callback) | ||
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. Before this PR, this line made the |
||
# Keep only one callback to log on cancel | ||
inner.remove_done_callback(_log_on_exception) | ||
inner.add_done_callback(_log_on_exception) | ||
|
||
if cur_task is not None: | ||
inner.add_done_callback(_clear_awaited_by_callback) | ||
Comment on lines
+998
to
+999
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. Now we're only adding the callback to |
||
|
||
|
||
inner.add_done_callback(_inner_done_callback) | ||
outer.add_done_callback(_outer_done_callback) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Fixed asyncio memory leak in cancelled shield tasks. For shielded tasks | ||
where the shield was cancelled, log potential exceptions through the | ||
exception handler. Contributed by Christian Harries. |
Uh oh!
There was an error while loading. Please reload this page.