diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index 2bbf554c..6852ebd5 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -39,10 +39,9 @@ class Arbiter(object): self.timeout = 30 self.reexec_pid = 0 self.pid = os.getpid() + self.log = logging.getLogger(__name__) self.init_signals() self.listen(self.address) - self.log = logging.getLogger(__name__) - self.log.info("Booted Arbiter: %s" % os.getpid()) @@ -200,7 +199,7 @@ class Arbiter(object): def sleep(self): try: - ready = select.select([self.PIPE[0]], [], [], 1) + ready = select.select([self.PIPE[0]], [], [], 1.0) if not ready[0]: return while os.read(self.PIPE[0], 1): @@ -237,6 +236,7 @@ class Arbiter(object): diff = time.time() - os.fstat(worker.tmp.fileno()).st_ctime if diff <= self.timeout: continue + self.log.error("worker %s PID %s timeout killing." % (str(worker.id), pid)) self.kill_worker(pid, signal.SIGKILL) def reap_workers(self): @@ -270,7 +270,7 @@ class Arbiter(object): continue worker = Worker(i, self.pid, self.LISTENER, self.modname, - self.timeout / 2.0) + self.timeout) pid = os.fork() if pid != 0: self.WORKERS[pid] = worker @@ -283,6 +283,7 @@ class Arbiter(object): worker.run() sys.exit(0) except SystemExit: + raise except: self.log.exception("Exception in worker process.") @@ -304,7 +305,8 @@ class Arbiter(object): self.log.warning("Problem killing process: %s" % pid) except OSError, e: if e.errno == errno.ESRCH: - pass - finally: - worker.tmp.close() + try: + worker.tmp.close() + except: + pass diff --git a/gunicorn/http/response.py b/gunicorn/http/response.py index 0f7d3df0..6e256900 100644 --- a/gunicorn/http/response.py +++ b/gunicorn/http/response.py @@ -30,7 +30,7 @@ class HttpResponse(object): resp_head.append("Date: %s\r\n" % http_date()) # broken clients resp_head.append("Status: %s\r\n" % str(self.status)) - # always close the conenction + # always close the connection resp_head.append("Connection: close\r\n") for name, value in self.headers.items(): resp_head.append("%s: %s\r\n" % (name, value)) diff --git a/gunicorn/util.py b/gunicorn/util.py index d45683e0..a7f2e901 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -22,7 +22,6 @@ weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] monthname = [None, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] - def close_on_exec(fd): flags = fcntl.fcntl(fd, fcntl.F_GETFD) | fcntl.FD_CLOEXEC @@ -62,21 +61,9 @@ def write(sock, data): if e[0] in (errno.EWOULDBLOCK, errno.EAGAIN): break elif e[0] in (errno.EPIPE,): - if i == 0: - continue + break raise i += 1 - -def write_nonblock(sock, data): - while True: - try: - ret = select.select([], [sock.fileno()], [], 2.0) - if ret[1]: break - except socket.error, e: - if e[0] == errno.EINTR: - continue - raise - write(sock, data) def normalize_name(name): diff --git a/gunicorn/worker.py b/gunicorn/worker.py index 811e4649..10e8984d 100644 --- a/gunicorn/worker.py +++ b/gunicorn/worker.py @@ -28,7 +28,7 @@ class Worker(object): def __init__(self, workerid, ppid, socket, app, timeout): self.id = workerid self.ppid = ppid - self.timeout = timeout + self.timeout = timeout / 2.0 fd, tmpname = tempfile.mkstemp() self.tmp = os.fdopen(fd, "r+b") self.tmpname = tmpname @@ -53,7 +53,7 @@ class Worker(object): signal.signal(signal.SIGTERM, self.handle_exit) signal.signal(signal.SIGINT, self.handle_exit) signal.signal(signal.SIGUSR1, self.handle_quit) - + def handle_quit(self, sig, frame): self.alive = False @@ -80,9 +80,11 @@ class Worker(object): try: client, addr = self.socket.accept() + self.client = client + # handle connection self.handle(client, addr) - + # Update the fd mtime on each client completion # to signal that this worker process is alive. spinner = (spinner+1) % 2 @@ -118,6 +120,7 @@ class Worker(object): spinner = (spinner+1) % 2 self._fchmod(spinner) + def handle(self, client, addr): util.close_on_exec(client) @@ -126,7 +129,6 @@ class Worker(object): response = self.app(req.read(), req.start_response) http.HttpResponse(client, response, req).send() except Exception, e: - # TODO: try to send something if an error happend - self.log.exception("Error processing request. [%s]" % str(e)) - # try to send something if an error happend + self.log.exception("Error processing request. [%s]" % str(e)) util.close(client) + \ No newline at end of file