Python 3.9 asyncio: Task cancel() throws asyncio.exceptions.CancelledError instead of asyncio.CancelledError

Chris Angelico rosuav at gmail.com
Fri Mar 31 18:22:52 EDT 2023


On Sat, 1 Apr 2023 at 09:19, Clint Olsen <clint.olsen at gmail.com> wrote:
>
> I'm having some issues with task cancellation inside a signal handler. My tasks do get cancelled, but I see odd behavior:
>
> Traceback (most recent call last):
>   File "/home/utils/release/sw/tools/python-3.9.7/lib/python3.9/site-packages/grpc/aio/_call.py", line 406, in _consume_request_iterator
>     async for request in request_iterator:
>   File "/home/foo/lib/python/rush/client.py", line 105, in send_status
>     status = await sendq.get()
>   File "/home/utils/Python/3.9/3.9.7-20211101/lib/python3.9/asyncio/queues.py", line 166, in get
>     await getter
> asyncio.exceptions.CancelledError: Client killed with signal 2
>
> Attempting to catch asyncio.CancelledError or asyncio.CancelledError does not work. The function in question looks like:
>
> 101 async def send_status():
> 102     while True:
> 103         logging.info('Waiting for sendq')
> 104         try:
> 105             status = await sendq.get()
> 106
> 107             if not status:
> 108                 break
> 109
> 110             logging.info(f'Sendq status {status}')
> 111
> 112             message = rush_pb2.Status(**status)
> 113             yield message
> 114         except asyncio.exceptions.CancelledError as err:
> 115             logging.error(f'Weird error {err}')
> 116             #raise asyncio.CancelledError
> 117             break
>
> If I use "except:" it does catch this exception.
>
> What's going on here?
>

>>> asyncio.exceptions.CancelledError is asyncio.CancelledError
True

Does that answer the question?

ChrisA


More information about the Python-list mailing list