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 6, 2018
1 parent d32f4ca commit 2339103
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions gunicorn/workers/gthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self, cfg, sock, client, server):

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

def init(self):
self.sock.setblocking(True)
Expand Down Expand Up @@ -149,7 +150,7 @@ def reuse_connection(self, conn, client):
self._keep.remove(conn)
except ValueError:
# race condition
return
conn.active = True

# submit the connection to a worker
self.enqueue_req(conn)
Expand All @@ -168,7 +169,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.active:
self._keep.appendleft(conn)
break
else:
self.nr_conns -= 1
Expand Down Expand Up @@ -259,6 +261,7 @@ def finish_request(self, fs):
conn.set_timeout()
with self._lock:
self._keep.append(conn)
conn.active = False

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

0 comments on commit 2339103

Please sign in to comment.