[New-bugs-announce] [issue35867] NameError is not caught at Task execution

Sampsa Riikonen report at bugs.python.org
Thu Jan 31 08:16:37 EST 2019

New submission from Sampsa Riikonen <sampsa.riikonen at gmail.com>:

- 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):
    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()


components: asyncio
messages: 334625
nosy: Sampsa Riikonen, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: NameError is not caught at Task execution
type: behavior
versions: Python 3.6

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list