title: How to close aiohttp ClientSession
body: <p>I am trying to make an app that might live for a day, a week or longer.
  Dyring the app's lifetime, it will make requests to different API's. Some of these apis might require log in, so it is important that 
  i have access to cookies at all times.</p>
  
  <p>So what i need is a file that the different API's can use without blocking the app. </p>
  
  <p>I am new to asynchronous programming(asyncio/aiohttp) and examples i have seen, shows how to make a lot of requests from a list of url's, but this is not what i need.</p>
  
  <p>The problem with the code i have is, either i get ClientSession is closed error or unclosed ClientSession warnings.</p>
  
  <pre><code>import asyncio  # only here for debugging purposes
  import aiohttp
  
  USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:61.0) Gecko/20100101 Firefox/61.1'
  
  
  def default_headers():
      header = {
          'User-Agent': USER_AGENT
      }
      return header
  
  
  class WebSession(object):
      session = None
  
      @classmethod
      def create(cls):
          cls.session = aiohttp.ClientSession()
          return cls.session
  
      @classmethod
      def close(cls):
          if cls.session is not None:
              cls.session.close()
  
  async def request(method, url, **kwargs):
  
      if kwargs.get('headers', None) is None:
          kwargs['headers'] = default_headers()
  
      if WebSession.session is None:
          session = WebSession.create()
      else:
          session = WebSession.session
  
  
      async with session.request(method=method, url=url, **kwargs) as response:
          if isinstance(session, aiohttp.ClientSession):
              # if i close the session here, i will get the ClientSession closed error on 2. request.
              # await session.close()
              pass
  
          return response
  
  
  async def get(url, **kwargs):
      return await request('GET', url=url, **kwargs)
  
  
  async def post(url, **kwargs):
      return await request('POST', url=url, **kwargs)
  
  
  async def get_url():
      res = await get('https://httpbin.org/get')
      print(f'Status code: {res.headers}')
  
  
  m_loop = asyncio.get_event_loop()
  m_loop.run_until_complete(get_url())
  # if i run this without closing the ClientSession, i will get unclosed ClientSession warnings.
  m_loop.run_until_complete(get_url())
  m_loop.close()
  </code></pre>
  
  <p>I do get a response from the server, however it is followed by this error/warning</p>
  
  <pre><code>Unclosed client session
  client_session: &lt;aiohttp.client.ClientSession object at 0x03354630&gt;
  Unclosed connector
  connections: ['[(&lt;aiohttp.client_proto.ResponseHandler object at   0x033BBBF0&gt;, 71.542)]']
  connector: &lt;aiohttp.connector.TCPConnector object at 0x033542D0&gt;
  </code></pre>
  
  <p>If i uncomment the <code>await session.close()</code> and remove the <code>pass</code> i get a response from the server in the first request, followed by <code>RuntimeError: Session is closed</code> in the second request.</p>