Merge pull request #314 from seemethere/make_closed_transport_handling_more_robust

Add exception handling for closed transports
This commit is contained in:
Eli Uriegas 2017-01-19 21:31:30 -06:00 committed by GitHub
commit bef34d66f5

View File

@ -89,15 +89,14 @@ class HttpProtocol(asyncio.Protocol):
def connection_lost(self, exc): def connection_lost(self, exc):
self.connections.discard(self) self.connections.discard(self)
self._timeout_handler.cancel() self._timeout_handler.cancel()
self.cleanup()
def connection_timeout(self): def connection_timeout(self):
# Check if # Check if
time_elapsed = current_time - self._last_request_time time_elapsed = current_time - self._last_request_time
if time_elapsed < self.request_timeout: if time_elapsed < self.request_timeout:
time_left = self.request_timeout - time_elapsed time_left = self.request_timeout - time_elapsed
self._timeout_handler = \ self._timeout_handler = (
self.loop.call_later(time_left, self.connection_timeout) self.loop.call_later(time_left, self.connection_timeout))
else: else:
if self._request_handler_task: if self._request_handler_task:
self._request_handler_task.cancel() self._request_handler_task.cancel()
@ -164,37 +163,50 @@ class HttpProtocol(asyncio.Protocol):
def write_response(self, response): def write_response(self, response):
try: try:
keep_alive = self.parser.should_keep_alive() \ keep_alive = (
and not self.signal.stopped self.parser.should_keep_alive() and not self.signal.stopped)
self.transport.write( self.transport.write(
response.output( response.output(
self.request.version, keep_alive, self.request_timeout)) self.request.version, keep_alive, self.request_timeout))
except RuntimeError:
log.error(
'Connection lost before response written @ {}'.format(
self.request.ip))
except Exception as e:
self.bail_out(
"Writing response failed, connection closed {}".format(e))
finally:
if not keep_alive: if not keep_alive:
self.transport.close() self.transport.close()
else: else:
# Record that we received data # Record that we received data
self._last_request_time = current_time self._last_request_time = current_time
self.cleanup() self.cleanup()
except Exception as e:
self.bail_out(
"Writing response failed, connection closed {}".format(e))
def write_error(self, exception): def write_error(self, exception):
try: try:
response = self.error_handler.response(self.request, exception) response = self.error_handler.response(self.request, exception)
version = self.request.version if self.request else '1.1' version = self.request.version if self.request else '1.1'
self.transport.write(response.output(version)) self.transport.write(response.output(version))
self.transport.close() except RuntimeError:
log.error(
'Connection lost before error written @ {}'.format(
self.request.ip))
except Exception as e: except Exception as e:
self.bail_out( self.bail_out(
"Writing error failed, connection closed {}".format(e)) "Writing error failed, connection closed {}".format(e),
from_error=True)
finally:
self.transport.close()
def bail_out(self, message): def bail_out(self, message, from_error=False):
if self.transport.is_closing(): if from_error and self.transport.is_closing():
log.error( log.error(
"Connection closed before error was sent to user @ {}".format( ("Transport closed @ {} and exception "
"experienced during error handling").format(
self.transport.get_extra_info('peername'))) self.transport.get_extra_info('peername')))
log.debug('Error experienced:\n{}'.format(traceback.format_exc())) log.debug(
'Exception:\n{}'.format(traceback.format_exc()))
else: else:
exception = ServerError(message) exception = ServerError(message)
self.write_error(exception) self.write_error(exception)