diff --git a/gunicorn/util.py b/gunicorn/util.py index 147b9a19..311c87be 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -60,7 +60,17 @@ def write(sock, data): break raise i += 1 - + +def write_nonblock(sock, data): + timeout = sock.gettimeout() + if timeout != "0.0": + sock.setblockin(0) + ret = write(sock, data) + sock.setblocking(1) + return ret + else: + return write(sock, data) + def writelines(sock, lines): for line in list(lines): write(sock, line) diff --git a/gunicorn/worker.py b/gunicorn/worker.py index 9ac365aa..873e6af8 100644 --- a/gunicorn/worker.py +++ b/gunicorn/worker.py @@ -133,7 +133,14 @@ class Worker(object): return http.HttpResponse(client, response, req).send() except Exception, e: - self.log.exception("Error processing request. [%s]" % str(e)) + self.log.exception("Error processing request. [%s]" % str(e)) + + # try to send a response even if something happend + try: + write_nonblock(sock, + "HTTP/1.0 500 Internal Server Error\r\n\r\n") + except: + pass finally: util.close(client)