Skip to content

Commit

Permalink
Fix eventlet graceful timeout handling
Browse files Browse the repository at this point in the history
The `StopServer` exception can lead to a handler blocked waiting for
an available greenthread to never be processed.
This change ensures we attempt to handle any accepted socket connection
within the graceful timeout period.
  • Loading branch information
Michael Bell committed Mar 20, 2018
1 parent d1f5268 commit 77aa82e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gunicorn/workers/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def handle(self, listener, client, addr):
try:
parser = http.RequestParser(self.cfg, client)
try:
listener_name = listener.getsockname()
listener_name = client.getsockname()
if not self.cfg.keepalive:
req = six.next(parser)
self.handle_request(listener_name, req, client, addr)
Expand Down
14 changes: 10 additions & 4 deletions gunicorn/workers/geventlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,18 @@ def _eventlet_serve(sock, handle, concurrency):
pool = eventlet.greenpool.GreenPool(concurrency)
server_gt = eventlet.greenthread.getcurrent()

def _eventlet_handle(conn, addr):
gt = pool.spawn(handle, conn, addr)
gt.link(_eventlet_stop, server_gt, conn)

while True:
try:
conn, addr = sock.accept()
gt = pool.spawn(handle, conn, addr)
gt.link(_eventlet_stop, server_gt, conn)
conn, addr, gt = None, None, None
_eventlet_handle(conn, addr)
conn, addr = None, None
except eventlet.StopServe:
if conn and addr:
_eventlet_handle(conn, addr)
sock.close()
pool.waitall()
return
Expand Down Expand Up @@ -123,7 +128,8 @@ def run(self):
for sock in self.sockets:
gsock = GreenSocket(sock)
gsock.setblocking(1)
hfun = partial(self.handle, gsock)
listener_name = gsock.getsockname()
hfun = partial(self.handle, listener_name)
acceptor = eventlet.spawn(_eventlet_serve, gsock, hfun,
self.worker_connections)

Expand Down

0 comments on commit 77aa82e

Please sign in to comment.