Add exception handling for closed transports
Adds handling for closed transports in the server for `write_response` as well as `write_error`, letting it all flow to `bail_out` seemed to be a little light handed in terms of telling the logs where the error actually occured. Handling to fix the infinite `write_error` loop is still there and those exceptions will get reported on in the debug logs.
This commit is contained in:
parent
99c8d779dc
commit
e9bfa30c1d
|
@ -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,8 +163,8 @@ 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))
|
||||||
|
@ -175,6 +174,10 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
# 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 RuntimeError:
|
||||||
|
log.error(
|
||||||
|
'Connection lost before response written @ {}'.format(
|
||||||
|
self.request.ip))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.bail_out(
|
self.bail_out(
|
||||||
"Writing response failed, connection closed {}".format(e))
|
"Writing response failed, connection closed {}".format(e))
|
||||||
|
@ -185,16 +188,23 @@ class HttpProtocol(asyncio.Protocol):
|
||||||
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()
|
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)
|
||||||
|
|
||||||
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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user