Skip to content

Commit fc1a04d

Browse files
committed
Handle RuntimeError from transport #1790
1 parent 9b7af75 commit fc1a04d

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Changes
1515

1616
- Cancel websocket heartbeat on close #1793
1717

18+
- Handle RuntimeError from transport #1790
19+
1820
- Dropped "%O" in access logger #1673
1921

2022

aiohttp/web_protocol.py

+5
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ def start(self, message, payload, handler):
467467
self.log_debug('Uncompleted request.')
468468
self.close()
469469

470+
except RuntimeError as exc:
471+
if self.debug:
472+
self.log_exception(
473+
'Unhandled runtime exception', exc_info=exc)
474+
self.force_close()
470475
except Exception as exc:
471476
self.log_exception('Unhandled exception', exc_info=exc)
472477
self.force_close()

tests/test_web_protocol.py

+27
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,33 @@ def test_handle_error__utf(make_srv, buf, transport, loop, request_handler):
340340
"Error handling request", exc_info=mock.ANY)
341341

342342

343+
@asyncio.coroutine
344+
def test_unhandled_runtime_error(make_srv, loop, transport, request_handler):
345+
346+
@asyncio.coroutine
347+
def handle(request):
348+
resp = web.Response()
349+
resp.write_eof = mock.Mock()
350+
resp.write_eof.side_effect = RuntimeError
351+
return resp
352+
353+
srv = make_srv(lingering_time=0)
354+
srv.debug = True
355+
srv.connection_made(transport)
356+
srv.logger.exception = mock.Mock()
357+
request_handler.side_effect = handle
358+
359+
srv.data_received(
360+
b'GET / HTTP/1.0\r\n'
361+
b'Host: example.com\r\n'
362+
b'Content-Length: 0\r\n\r\n')
363+
364+
yield from srv._request_handlers[0]
365+
assert request_handler.called
366+
srv.logger.exception.assert_called_with(
367+
"Unhandled runtime exception", exc_info=mock.ANY)
368+
369+
343370
@asyncio.coroutine
344371
def test_handle_uncompleted(
345372
make_srv, loop, transport, handle_with_error, request_handler):

0 commit comments

Comments
 (0)