Skip to content

Commit

Permalink
Merge pull request #1031 from jhekkanen/1030-fix
Browse files Browse the repository at this point in the history
Fix self.nr usage in ThreadedWorker so that auto restart works as expected
  • Loading branch information
benoitc committed May 20, 2015
2 parents cc7459d + 826ef2d commit 3a2c437
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions gunicorn/workers/gthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __init__(self, *args, **kwargs):
self._lock = None
self.futures = deque()
self._keep = deque()
self.nr_conns = 0

@classmethod
def check_config(cls, cfg, log):
Expand Down Expand Up @@ -131,7 +132,7 @@ def accept(self, listener):
client, addr = listener.accept()
# initialize the connection object
conn = TConn(self.cfg, listener, client, addr)
self.nr += 1
self.nr_conns += 1
# enqueue the job
self.enqueue_req(conn)
except socket.error as e:
Expand Down Expand Up @@ -170,7 +171,7 @@ def murder_keepalived(self):
self._keep.appendleft(conn)
break
else:
self.nr -= 1
self.nr_conns -= 1
# remove the socket from the poller
with self._lock:
try:
Expand Down Expand Up @@ -202,7 +203,7 @@ def run(self):
self.notify()

# can we accept more connections?
if self.nr < self.worker_connections:
if self.nr_conns < self.worker_connections:
# wait for an event
events = self.poller.select(0.02)
for key, mask in events:
Expand Down Expand Up @@ -253,12 +254,12 @@ def finish_request(self, fs):
self.poller.register(conn.sock, selectors.EVENT_READ,
partial(self.reuse_connection, conn))
else:
self.nr -= 1
self.nr_conns -= 1
conn.close()
except:
# an exception happened, make sure to close the
# socket.
self.nr -= 1
self.nr_conns -= 1
fs.conn.close()

def handle(self, conn):
Expand Down Expand Up @@ -308,7 +309,7 @@ def handle_request(self, req, conn):
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()
Expand Down

0 comments on commit 3a2c437

Please sign in to comment.