diff --git a/gunicorn/workers/gthread.py b/gunicorn/workers/gthread.py index 5f918e2e74..d5a7c2d603 100644 --- a/gunicorn/workers/gthread.py +++ b/gunicorn/workers/gthread.py @@ -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) @@ -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) @@ -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 @@ -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,