GH-93896: AAlways set event loop in asyncio.run and IsolatedAsyncioTestCase (#94593)

This commit is contained in:
Kumar Aditya 2022-07-06 20:48:21 +05:30 committed by GitHub
parent e925241d95
commit 14fea6b4d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View file

@ -52,6 +52,7 @@ class Runner:
self._loop = None
self._context = None
self._interrupt_count = 0
self._set_event_loop = False
def __enter__(self):
self._lazy_init()
@ -70,6 +71,8 @@ class Runner:
loop.run_until_complete(loop.shutdown_asyncgens())
loop.run_until_complete(loop.shutdown_default_executor())
finally:
if self._set_event_loop:
events.set_event_loop(None)
loop.close()
self._loop = None
self._state = _State.CLOSED
@ -111,6 +114,8 @@ class Runner:
self._interrupt_count = 0
try:
if self._set_event_loop:
events.set_event_loop(self._loop)
return self._loop.run_until_complete(task)
except exceptions.CancelledError:
if self._interrupt_count > 0 and task.uncancel() == 0:
@ -130,6 +135,7 @@ class Runner:
return
if self._loop_factory is None:
self._loop = events.new_event_loop()
self._set_event_loop = True
else:
self._loop = self._loop_factory()
if self._debug is not None: