Skip to content

Commit

Permalink
Merge pull request #2918 from JorisOnGithub/bugfix/fix-gthread-deadlock
Browse files Browse the repository at this point in the history
gthread: only read sockets when they are readable (#2917)
  • Loading branch information
benoitc committed May 7, 2023
2 parents 1fd1c82 + 0ebb73a commit a423fca
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions gunicorn/workers/gthread.py
Expand Up @@ -40,12 +40,15 @@ def __init__(self, cfg, sock, client, server):

self.timeout = None
self.parser = None
self.initialized = False

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

def init(self):
self.initialized = True
self.sock.setblocking(True)

if self.parser is None:
# wrap the socket if needed
if self.cfg.is_ssl:
Expand Down Expand Up @@ -120,23 +123,28 @@ def accept(self, server, listener):
# initialize the connection object
conn = TConn(self.cfg, sock, client, server)
self.nr_conns += 1
# enqueue the job
self.enqueue_req(conn)

# wait until socket is readable
with self._lock:
self.poller.register(conn.sock, selectors.EVENT_READ,
partial(self.on_client_socket_readable, conn))
except EnvironmentError as e:
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
errno.EWOULDBLOCK):
raise

def reuse_connection(self, conn, client):
def on_client_socket_readable(self, conn, client):
with self._lock:
# unregister the client from the poller
self.poller.unregister(client)
# remove the connection from keepalive
try:
self._keep.remove(conn)
except ValueError:
# race condition
return

if conn.initialized:
# remove the connection from keepalive
try:
self._keep.remove(conn)
except ValueError:
# race condition
return

# submit the connection to a worker
self.enqueue_req(conn)
Expand Down Expand Up @@ -249,7 +257,7 @@ def finish_request(self, fs):

# add the socket to the event loop
self.poller.register(conn.sock, selectors.EVENT_READ,
partial(self.reuse_connection, conn))
partial(self.on_client_socket_readable, conn))
else:
self.nr_conns -= 1
conn.close()
Expand Down

0 comments on commit a423fca

Please sign in to comment.