Issue 35867: NameError is not caught at Task execution (original) (raw)
Issue35867
Created on 2019-01-31 13:16 by Sampsa Riikonen, last changed 2022-04-11 14:59 by admin. This issue is now closed.
Messages (3) | ||
---|---|---|
msg334625 - (view) | Author: Sampsa Riikonen (Sampsa Riikonen) | Date: 2019-01-31 13:16 |
- Create a cofunction that raises an Exception or an Error - Schedule that cofunction as a task - Exceptions are raised when the task is executed OK - However, Errors (i.e. NameError, AssertionError, etc.) are raised only at task garbage collection..! Please try this snippet: ``` import asyncio class HevonPaskaa: def __init__(self): pass async def goodfunc(self): await asyncio.sleep(3) print("Good function was called allright") print("While it was sleeping, hevonpaska must have been executed") async def hevonpaska(self): """When this cofunction is scheduled as a task: - The NameError is not raised immediately .. ! - BaseException is raised immeadiately OK """ raise NameError # WARNING: This is catched only when the program terminates # raise BaseException # WARNING: comment the previous line and uncomment this: this is catched immediately async def cofunc2(self): # # we'd like this to raise the NameError immediately: self.task = asyncio.get_event_loop().create_task(self.hevonpaska()) self.good_task = asyncio.get_event_loop().create_task(self.goodfunc()) # # this raises NameError immediately because the task is garbage collected: # self.task = None async def cofunc1(self): await self.cofunc2() print("\nwaitin' : where-t-f is the NameError hiding!?") await asyncio.sleep(6) print("Wait is over, let's exit\n") hv = HevonPaskaa() asyncio.get_event_loop().run_until_complete(hv.cofunc1()) ``` | ||
msg335856 - (view) | Author: Emmanuel Arias (eamanu) * | Date: 2019-02-18 17:53 |
I test it and this have the same behavior on 3.7 and 3.8... I will work on this | ||
msg336265 - (view) | Author: Pablo Galindo Salgado (pablogsal) * ![]() |
Date: 2019-02-21 21:50 |
I do not think this is a bug. Any exception that is raised inside a task will be in the .exception() method when the task is finished. Here you are running the task without waiting for finalization. For example, if you change: async def cofunc1(self): await self.cofunc2() await self.task # <------------- print("\nwaitin' : where-t-f is the NameError hiding!?") await asyncio.sleep(6) print("Wait is over, let's exit\n") you will find the NameError immediately. If you do not want to await the task you can wait until self.task.done() is True and then check self.task.exception() for retrieving the exception (if any). What happens with BaseException is that is a very low-level exception that is handled differently compared with regular exceptions that derive from Exception. The reason is that control flow exceptions and things like KeyboardInterrupt need to be handled differently. This happens explicitly here: https://github.com/python/cpython/blob/master/Modules/_asynciomodule.c#L2675 |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:59:10 | admin | set | github: 80048 |
2019-02-21 21:50:42 | pablogsal | set | status: open -> closednosy: + pablogsalmessages: + resolution: not a bugstage: resolved |
2019-02-18 17:53:33 | eamanu | set | nosy: + eamanumessages: + versions: + Python 3.7, Python 3.8 |
2019-01-31 13:16:37 | Sampsa Riikonen | create |