From 4ae2a05c37b332773997f90ba7542713b9bf8274 Mon Sep 17 00:00:00 2001 From: Randall Leeds Date: Mon, 20 Apr 2020 14:35:19 -0700 Subject: [PATCH] Disable keepalive during graceful shutdown Fix graceful shutdown behavior so that clients receive notice to close the connection. After #2304 and the follow-up in ebb41da, Joe Kemp noticed that, while the new behavior would successfully indicate a connect close after hitting the maximum request limit for a worker during graceful shutdown, the worker would not indicate a connection close if if it had not hit the maximum request limit. This change ensures that the worker exits gracefully when hitting the request limit and also indicates to clients that the connection should close once the shutdown begins. --- gunicorn/workers/base_async.py | 3 +-- gunicorn/workers/gthread.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/gunicorn/workers/base_async.py b/gunicorn/workers/base_async.py index 0aaaa4dd..6e7ae3f8 100644 --- a/gunicorn/workers/base_async.py +++ b/gunicorn/workers/base_async.py @@ -99,9 +99,8 @@ class AsyncWorker(base.Worker): if self.alive: self.log.info("Autorestarting worker after current request.") self.alive = False - resp.force_close() - if not self.cfg.keepalive: + if not self.alive or not self.cfg.keepalive: resp.force_close() respiter = self.wsgi(environ, resp.start_response) diff --git a/gunicorn/workers/gthread.py b/gunicorn/workers/gthread.py index c7f64b61..5cd3e8a5 100644 --- a/gunicorn/workers/gthread.py +++ b/gunicorn/workers/gthread.py @@ -313,7 +313,7 @@ class ThreadWorker(base.Worker): self.alive = False resp.force_close() - if not self.cfg.keepalive: + if not self.alive or not self.cfg.keepalive: resp.force_close() elif len(self._keep) >= self.max_keepalived: resp.force_close()