[PATCH] Be consistent with keepalive during graceful shutdown

Yuchen Wu yuchen at cloudflare.com
Thu Jan 17 19:28:54 UTC 2019


# HG changeset patch
# User Yuchen Wu <yuchen at cloudflare.com>
# Date 1547749157 28800
#      Thu Jan 17 10:19:17 2019 -0800
# Node ID 8a6290c41b33c664d52d7a472400381e71ecf171
# Parent  8acaa1161783347106dcaea574837e441e13b540
Be consistent with keepalive during graceful shutdown

Before, when nginx sends the Connection: Keep-Alive header and then
nginx decides to shutdown, it will close the connection. Downstream
may already reuse the connection for another request in the meanwhile.

This change allows one extra use of such a connection to make sure it
is closed cleanly.

diff -r 8acaa1161783 -r 8a6290c41b33 src/http/ngx_http_header_filter_module.c
--- a/src/http/ngx_http_header_filter_module.c	Thu Dec 27 19:37:34 2018 +0300
+++ b/src/http/ngx_http_header_filter_module.c	Thu Jan 17 10:19:17 2019 -0800
@@ -187,6 +187,10 @@
         r->header_only = 1;
     }
 
+    if (ngx_terminate || ngx_exiting) {
+        r->keepalive = 0;
+    }
+
     if (r->headers_out.last_modified_time != -1) {
         if (r->headers_out.status != NGX_HTTP_OK
             && r->headers_out.status != NGX_HTTP_PARTIAL_CONTENT
diff -r 8acaa1161783 -r 8a6290c41b33 src/http/ngx_http_request.c
--- a/src/http/ngx_http_request.c	Thu Dec 27 19:37:34 2018 +0300
+++ b/src/http/ngx_http_request.c	Thu Jan 17 10:19:17 2019 -0800
@@ -2611,9 +2611,7 @@
         r->lingering_close = 1;
     }
 
-    if (!ngx_terminate
-         && !ngx_exiting
-         && r->keepalive
+    if (r->keepalive
          && clcf->keepalive_timeout > 0)
     {
         ngx_http_set_keepalive(r);


More information about the nginx-devel mailing list