From 573d1da0efa1812b78a137a78e28b5db8cbb8559 Mon Sep 17 00:00:00 2001 From: Eli Uriegas Date: Tue, 17 Jan 2017 18:28:22 -0600 Subject: [PATCH] Fixes write_error loop from bail_out function Fixes stack-overflow found in #307 --- sanic/server.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/sanic/server.py b/sanic/server.py index 711117dc..a36c47aa 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -1,4 +1,5 @@ import asyncio +import traceback from functools import partial from inspect import isawaitable from signal import SIGINT, SIGTERM @@ -189,9 +190,15 @@ class HttpProtocol(asyncio.Protocol): "Writing error failed, connection closed {}".format(e)) def bail_out(self, message): - exception = ServerError(message) - self.write_error(exception) - log.error(message) + if self.transport.is_closing(): + log.error( + "Connection closed before error was sent to user @ {}".format( + self.transport.get_extra_info('peername'))) + log.debug('Error experienced:\n{}'.format(traceback.format_exc())) + else: + exception = ServerError(message) + self.write_error(exception) + log.error(message) def cleanup(self): self.parser = None