From 12ec996d54be3775dfa6f25a7ee5bc0dfce44a5a Mon Sep 17 00:00:00 2001 From: Alexandre Zani Date: Thu, 28 Feb 2013 23:43:23 -0800 Subject: [PATCH] Close connection on error after sending started. When the application starts yielding a body and then raises an error, we should immediately close the connection to the client to indicate the error. --- gunicorn/workers/async.py | 7 +++++++ gunicorn/workers/sync.py | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/gunicorn/workers/async.py b/gunicorn/workers/async.py index c5e26552..c1da8649 100644 --- a/gunicorn/workers/async.py +++ b/gunicorn/workers/async.py @@ -104,6 +104,13 @@ class AsyncWorker(base.Worker): respiter.close() if resp.should_close(): raise StopIteration() + except Exception: + if resp.headers_sent: + # If the requests have already been sent, we should close the + # connection to indicate the error. + sock.shutdown(socket.SHUT_RDWR) + sock.close() + raise finally: try: self.cfg.post_request(self, req, environ, resp) diff --git a/gunicorn/workers/sync.py b/gunicorn/workers/sync.py index 37d256a4..562fc2b7 100644 --- a/gunicorn/workers/sync.py +++ b/gunicorn/workers/sync.py @@ -138,6 +138,11 @@ class SyncWorker(base.Worker): except socket.error: raise except Exception as e: + if resp.headers_sent: + # If the requests have already been sent, we should close the + # connection to indicate the error. + client.shutdown(socket.SHUT_RDWR) + client.close() # Only send back traceback in HTTP in debug mode. self.handle_error(req, client, addr, e) return