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

Chris Angelico rosuav at gmail.com
Fri Mar 31 20:51:02 EDT 2023


On Sat, 1 Apr 2023 at 11:42, Clint Olsen <clint.olsen at gmail.com> wrote:
>
> On Friday, March 31, 2023 at 4:14:51 PM UTC-7, Chris Angelico wrote:
> > Okay, so that deals with the part from the subject line, leaving a
> > slightly different problem: The caught exception is not of the same
> > type as you were expecting. First question: Can you reproduce the
> > issue on command? If so, I would recommend trying this:
> >
> > except BaseException as e:
> > print("Got an exception!", type(e))
> > print(id(type(e)))
> > print(id(asyncio.CancelledError)
> > except:
> > print("Weird things are happening")
> > import sys
> > print(sys.exc_info())
> > print(id(sys.exc_info()[0]))
> > print(id(asyncio.CancelledError))
> >
> > Basically, I want to know whether (a) BaseException has changed, which
> > would be a nightmare; and (b) whether asyncio.CancelledError has
> > changed.
> >
> > This is the kind of bizarre behaviour that can happen if a module is
> > reloaded, or if there are multiple versions loaded for some reason.
> > But if that ISN'T what's happening, there'll have to be some other
> > explanation.
>
> Here's what I see:
>
> Got an exception! <class 'asyncio.exceptions.CancelledError'>
> 8204064
> 8204064
>

Can you confirm that it is indeed failing to catch the exception? Try this:

except asyncio.CancelledError:
    print("Cancelled correctly")

followed by the same type checking from above. Since the ID is the
same, I would expect it to match!

Can you post a full runnable example that exhibits the problem?

ChrisA


More information about the Python-list mailing list