Skip to content

Commit

Permalink
Handle RuntimeError from transport #1790
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Apr 8, 2017
1 parent 9b7af75 commit fc1a04d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Changes

- Cancel websocket heartbeat on close #1793

- Handle RuntimeError from transport #1790

- Dropped "%O" in access logger #1673


Expand Down
5 changes: 5 additions & 0 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,11 @@ def start(self, message, payload, handler):
self.log_debug('Uncompleted request.')
self.close()

except RuntimeError as exc:
if self.debug:
self.log_exception(
'Unhandled runtime exception', exc_info=exc)
self.force_close()
except Exception as exc:
self.log_exception('Unhandled exception', exc_info=exc)
self.force_close()
Expand Down
27 changes: 27 additions & 0 deletions tests/test_web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,33 @@ def test_handle_error__utf(make_srv, buf, transport, loop, request_handler):
"Error handling request", exc_info=mock.ANY)


@asyncio.coroutine
def test_unhandled_runtime_error(make_srv, loop, transport, request_handler):

@asyncio.coroutine
def handle(request):
resp = web.Response()
resp.write_eof = mock.Mock()
resp.write_eof.side_effect = RuntimeError
return resp

srv = make_srv(lingering_time=0)
srv.debug = True
srv.connection_made(transport)
srv.logger.exception = mock.Mock()
request_handler.side_effect = handle

srv.data_received(
b'GET / HTTP/1.0\r\n'
b'Host: example.com\r\n'
b'Content-Length: 0\r\n\r\n')

yield from srv._request_handlers[0]
assert request_handler.called
srv.logger.exception.assert_called_with(
"Unhandled runtime exception", exc_info=mock.ANY)


@asyncio.coroutine
def test_handle_uncompleted(
make_srv, loop, transport, handle_with_error, request_handler):
Expand Down

0 comments on commit fc1a04d

Please sign in to comment.