From 5f5b67da962e1d14031639c8bd7c1872cf82d7b4 Mon Sep 17 00:00:00 2001 From: benoitc Date: Mon, 8 Mar 2010 16:57:32 +0100 Subject: [PATCH] fix possible race condition when socket.error isn't raised but remote connection was closed. --- gunicorn/http/tee.py | 12 ++++++++++++ gunicorn/worker.py | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/gunicorn/http/tee.py b/gunicorn/http/tee.py index 8183adc8..f74eedbd 100644 --- a/gunicorn/http/tee.py +++ b/gunicorn/http/tee.py @@ -19,6 +19,9 @@ import tempfile from gunicorn import util +class UnexpectedEOF(object): + """ exception raised when remote closed the connection """ + class TeeInput(object): CHUNK_SIZE = util.CHUNK_SIZE @@ -163,8 +166,17 @@ class TeeInput(object): if self.parser.body_eof(): break + + if not self._is_socket: + if self.parser.is_chunked: + data = buf2.getvalue() + if data.find("\r\n") >= 0: + continue + raise UnexpectedEOF("remote closed the connection") data = self._sock.recv(length) + if not data: + self._is_socket = False buf2.write(data) self._finalize() diff --git a/gunicorn/worker.py b/gunicorn/worker.py index 346ffc92..503d6244 100644 --- a/gunicorn/worker.py +++ b/gunicorn/worker.py @@ -16,7 +16,7 @@ import traceback from gunicorn import http from gunicorn import util - +from gunicorn.http.tee import UnexpectedEOF class Worker(object): @@ -160,6 +160,8 @@ class Worker(object): self.log.exception("Error processing request.") else: self.log.warn("Ignoring EPIPE") + except UnexpectedShutdown: + self.log.exception("remote closed the connection unexpectedly.") except Exception, e: self.log.exception("Error processing request.") try: