title: Why am I getting a ConnectionResetError here?
body: <p>I am new to Python 3 and am playing around with asyncio. Thereby, I am experiencing a strange behavior with the following server-side code:</p>
  
  <pre><code>import asyncio
  
  
  @asyncio.coroutine
  def handle_client(reader, writer):
      print('Client connected.')
      client_connected = True
      while client_connected:
          print('Waiting for client event.')
          line = yield from reader.readline()
          if line:
              print('Got: {}'.format(line))
              if line.decode() == 'echo\n':
                  print('Sending back echo.')
                  writer.write(line)
              else:
                  print('Not sending back anything.')
          else:
              print('Client disconnected.')
              client_connected = False
  
  
  if __name__ == '__main__':
      asyncio.async(asyncio.start_server(handle_client, 'localhost', 8888))
      asyncio.get_event_loop().run_forever()
  </code></pre>
  
  <p>When I run this client code (EDIT: client code is entered manually into an IPython session, the server definitely has time to write before I close the socket)...</p>
  
  <pre><code>import socket
  client = socket.create_connection(('localhost', 8888))
  client.sendall('echo\n'.encode())
  client.close()
  </code></pre>
  
  <p>... I get an error traceback from the server:</p>
  
  <pre><code>C:\Users\Gnar\Anaconda3\python.exe C:/Users/Gnar/Code/echo.py
  Client connected.
  Waiting for client event.
  Got: b'echo\n'
  Sending back echo.
  Waiting for client event.
  Task exception was never retrieved
  future: &lt;Task finished coro=&lt;handle_client() done, defined at C:/Users/Gnar/Code/echo.py:4&gt; exception=ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)&gt;
  Traceback (most recent call last):
    File "C:\Users\Gnar\Anaconda3\lib\asyncio\tasks.py", line 234, in _step
      result = coro.throw(exc)
    File "C:/Users/Gnar/Code/echo.py", line 10, in handle_client
      line = yield from reader.readline()
    File "C:\Users\Gnar\Anaconda3\lib\asyncio\streams.py", line 425, in readline
      yield from self._wait_for_data('readline')
    File "C:\Users\Gnar\Anaconda3\lib\asyncio\streams.py", line 393, in _wait_for_data
      yield from self._waiter
    File "C:\Users\Gnar\Anaconda3\lib\asyncio\futures.py", line 386, in __iter__
      yield self  # This tells Task to wait for completion.
    File "C:\Users\Gnar\Anaconda3\lib\asyncio\tasks.py", line 287, in _wakeup
      value = future.result()
    File "C:\Users\Gnar\Anaconda3\lib\asyncio\futures.py", line 275, in result
      raise self._exception
    File "C:\Users\Gnar\Anaconda3\lib\asyncio\selector_events.py", line 662, in _read_ready
      data = self._sock.recv(self.max_size)
  ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
  </code></pre>
  
  <p>The issue must be somehow in relation with <code>writer.write</code>, because when I call the following client code (which makes the server skip the writing), there is no error:</p>
  
  <pre><code>import socket
  client = socket.create_connection(('localhost', 8888))
  client.sendall('foo\n'.encode())
  client.close()
  </code></pre>
  
  <p>The corresponding server log:</p>
  
  <pre><code>C:\Users\Gnar\Anaconda3\python.exe C:/Users/Gnar/Code/echo.py
  Client connected.
  Waiting for client event.
  Got: b'foo\n'
  Not sending back anything.
  Waiting for client event.
  Client disconnected.
  </code></pre>
  
  <p>What am I missing? Am I using asyncio incorrectly?</p>
  
  <p>Thanks!</p>