Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ConnectionResetError #3648

Closed
decaz opened this issue Mar 12, 2019 · 7 comments
Closed

Handle ConnectionResetError #3648

decaz opened this issue Mar 12, 2019 · 7 comments

Comments

@decaz
Copy link
Contributor

decaz commented Mar 12, 2019

Long story short

There was introduced ConnectionResetError within #2989. What is the preferable way to handle this exception on the server side?

Expected behaviour

No exceptions or all exceptions are catched and handled.

Actual behaviour

Unhandled exception
Traceback (most recent call last):
  File "/home/decaz/.virtualenvs/test-aiohttp/lib/python3.6/site-packages/aiohttp/web_protocol.py", line 448, in start
    await resp.write_eof()
  File "/usr/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
    return self.gen.send(None)
  File "/home/decaz/.virtualenvs/test-aiohttp/lib/python3.6/site-packages/aiohttp/web_response.py", line 444, in write_eof
    await self._payload_writer.write_eof(data)
  File "/usr/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
    return self.gen.send(None)
  File "/home/decaz/.virtualenvs/test-aiohttp/lib/python3.6/site-packages/aiohttp/http_writer.py", line 138, in write_eof
    self._write(chunk)
  File "/home/decaz/.virtualenvs/test-aiohttp/lib/python3.6/site-packages/aiohttp/http_writer.py", line 67, in _write
    raise ConnectionResetError('Cannot write to closing transport')
ConnectionResetError: Cannot write to closing transport

Steps to reproduce

Close client connection while there is running coroutine on the server side.

Your environment

Server: aiohttp==3.5.4

@aio-libs-bot
Copy link

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #720 (My server cant handle Exception.), #255 (Handling WebSocket disconnects), #850 (How to handle ClientResponseError/ServerDisconnectedError properly), #962 (Handling very long connection timeouts), and #2489 (drop handle.cancel).

@h0rn3t
Copy link

h0rn3t commented Mar 19, 2019

have same problem with python-socketio library

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/aiohttp/web_protocol.py", line 447, in start
    await resp.prepare(request)
  File "/usr/local/lib/python3.6/dist-packages/aiohttp/web_response.py", line 353, in prepare
    return await self._start(request)
  File "/usr/local/lib/python3.6/dist-packages/aiohttp/web_response.py", line 667, in _start
    return await super()._start(request)
  File "/usr/local/lib/python3.6/dist-packages/aiohttp/web_response.py", line 410, in _start
    await writer.write_headers(status_line, headers)
  File "/usr/local/lib/python3.6/dist-packages/aiohttp/http_writer.py", line 112, in write_headers
    self._write(buf)
  File "/usr/local/lib/python3.6/dist-packages/aiohttp/http_writer.py", line 67, in _write
    raise ConnectionResetError('Cannot write to closing transport')
ConnectionResetError: Cannot write to closing transport

@gjcarneiro
Copy link
Contributor

Same here (python 3.7.3, aiohttp 3.5.4). I am unable to catch this exception in my code, as the traceback is 100% in aiohttp side. This happens very rarely, and I'm sure is no real problem, but it causes Sentry issues, so is kind of annoying:

ConnectionResetError: Cannot write to closing transport
  File "aiohttp/web_protocol.py", line 447, in start
    await resp.prepare(request)
  File "aiohttp/web_response.py", line 353, in prepare
    return await self._start(request)
  File "aiohttp/web_response.py", line 667, in _start
    return await super()._start(request)
  File "aiohttp/web_response.py", line 410, in _start
    await writer.write_headers(status_line, headers)
  File "aiohttp/http_writer.py", line 112, in write_headers
    self._write(buf)
  File "aiohttp/http_writer.py", line 67, in _write
    raise ConnectionResetError('Cannot write to closing transport')

@h0rn3t
Copy link

h0rn3t commented Apr 16, 2019

@gjcarneiro hi, i fixed that with patching streamwriter
this patch need do before import aiohttp.web

from httpwriter_patch import patch_streamwriter
patch_streamwriter()
from aiohttp import web

and patch code

def _write_no_exception(self, chunk: bytes) -> None:
    try:
        self.original_write(chunk)
    except ConnectionResetError as exc:
        log.debug('ConnectionResetError exception suppressed')


def patch_streamwriter():
    http_writer.StreamWriter.original_write = http_writer.StreamWriter._write
    http_writer.StreamWriter._write = _write_no_exception
    log.warning('StreamWriter patched to suppress ConnectionResetError\'s')

@gjcarneiro
Copy link
Contributor

That may not be the 100% correct fix for this: I am worried that ConnectionResetError probably needs to be properly handled somewhere, higher up the stack. But thanks!

gjcarneiro pushed a commit to gjcarneiro/aiohttp that referenced this issue Apr 16, 2019
@h0rn3t
Copy link

h0rn3t commented Apr 16, 2019

@gjcarneiro in my case, ConnectionResetError happens very often when client moves through the city and loses connection then just reconnect

gjcarneiro added a commit to gjcarneiro/aiohttp that referenced this issue May 6, 2019
gjcarneiro pushed a commit to gjcarneiro/aiohttp that referenced this issue May 6, 2019
asvetlov pushed a commit that referenced this issue May 7, 2019
(cherry picked from commit 7846174)

Co-authored-by: Gustavo J. A. M. Carneiro <[email protected]>
asvetlov added a commit that referenced this issue May 8, 2019
(cherry picked from commit 7846174)

Co-authored-by: Gustavo J. A. M. Carneiro <[email protected]>
@gjcarneiro
Copy link
Contributor

Would be nice to get a new aiohttp release to fix this bug.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants