title: Closing aiohttp ClientSession when used in class
body: <p>I have the following code</p>
  
  <pre><code>session = aiohttp.ClientSession()
  
  
  async def fetch(session, url):
      while True:
          try:
              async with session.get(url) as response:
                  assert response.status == 200
                  return await response.json()
          except Exception as error:
              print(error)
  
  
  
  class FPL():
      async def get_player_summaries(self, player_ids=[], return_json=False):
          tasks = [asyncio.ensure_future(
                  fetch(session, API_URLS["player"].format(player_id)))
                  for player_id in player_ids]
  
          player_summaries = await asyncio.gather(*tasks)
  
          if return_json:
              return player_summaries
  
          return [PlayerSummary(player_summary)
                  for player_summary in player_summaries]
  
      async def get_points_against(self):
          players = await self.get_players(return_json=True)
          player_ids = [player["id"] for player in players]
          player_summaries = await self.get_player_summaries(
              player_ids, return_json=True)
          points_against = {}
  
          for player in players:
              try:
                  player_summary = next(
                      summary for summary in player_summaries
                      if summary["history"][0]["element"] == player["id"])
              except Exception:
                  continue
              player.update(player_summary)
              position = position_converter(player["element_type"]).lower()
  
              for fixture in player["history"]:
                  if fixture["minutes"] == 0:
                      continue
  
                  points = fixture["total_points"]
                  opponent = team_converter(fixture["opponent_team"])
                  location = "H" if fixture["was_home"] else "A"
  
                  points_against.setdefault(
                      opponent,
                      {
                          "all": {"H": [], "A": []},
                          "goalkeeper": {"H": [], "A": []},
                          "defender": {"H": [], "A": []},
                          "midfielder": {"H": [], "A": []},
                          "forward": {"H": [], "A": []}
                      }
                  )
  
                  points_against[opponent]["all"][location].append(points)
                  points_against[opponent][position][location].append(points)
  
          return points_against
  </code></pre>
  
  <p>which is used like so</p>
  
  <pre><code>from fpl import FPL
  import asyncio
  
  loop = asyncio.get_event_loop()
  fpl = FPL()
  
  x = loop.run_until_complete(fpl.get_points_against())
  print(x)
  </code></pre>
  
  <p>this works fine and <code>x</code> is printed, but it's accompanied by the following warning</p>
  
  <pre><code>Unclosed client session
  client_session: &lt;aiohttp.client.ClientSession object at 0x7fa2138111d0&gt;
  Unclosed connector
  connections: ['[(&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffac48&gt;, 7377.304262674), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8ee8&gt;, 7377.305252688), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8fa8&gt;, 7377.306179118), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9528&gt;, 7377.306921374), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc91c8&gt;, 7377.307587193), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9d08&gt;, 7377.308546534), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030be8&gt;, 7377.324784641), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8b88&gt;, 7377.325538414), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa4c8&gt;, 7377.326132111), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc92e8&gt;, 7377.327035234), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030fa8&gt;, 7377.327984252), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8408&gt;, 7377.328409375), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa5e8&gt;, 7377.329149849), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8a08&gt;, 7377.329776115), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa1c8&gt;, 7377.330400698), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3708&gt;, 7377.331131844), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8be8&gt;, 7377.331974929), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3e88&gt;, 7377.332880665), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3fa8&gt;, 7377.339996297), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa168&gt;, 7377.34077951), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3768&gt;, 7377.341734826), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3dc8&gt;, 7377.342455072), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9288&gt;, 7377.343152698), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa228&gt;, 7377.343844791), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3c48&gt;, 7377.344872661), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3d08&gt;, 7377.34534166), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffaee8&gt;, 7377.346136623), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3408&gt;, 7377.346773908), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa828&gt;, 7377.347680292), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8348&gt;, 7377.34843671), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c0305e8&gt;, 7377.349162388), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3f48&gt;, 7377.349909575), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffab88&gt;, 7377.350612879), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffaa68&gt;, 7377.351261815), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa588&gt;, 7377.352141721), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8168&gt;, 7377.352916437), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c0307c8&gt;, 7377.353702472), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa3a8&gt;, 7377.354645898), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030708&gt;, 7377.355418555), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd32e8&gt;, 7377.356222696), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030d08&gt;, 7377.356890307), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030828&gt;, 7377.357748581), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc98e8&gt;, 7377.358468552), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe89a8&gt;, 7377.36431476), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c0309a8&gt;, 7377.365148168), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030648&gt;, 7377.365791136), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa528&gt;, 7377.36643204), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030dc8&gt;, 7377.367176097), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c0308e8&gt;, 7377.367818125), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9708&gt;, 7377.36853057), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030ac8&gt;, 7377.369177849), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030e88&gt;, 7377.369894806), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa048&gt;, 7377.370589053), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8e28&gt;, 7377.371427258), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9ee8&gt;, 7377.37224736), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8588&gt;, 7377.372942966), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3048&gt;, 7377.373591994), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe86a8&gt;, 7377.374399274), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa0a8&gt;, 7377.374866638), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3288&gt;, 7377.375751613), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8528&gt;, 7377.376432785), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd39a8&gt;, 7377.377390439), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c2fcd68&gt;, 7377.381260399), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe83a8&gt;, 7377.382196696), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030a68&gt;, 7377.382944559), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c030ee8&gt;, 7377.3833559), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8468&gt;, 7377.383995208), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffabe8&gt;, 7377.38498033), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9c48&gt;, 7377.385649648), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9108&gt;, 7377.38617506), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c52bfa8&gt;, 7377.388227618), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c52bf48&gt;, 7377.388934452), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8d08&gt;, 7377.393284709), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8108&gt;, 7377.394200951), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9ca8&gt;, 7377.395036827), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffae28&gt;, 7377.396141727), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9b28&gt;, 7377.396889149), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8888&gt;, 7377.397542457), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa768&gt;, 7377.398182665), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffaca8&gt;, 7377.398876879), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9be8&gt;, 7377.399550454), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa6a8&gt;, 7377.400299738), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8288&gt;, 7377.401028687), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c35a648&gt;, 7377.401664671), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9648&gt;, 7377.402413862), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8828&gt;, 7377.403071713), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3228&gt;, 7377.407205263), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9d68&gt;, 7377.408117483), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa288&gt;, 7377.40877989), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20d570be8&gt;, 7377.409435516), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc96a8&gt;, 7377.409980892), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffa468&gt;, 7377.410663022), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe81c8&gt;, 7377.411396958), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9a68&gt;, 7377.412164899), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fc9348&gt;, 7377.412951192), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3468&gt;, 7377.413625465), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa20c52bee8&gt;, 7377.414277707), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207ffad68&gt;, 7377.414928317), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fe8ac8&gt;, 7377.415578122), (&lt;aiohttp.client_proto.ResponseHandler object at 0x7fa207fd3b88&gt;, 7377.415948294)]']
  connector: &lt;aiohttp.connector.TCPConnector object at 0x7fa20d72c710&gt;
  </code></pre>
  
  <p>The <code>FPL</code> class has many other functions, all which seem to work fine, so I can only assume the session <em>is</em> closed for those (the sessions not being closed is probably caused my the high number of requests made (~570), while the others are &lt; 30). I've tried changing the class by making the session an argument that is passed to the class, and then closing it after, but that doesn't seem to work either. In the documentation it is mentioned that session context manager usage is not mandatory, but <code>await session.close()</code> method should be called in this case. Since the way I'm using it in a class isn't working, it is probably not the correct way of instantiating the <code>session</code>.</p>
  
  <p>How are you supposed use an <code>aiohttp ClientSession</code> in a class?</p>