code: async def _restart_on_cancel(logger, agent):
      
      while True:
          try:
              await agent.run()
          except asyncio.CancelledError:
              logger.exception("Restarting agent")
              pass
docs: Restarts an agent when it is cancelled