From db3e7656e06a979e3c2d8b201b6eaacf64e9aa67 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 6 Jun 2020 13:54:19 +0200 Subject: [PATCH] Handle AssertionError when cancelling tasks Fixes #1478. --- telethon/helpers.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/telethon/helpers.py b/telethon/helpers.py index 1b5f8843..664d504e 100644 --- a/telethon/helpers.py +++ b/telethon/helpers.py @@ -123,6 +123,8 @@ async def _cancel(log, **tasks): except RuntimeError: # Probably: RuntimeError: await wasn't used with future # + # See: https://github.com/python/cpython/blob/12d3061c7819a73d891dcce44327410eaf0e1bc2/Lib/asyncio/futures.py#L265 + # # Happens with _asyncio.Task instances (in "Task cancelling" state) # trying to SIGINT the program right during initial connection, on # _recv_loop coroutine (but we're creating its task explicitly with @@ -131,6 +133,12 @@ async def _cancel(log, **tasks): # Since we're aware of this error there's no point in logging it. # *May* be https://bugs.python.org/issue37172 pass + except AssertionError as e: + # In Python 3.6, the above RuntimeError is an AssertionError + # See https://github.com/python/cpython/blob/7df32f844efed33ca781a016017eab7050263b90/Lib/asyncio/futures.py#L328 + if e.args != ("yield from wasn't used with future",): + log.exception('Unhandled exception from %s after cancelling ' + '%s (%s)', name, type(task), task) except Exception: log.exception('Unhandled exception from %s after cancelling ' '%s (%s)', name, type(task), task)