title: How to retry the task when aiohttp.ClientSession fails in async
body: <p>I am struggling to understand this behavior as I am new to async functions in Python.</p>
  <p>I am trying to create this simple download tool and I have this function</p>
  <pre><code>async def download_all_pages(sites):
      print('Running download all pages')
      try:
          async with aiohttp.ClientSession() as session:
              tasks = [asyncio.ensure_future(safe_download_page(session,url)) for url in sites]
              await asyncio.gather(*tasks, return_exceptions = True)
              try:
                  await asyncio.sleep(0.25)
              except asyncio.CancelledError:
                  print(&quot;Got CancelledError&quot;)
      except (aiohttp.ServerDisconnectedError, aiohttp.ClientResponseError,aiohttp.ClientConnectorError) as s:
          print(&quot;Oops, the server connection was dropped before we finished.&quot;)
          print(s)
  </code></pre>
  <p>I init this function like below :</p>
  <pre><code>try:
      loop.run_until_complete(download_all_pages([url+'/'+str(i) for i in range(1, nb_pages+1)]))
      loop.run_until_complete(download_all_sites([result['href'] for result in results]))
  finally:
      loop.run_until_complete(loop.shutdown_asyncgens())
      loop.close()
  print('Finished at '+str(datetime.timestamp(datetime.now())))
  
  </code></pre>
  <p>Whenever I get an error, in this example mainly a aiohttp.ServerDisconnectedError; the output show</p>
  <pre><code>Oops, the server connection was dropped before we finished.
  Server disconnected
  Finished at 1606440807.007339
  Task was destroyed but it is pending!
  Task was destroyed but it is pending!
  Task was destroyed but it is pending!
  Task was destroyed but it is pending!
  Task was destroyed but it is pending!
  Task was destroyed but it is pending!
  </code></pre>
  <p>... and just a million <code>Task was destroyed but it is pending!</code></p>
  <p>So when this error occurs, I dont want the function to finish since there are still a lot of tasks to achieve; hence the error <strong>Task was destroyed but it is pending!</strong>.</p>
  <p>And as you can see, it calls the <em>print('Finished at')</em> before it even called the <strong>loop.run_until_complete(download_all_sites([result['href'])</strong>; it seems to completely exit the whole script. <em>(EDIT : I think I found out why this happens.
  Because of the try: above, since it fails, it goes straight to the finally: clause therefore destroying pending tasks. Still the question of how to avoid that whole disconnect issue remains)</em></p>
  <p>Do you have any idea how I could safely retry the task that had the aiohttp.ServerDisconnectedError error ?</p>
  <p>Does this have to do with not using <code>if __name__ == &quot;__main__&quot;:</code> ?</p>