fix: random hangs in CI
This commit is contained in:
@@ -127,26 +127,29 @@ class ExternalMessageConsumer:
|
|||||||
|
|
||||||
self._channel_streams = {}
|
self._channel_streams = {}
|
||||||
|
|
||||||
if self._sub_tasks:
|
asyncio.run_coroutine_threadsafe(self._shutdown_async(), loop=self._loop)
|
||||||
# Cancel sub tasks
|
|
||||||
for task in self._sub_tasks:
|
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
if self._main_task:
|
self._thread.join(timeout=5)
|
||||||
# Cancel the main task
|
|
||||||
self._main_task.cancel()
|
|
||||||
|
|
||||||
self._thread.join()
|
|
||||||
|
|
||||||
self._thread = None
|
self._thread = None
|
||||||
self._loop = None
|
self._loop = None
|
||||||
self._sub_tasks = None
|
self._sub_tasks = None
|
||||||
self._main_task = None
|
self._main_task = None
|
||||||
|
|
||||||
|
async def _shutdown_async(self):
|
||||||
|
"""Cancel all tasks, let them finish, then stop the loop."""
|
||||||
|
if self._sub_tasks:
|
||||||
|
for task in self._sub_tasks:
|
||||||
|
task.cancel()
|
||||||
|
await asyncio.gather(*self._sub_tasks, return_exceptions=True)
|
||||||
|
|
||||||
|
if self._main_task:
|
||||||
|
self._main_task.cancel()
|
||||||
|
|
||||||
|
self._loop.stop()
|
||||||
|
|
||||||
async def _main(self):
|
async def _main(self):
|
||||||
"""
|
"""The main task coroutine"""
|
||||||
The main task coroutine
|
|
||||||
"""
|
|
||||||
lock = asyncio.Lock()
|
lock = asyncio.Lock()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -161,7 +164,8 @@ class ExternalMessageConsumer:
|
|||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
# Stop the loop once we are done
|
# Stop the loop once we are done
|
||||||
self._loop.stop()
|
if self._loop:
|
||||||
|
self._loop.stop()
|
||||||
|
|
||||||
async def _handle_producer_connection(self, producer: Producer, lock: asyncio.Lock):
|
async def _handle_producer_connection(self, producer: Producer, lock: asyncio.Lock):
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user