title: ConnectionResetError('Connection lost') python asyncio server
body: <p>I am writing a redis server for a redis client connecting on port 9000 (using redis-py).</p>
  
  <p>I wrote a simple test code for now to check if the server is receiving the client's command. My code is as follows:</p>
  
  <p><strong>client.py</strong></p>
  
  <pre><code>import redis
  
  r = redis.Redis(
      host="127.0.0.1",
      port=9000
  )
  
  v = r.set('foo', 1)
  print(v)
  
  v = r.set('bar', '1')
  </code></pre>
  
  <p><strong>server.py</strong></p>
  
  <pre><code>import asyncio
  
  HOST = "127.0.0.1"
  PORT = 15000
  
  
  async def exo_redis(reader, writer):
  
      try:
          while True:
              data = await reader.readuntil(b'\r\n')
              # data = await reader.read(1024)
              if not data:
                  break
              print("Data: ", data)
              response = "+OK\r\n"
              writer.write(response.encode())
              await writer.drain()
  
          writer.close()
  
      except asyncio.streams.IncompleteReadError:
          pass
  
  
  def start_server():
      loop = asyncio.get_event_loop()
      coro = asyncio.start_server(exo_redis, HOST, PORT)
      server = loop.run_until_complete(coro)
      print("Serving on ", HOST, "port: ", PORT)
      try:
          loop.run_forever()
      except KeyboardInterrupt:
          print("\nShutting Down Server...\n")
      finally:
          server.close()
          loop.run_until_complete(server.wait_closed())
          loop.close()
  
  
  if __name__ == '__main__':
      start_server()
  </code></pre>
  
  <p>Upon running this, I am getting the output with the following exception:</p>
  
  <pre><code>Data:  b'*3\r\n'
  Data:  b'$3\r\n'
  Data:  b'SET\r\n'
  Data:  b'$3\r\n'
  Data:  b'foo\r\n'
  Task exception was never retrieved
  future: &lt;Task finished coro=&lt;exo_redis() done, defined at app.py:13&gt; exception=ConnectionResetError('Connection lost')&gt;
  Traceback (most recent call last):
    File "app.py", line 23, in exo_redis
      await writer.drain()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/streams.py", line 348, in drain
      await self._protocol._drain_helper()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/streams.py", line 202, in _drain_helper
      raise ConnectionResetError('Connection lost')
  ConnectionResetError: Connection lost
  Data:  b'*3\r\n'
  Data:  b'$3\r\n'
  Data:  b'SET\r\n'
  Data:  b'$3\r\n'
  Data:  b'bar\r\n'
  Data:  b'$1\r\n'
  Data:  b'1\r\n'
  </code></pre>
  
  <p>For some reason, the 1st command sent from the client throws an exception but the second one does not.</p>
  
  <p>If I use reader.read(1024) (commented in the code), I get the following output:</p>
  
  <pre><code>Data:  b'*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$1\r\n1\r\n'
  Data:  b'*3\r\n$3\r\nSET\r\n$3\r\nbar\r\n$1\r\n1\r\n'
  </code></pre>
  
  <p>This works sometimes and there is no exception thrown, but some other times I am getting this exception with the command not being read completely. Any ideas on how this can be rectified and I can ensure that the entire command is being received at the server end?</p>