Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ def __step_run_and_handle_result(self, exc):
super().cancel() # I.e., Future.cancel(self).
except (KeyboardInterrupt, SystemExit) as exc:
super().set_exception(exc)
# This exception is re-raised to the event loop instead of being
# retained solely by this Task, so it is not unhandled if the
# task cannot subsequently be awaited during loop shutdown.
self._log_traceback = False
raise
except BaseException as exc:
super().set_exception(exc)
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,17 @@ async def current_task(exc):
self.assertTrue(not t.cancelled())
self.assertEqual(t.result(), "ok")

def test_step_does_not_log_reraised_base_exceptions(self):
async def raise_exception(exc):
raise exc

for exc_type in (SystemExit, KeyboardInterrupt):
with self.subTest(exc_type=exc_type):
task = self.new_task(self.loop, raise_exception(exc_type()))
with self.assertRaises(exc_type):
self.loop.run_until_complete(task)
self.assertFalse(task._log_traceback)

def test_step_result_future(self):
# If coroutine returns future, task waits on this future.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Suppress a duplicate ``Task exception was never retrieved`` warning when an
asyncio task raises :exc:`SystemExit` or :exc:`KeyboardInterrupt`, which is
already propagated by the event loop.
5 changes: 4 additions & 1 deletion Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3129,7 +3129,10 @@ task_step_impl(asyncio_state *state, TaskObj *task, PyObject *exc)
if (PyErr_GivenExceptionMatches(exc, PyExc_KeyboardInterrupt) ||
PyErr_GivenExceptionMatches(exc, PyExc_SystemExit))
{
/* We've got a KeyboardInterrupt or a SystemError; re-raise it */
/* The exception is re-raised to the event loop, so it cannot be
lost even if loop shutdown prevents this task from being
awaited. */
task->task_log_tb = 0;
PyErr_SetRaisedException(exc);
goto fail;
}
Expand Down
Loading