Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3227: Wait for the keepalive period to end before stopping the acceptance of new requests #3236

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gunicorn/workers/base_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def handle(self, listener, client, addr):
parser = http.RequestParser(self.cfg, client, addr)
try:
listener_name = listener.getsockname()
if not self.cfg.keepalive:
# do not allow keepalive if the worker is about to be restarted
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is OK

if not self.cfg.keepalive or not self.alive:
req = next(parser)
self.handle_request(listener_name, req, client, addr)
else:
Expand Down
17 changes: 17 additions & 0 deletions gunicorn/workers/ggevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ def run(self):
self.notify()
gevent.sleep(1.0)

# Wait for pending keepalive connections to complete before stopping the acceptance of new requests
if self.cfg.keepalive:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i disagree there, we shouldn't care about waiting for keepalive connections when closing. CLose should be done as fast as possible.

self._wait_for_keepalive(servers)

try:
# Stop accepting requests
for server in servers:
Expand Down Expand Up @@ -117,6 +121,19 @@ def run(self):
except Exception:
pass

def _wait_for_keepalive(self, servers):
# Retrieve all active greenlets and repeatedly check, until the keepalive period ends,
# if any of those greenlets are still active. If none are active, exit the loop.

greenlets = {id(greenlet) for server in servers for greenlet in server.pool.greenlets}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this line is executed, self.alive is already False, which means that all keepalive connections are either closed or are being handled by the existing greenlets. No new keepalive connections will be created until the worker restarts.
Therefore, if the greenlets found in this line have finished, we do not need to wait any longer, as we are assured that there are no more active keepalive connections.

ts = time.monotonic()

while time.monotonic() - ts <= self.cfg.keepalive:
if not greenlets.intersection({id(greenlet) for server in servers for greenlet in server.pool.greenlets}):
break
self.notify()
gevent.sleep(1.0)

def handle(self, listener, client, addr):
# Connected socket timeout defaults to socket.getdefaulttimeout().
# This forces to blocking mode.
Expand Down