# Buggy code
async def _do_run(self) -> None:
    with child_process_logging(self._boot_info):
        endpoint_name = self.get_endpoint_name()
        event_bus_service = AsyncioEventBusService(
            self._boot_info.trinity_config,
            endpoint_name,
        )
        async with background_asyncio_service(event_bus_service) as eventbus_manager:
            event_bus = await event_bus_service.get_event_bus()
            loop_monitoring_task = create_task(
                self._loop_monitoring_task(event_bus),
                f"AsyncioIsolatedComponent/{self.name}/loop_monitoring_task",
            )

            do_run_task = create_task(
                self.do_run(event_bus), f"AsyncioIsolatedComponent/{self.name}/do_run"
            )
            eventbus_task = create_task(
                eventbus_manager.wait_finished(),
                f"AsyncioIsolatedComponent/{self.name}/eventbus/wait_finished",
            )
            try:
                max_wait_after_cancellation = 2
                if self._boot_info.profile:
                    with profiler(f"profile_{self.get_endpoint_name()}"):
                        await wait_first(
                            [do_run_task, eventbus_task, loop_monitoring_task],
                            max_wait_after_cancellation,
                        )
                else:
                    # XXX: When open_in_process() injects a KeyboardInterrupt into us (via
                    # coro.throw()), we hang forever here, until open_in_process() times
                    # out and sends us a SIGTERM, at which point we exit without executing
                    # either the except or the finally blocks below.
                    # See https://github.com/ethereum/trinity/issues/1711 for more.
                    await wait_first(
                        [do_run_task, eventbus_task, loop_monitoring_task],
                        max_wait_after_cancellation,
                    )
            except KeyboardInterrupt:
                self.logger.debug("%s: KeyboardInterrupt", self)
                # Currently we never reach this code path, but when we fix the issue above
                # it will be needed.
                return
            finally:
                # Once we start seeing this in the logs after a Ctrl-C, we'll likely have
                # figured out the issue above.
                self.logger.debug("%s: do_run() finished", self)


# 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) -> None:
    with child_process_logging(self._boot_info):
        endpoint_name = self.get_endpoint_name()
        event_bus_service = AsyncioEventBusService(
            self._boot_info.trinity_config,
            endpoint_name,
        )
        async with background_asyncio_service(event_bus_service) as eventbus_manager:
            event_bus = await event_bus_service.get_event_bus()
            loop_monitoring_task = create_task(
                self._loop_monitoring_task(event_bus),
                f"AsyncioIsolatedComponent/{self.name}/loop_monitoring_task",
            )

            do_run_task = create_task(
                self.do_run(event_bus), f"AsyncioIsolatedComponent/{self.name}/do_run"
            )
            eventbus_task = create_task(
                eventbus_manager.wait_finished(),
                f"AsyncioIsolatedComponent/{self.name}/eventbus/wait_finished",
            )
            try:
                max_wait_after_cancellation = 2
                tasks = [do_run_task, eventbus_task, loop_monitoring_task]
                if self._boot_info.profile:
                    with profiler(f"profile_{self.get_endpoint_name()}"):
                        try:
                            await wait_first(
                                tasks,
                                max_wait_after_cancellation,
                            )
                        except asyncio.TimeoutError:
                            self.logger.warning(
                                "Timed out waiting for tasks to "
                                "terminate after cancellation: %s",
                                tasks,
                            )

                else:
                    # XXX: When open_in_process() injects a KeyboardInterrupt into us (via
                    # coro.throw()), we hang forever here, until open_in_process() times
                    # out and sends us a SIGTERM, at which point we exit without executing
                    # either the except or the finally blocks below.
                    # See https://github.com/ethereum/trinity/issues/1711 for more.
                    try:
                        await wait_first(
                            tasks,
                            max_wait_after_cancellation,
                        )
                    except asyncio.TimeoutError:
                        self.logger.warning(
                            "Timed out waiting for tasks to terminate after cancellation: %s",
                            tasks,
                        )

            except KeyboardInterrupt:
                self.logger.debug("%s: KeyboardInterrupt", self)
                # Currently we never reach this code path, but when we fix the issue above
                # it will be needed.
                return
            finally:
                # Once we start seeing this in the logs after a Ctrl-C, we'll likely have
                # figured out the issue above.
                self.logger.debug("%s: do_run() finished", self)