Skip to content

Commit

Permalink
Avoid 'Connection refused' when using multithreaded gthread worker wi…
Browse files Browse the repository at this point in the history
…th keep-alive

Fixes benoitc#1698
  • Loading branch information
dzialak committed Feb 12, 2018
1 parent d32f4ca commit 239a8bf
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions gunicorn/workers/gthread.py
Expand Up @@ -55,6 +55,7 @@ def __init__(self, cfg, sock, client, server):

# set the socket to non blocking
self.sock.setblocking(False)
self.is_active = True

def init(self):
self.sock.setblocking(True)
Expand Down Expand Up @@ -148,8 +149,9 @@ def reuse_connection(self, conn, client):
try:
self._keep.remove(conn)
except ValueError:
# race condition
return
# race condition (other thread executed murder_keepalived)
pass
conn.is_active = True

# submit the connection to a worker
self.enqueue_req(conn)
Expand All @@ -168,7 +170,8 @@ def murder_keepalived(self):
if delta > 0:
# add the connection back to the queue
with self._lock:
self._keep.appendleft(conn)
if not conn.is_active:
self._keep.appendleft(conn)
break
else:
self.nr_conns -= 1
Expand Down Expand Up @@ -259,6 +262,7 @@ def finish_request(self, fs):
conn.set_timeout()
with self._lock:
self._keep.append(conn)
conn.is_active = False

# add the socket to the event loop
self.poller.register(conn.sock, selectors.EVENT_READ,
Expand Down

0 comments on commit 239a8bf

Please sign in to comment.