# Buggy code
async def do_run(self, event_bus: EndpointAPI) -> None:
    boot_info = self._boot_info

    if boot_info.args.enable_metrics:
        metrics_service = metrics_service_from_args(
            boot_info.args, AsyncioMetricsService
        )
    else:
        # Use a NoopMetricsService so that no code branches need to be taken if metrics
        # are disabled
        metrics_service = NOOP_METRICS_SERVICE

    trinity_config = boot_info.trinity_config
    NodeClass = trinity_config.get_app_config(Eth1AppConfig).node_class
    node = NodeClass(event_bus, metrics_service, trinity_config)
    strategy = self.get_active_strategy(boot_info)

    async with background_asyncio_service(node) as node_manager:
        sync_task = create_task(
            self.launch_sync(node, strategy, boot_info, event_bus), self.name
        )
        # The Node service is our responsibility, so we must exit if either that or the syncer
        # returns.
        node_manager_task = create_task(
            node_manager.wait_finished(), f"{NodeClass.__name__} wait_finished() task"
        )
        await wait_first([sync_task, node_manager_task], max_wait_after_cancellation=2)


# Traceback (trio.MultiError)
�[1m�[33m WARNING  2020-09-04 04:19:18,179       Sync / PeerPool  Event loop blocked or overloaded: delay=2.005s, tasks=74�[0m
�[1m�[31m   ERROR  2020-09-04 04:19:18,181               asyncio  Exception in callback <TaskWakeupMethWrapper object at 0x7fa47b456e90>
handle: <Handle <TaskWakeupMethWrapper object at 0x7fa47b456e90>>
Traceback (most recent call last):
File "uvloop/cbhandles.pyx", line 70, in uvloop.loop.Handle._run
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 300, in _run_and_manage_task
await task.run()
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 165, in run
await self.child_manager.run()
File "/usr/local/lib/python3.7/site-packages/async_service/asyncio.py", line 246, in run
for _, exc_value, exc_tb in self._errors
trio.MultiError: TimeoutError('Tasks never returned after being cancelled: %s', {<Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>}), TimeoutError('Tasks never returned after being cancelled: %s', {<Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>})

Details of embedded exception 1:

Traceback (most recent call last):
File "/usr/src/app/trinity/p2p/asyncio_utils.py", line 82, in cancel_pending_tasks
yield
File "/usr/src/app/trinity/p2p/asyncio_utils.py", line 54, in wait_first
done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
File "/usr/local/lib/python3.7/asyncio/tasks.py", line 389, in wait
return await _wait(fs, timeout, return_when, loop)
File "/usr/local/lib/python3.7/asyncio/tasks.py", line 482, in _wait
await waiter
concurrent.futures._base.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 300, in _run_and_manage_task
await task.run()
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 165, in run
await self.child_manager.run()
File "/usr/local/lib/python3.7/site-packages/async_service/asyncio.py", line 246, in run
for _, exc_value, exc_tb in self._errors
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 300, in _run_and_manage_task
await task.run()
File "/usr/local/lib/python3.7/site-packages/async_service/asyncio.py", line 35, in run
await self._async_fn(*self._async_fn_args)
File "/usr/src/app/trinity/p2p/connection.py", line 133, in run_behaviors
await wait_first(futures, max_wait_after_cancellation=2)
File "/usr/src/app/trinity/p2p/asyncio_utils.py", line 69, in wait_first
raise done_task.exception()
File "/usr/local/lib/python3.7/contextlib.py", line 188, in __aexit__
await self.gen.athrow(typ, value, traceback)
File "/usr/src/app/trinity/p2p/asyncio_utils.py", line 113, in cancel_pending_tasks
raise MultiError(errors)
concurrent.futures._base.TimeoutError: ('Tasks never returned after being cancelled: %s', {<Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>})

Details of embedded exception 2:

Traceback (most recent call last):
File "/usr/src/app/trinity/p2p/asyncio_utils.py", line 82, in cancel_pending_tasks
yield
File "/usr/src/app/trinity/p2p/asyncio_utils.py", line 54, in wait_first
done, pending = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED)
File "/usr/local/lib/python3.7/asyncio/tasks.py", line 389, in wait
return await _wait(fs, timeout, return_when, loop)
File "/usr/local/lib/python3.7/asyncio/tasks.py", line 482, in _wait
await waiter
concurrent.futures._base.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 300, in _run_and_manage_task
await task.run()
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 165, in run
await self.child_manager.run()
File "/usr/local/lib/python3.7/site-packages/async_service/asyncio.py", line 246, in run
for _, exc_value, exc_tb in self._errors
File "/usr/local/lib/python3.7/site-packages/async_service/base.py", line 300, in _run_and_manage_task
await task.run()
File "/usr/local/lib/python3.7/site-packages/async_service/asyncio.py", line 35, in run
await self._async_fn(*self._async_fn_args)
File "/usr/src/app/trinity/p2p/connection.py", line 133, in run_behaviors
await wait_first(futures, max_wait_after_cancellation=2)
File "/usr/src/app/trinity/p2p/asyncio_utils.py", line 69, in wait_first
raise done_task.exception()
File "/usr/local/lib/python3.7/contextlib.py", line 188, in __aexit__
await self.gen.athrow(typ, value, traceback)
File "/usr/src/app/trinity/p2p/asyncio_utils.py", line 113, in cancel_pending_tasks
raise MultiError(errors)
concurrent.futures._base.TimeoutError: ('Tasks never returned after being cancelled: %s', {<Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>, <Task cancelled coro=<wait_first() done, defined at /usr/src/app/trinity/p2p/asyncio_utils.py:31>>})�[0m

# Fixed code
async def do_run(self, event_bus: EndpointAPI) -> None:
    boot_info = self._boot_info

    if boot_info.args.enable_metrics:
        metrics_service = metrics_service_from_args(
            boot_info.args, AsyncioMetricsService
        )
    else:
        # Use a NoopMetricsService so that no code branches need to be taken if metrics
        # are disabled
        metrics_service = NOOP_METRICS_SERVICE

    trinity_config = boot_info.trinity_config
    NodeClass = trinity_config.get_app_config(Eth1AppConfig).node_class
    node = NodeClass(event_bus, metrics_service, trinity_config)
    strategy = self.get_active_strategy(boot_info)

    async with background_asyncio_service(node) as node_manager:
        sync_task = create_task(
            self.launch_sync(node, strategy, boot_info, event_bus), self.name
        )
        # The Node service is our responsibility, so we must exit if either that or the syncer
        # returns.
        node_manager_task = create_task(
            node_manager.wait_finished(), f"{NodeClass.__name__} wait_finished() task"
        )
        tasks = [sync_task, node_manager_task]
        try:
            await wait_first(tasks, max_wait_after_cancellation=2)
        except asyncio.TimeoutError:
            self.logger.warning(
                "Timed out waiting for tasks to terminate after cancellation: %s", tasks
            )