Message 321321 - Python tracker

Message321321

Author and800
Recipients and800, asvetlov, yselivanov
Date 2018-07-09.14:01:51
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1531144912.36.0.56676864532.issue34074@psf.upfronthosting.co.za>
In-reply-to
Content
Source: https://stackoverflow.com/q/51245011/6275324

Asyncio somehow breaks coroutine finalization. I believe there may be a bug in C implementation (_asyncio) of tasks or futures. Reproducible within version 3.7.0@python:3.7 docker container.

Consider this example (except and finally blocks will never execute):


import asyncio

async def work():
    try:
        print('started working')
        await asyncio.sleep(3600)
    except BaseException as e:
        print('caught ' + str(type(e)))
    finally:
        print('finalization completed')

async def stopper():
    await asyncio.sleep(5)
    loop.stop()

loop = asyncio.get_event_loop()
loop.create_task(work())
loop.create_task(stopper())
loop.run_forever()



And there is asyncio-free piece of code, which works properly, catching GeneratorExit, thrown by coro destructor:


import asyncio

async def work():
    try:
        print('started working')
        await asyncio.sleep(3600)
    except BaseException as e:
        print('caught ' + str(type(e)))
    finally:
        print('finalization completed')

coro = work()
coro.send(None)
del coro
History
Date User Action Args
2018-07-09 14:01:52and800setrecipients: + and800, asvetlov, yselivanov
2018-07-09 14:01:52and800setmessageid: <1531144912.36.0.56676864532.issue34074@psf.upfronthosting.co.za>
2018-07-09 14:01:52and800linkissue34074 messages
2018-07-09 14:01:51and800create