# Buggy code
def handle_request(self, req, conn):
    environ = {}
    resp = None
    try:
        self.cfg.pre_request(self, req)
        request_start = datetime.now()
        resp, environ = wsgi.create(
            req, conn.sock, conn.addr, conn.listener.getsockname(), self.cfg
        )
        environ["wsgi.multithread"] = True
        self.nr += 1
        if self.alive and self.nr >= self.max_requests:
            self.log.info("Autorestarting worker after current request.")
            resp.force_close()
            self.alive = False

        if not self.cfg.keepalive:
            resp.force_close()
        elif len(self._keep) >= self.max_keepalived:
            resp.force_close()

        respiter = self.wsgi(environ, resp.start_response)
        try:
            if isinstance(respiter, environ["wsgi.file_wrapper"]):
                resp.write_file(respiter)
            else:
                for item in respiter:
                    resp.write(item)

            resp.close()
            request_time = datetime.now() - request_start
            self.log.access(resp, req, environ, request_time)
        finally:
            if hasattr(respiter, "close"):
                respiter.close()

        if resp.should_close():
            self.log.debug("Closing connection.")
            return False
    except socket.error:
        exc_info = sys.exc_info()
        # pass to next try-except level
        six.reraise(exc_info[0], exc_info[1], exc_info[2])
    except Exception:
        if resp and resp.headers_sent:
            # If the requests have already been sent, we should close the
            # connection to indicate the error.
            self.log.exception("Error handling request")
            try:
                conn.sock.shutdown(socket.SHUT_RDWR)
                conn.sock.close()
            except socket.error:
                pass
            raise StopIteration()
        raise
    finally:
        try:
            self.cfg.post_request(self, req, environ, resp)
        except Exception:
            self.log.exception("Exception in post_request hook")

    return True


# Traceback (BrokenPipeError)
[2014-11-17 04:18:49 -0500] [13520] [ERROR] Error handling request
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/gunicorn/workers/async.py", line 108, in handle_request
resp.write(item)
File "/usr/local/lib/python3.4/site-packages/gunicorn/http/wsgi.py", line 344, in write
util.write(self.sock, arg, self.chunked)
File "/usr/local/lib/python3.4/site-packages/gunicorn/util.py", line 301, in write
sock.sendall(data)
File "/usr/local/lib/python3.4/site-packages/eventlet/greenio.py", line 375, in sendall
tail = self.send(data, flags)
File "/usr/local/lib/python3.4/site-packages/eventlet/greenio.py", line 358, in send
total_sent += fd.send(data[total_sent:], flags)
File "/usr/local/lib/python3.4/site-packages/eventlet/greenio.py", line 351, in send
return fd.send(data, flags)
BrokenPipeError: [Errno 32] Broken pipe

# Fixed code
def handle_request(self, req, conn):
    environ = {}
    resp = None
    try:
        self.cfg.pre_request(self, req)
        request_start = datetime.now()
        resp, environ = wsgi.create(
            req, conn.sock, conn.addr, conn.listener.getsockname(), self.cfg
        )
        environ["wsgi.multithread"] = True
        self.nr += 1
        if self.alive and self.nr >= self.max_requests:
            self.log.info("Autorestarting worker after current request.")
            resp.force_close()
            self.alive = False

        if not self.cfg.keepalive:
            resp.force_close()
        elif len(self._keep) >= self.max_keepalived:
            resp.force_close()

        respiter = self.wsgi(environ, resp.start_response)
        try:
            if isinstance(respiter, environ["wsgi.file_wrapper"]):
                resp.write_file(respiter)
            else:
                for item in respiter:
                    resp.write(item)

            resp.close()
            request_time = datetime.now() - request_start
            self.log.access(resp, req, environ, request_time)
        finally:
            if hasattr(respiter, "close"):
                respiter.close()

        if resp.should_close():
            self.log.debug("Closing connection.")
            return False
    except EnvironmentError:
        exc_info = sys.exc_info()
        # pass to next try-except level
        six.reraise(exc_info[0], exc_info[1], exc_info[2])
    except Exception:
        if resp and resp.headers_sent:
            # If the requests have already been sent, we should close the
            # connection to indicate the error.
            self.log.exception("Error handling request")
            try:
                conn.sock.shutdown(socket.SHUT_RDWR)
                conn.sock.close()
            except EnvironmentError:
                pass
            raise StopIteration()
        raise
    finally:
        try:
            self.cfg.post_request(self, req, environ, resp)
        except Exception:
            self.log.exception("Exception in post_request hook")

    return True