This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author machine.gw
Recipients asvetlov, cjrh, cmeyer, jack1142, lawsonjl.ornl, machine.gw, mikeshardmind, pepoluan, rmawatson, steve.dower, yselivanov
Date 2021-08-23.12:27:40
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1629721661.04.0.750137469404.issue39232@roundup.psfhosted.org>
In-reply-to
Content
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:(
History
Date User Action Args
2021-08-23 12:27:41machine.gwsetrecipients: + machine.gw, asvetlov, cjrh, yselivanov, steve.dower, cmeyer, mikeshardmind, jack1142, rmawatson, pepoluan, lawsonjl.ornl
2021-08-23 12:27:41machine.gwsetmessageid: <1629721661.04.0.750137469404.issue39232@roundup.psfhosted.org>
2021-08-23 12:27:41machine.gwlinkissue39232 messages
2021-08-23 12:27:40machine.gwcreate