dabeaz / curio

Good Curio!
Other
4.02k stars 241 forks source link

Task Exit exception #262

Closed skgbanga closed 5 years ago

skgbanga commented 6 years ago

In the curio task model following example is given to show TaskExit

from curio import *

async def coro1():
    print('About to die')
    raise TaskExit()

async def coro2():
    try:
        await coro1()
    except Exception as e:
        print('Something went wrong')

async def coro3():
    await coro2()

try:
    run(coro3())
except TaskExit:
    print('Task exited')

It is also mentioned that TaskExit is a subclass of BaseException, and wouldn't be caught by handlers looking for Exception.

As a result, I did not expect the above to print "Something went wrong"

However, I get the following output when I run the above example:

About to die
Something went wrong
Dobatymo commented 6 years ago

According to https://github.com/dabeaz/curio/blob/master/curio/errors.py TaskExit inherits from Exception not BaseException. So either the code or the documentation needs to be changed.

skgbanga commented 6 years ago

Thanks @Dobatymo It seems the only exception which is derived from BaseException is KernelExit.

kawing-chiu commented 5 years ago

Yeah, the documentation is wrong and should be updated. Also, the doc on TaskExit is not adequate. The example is a poor demonstration of TaskExit:

So, what is the point of TaskExit?

dabeaz commented 5 years ago

I will review what's happening with TaskExit. I believe that the original intent was that if a task wanted to cleanly exit from a deeply nested set of calls, it could raise TaskExit much like normal code could raise SystemExit to cause Python to terminate. It's entirely possible that TaskExit isn't fully implemented. I will check.

dabeaz commented 5 years ago

Honestly, I don't know what TaskExit is used for or what I was thinking (apparently nothing). I thought about making it mean something more concrete, but I've decided to just delete it instead. If someone really wants something like this, they can just make their own custom exception and raise that.