[issue39232] asyncio crashes when tearing down the proactor event loop

Shane Mai report at bugs.python.org
Mon Aug 23 08:27:41 EDT 2021


Shane Mai <machine.gw at gmail.com> added the comment:

I added some code to wait for all tasks completion before exit: 
    currentTask = asyncio.current_task()
    for t in asyncio.all_tasks():
        if currentTask != t:
            await t
and still got the exception


Then I think it created additional thread somewhere and created an event_loop inside it. To dig it out I sub-classed threading.Thread. I also wait for all tasks in all threads before exiting:

loops: list[asyncio.AbstractEventLoop] = []

class MyThread(threading.Thread):
    def start(self):
        global loops
        loops.append(asyncio.get_event_loop())
        super().start()

async def func1():
    async with aiohttp.ClientSession() as session:
        async with session.get('https://www.okex.com/api/v5/public/time') as resp:
            print(resp.status)
            print(await resp.json())

threading.Thread = MyThread
import aiohttp

async def main():
    global loops
    loops.append(asyncio.get_running_loop())
    print(sys.argv)
    task = asyncio.create_task(func1())
    await task
    print('done.')

    currentTask = asyncio.current_task()
    for loop in loops:
        for t in asyncio.all_tasks(loop):
            if currentTask != t:
                await t

    print('done2.')
    #await asyncio.sleep(1)

#if __file__ == '__main__':
asyncio.run(main())


Then I found out the thread is created inside _loop.getaddrinfo: (files are from python 3.9.6)
    File aiohttp\resolver.py, line 31, in ThreadedResolver.resolve
    FILE asyncio\base_events.py, line 856, in BaseEventLoop(ProactorEventLoop).getaddrinfo

And it is strange that another thread is created when program exit:
    FILE asyncio\base_events.py, line 563, in BaseEventLoop(ProactorEventLoop).shutdown_default_executor

But sad it seems vscode cannot break a __del__ call. If I break somewhere  else first then it would not crash:(

----------
nosy: +machine.gw

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39232>
_______________________________________


More information about the Python-bugs-list mailing list