[PATCH] HTTP: trigger lingering close when keepalive connection will be closed

Miao Wang shankerwangmiao at gmail.com
Wed Jan 18 15:28:52 UTC 2023


# HG changeset patch
# User Miao Wang <shankerwangmiao at gmail.com>
# Date 1674055068 -28800
#      Wed Jan 18 23:17:48 2023 +0800
# Node ID 73aa64bd29f3dec9e43e97560d6b5a07cdf40063
# Parent  07b0bee87f32be91a33210bc06973e07c4c1dac9
HTTP: trigger lingering close when keepalive connection will be closed

When finalizing a request, if the request is not keepalive but
its connection has served more than one request, then the connection
has been a keepalive connection previously and this connection will
be closed after this response. In this condition, it is likely that
there are pipelined requests following this request, which we should
ignore. As a result, lingering close is necessary in this case.

Without this patch, nginx (with its default configuration) will send
out TCP RST when there are more pipelined requests. The symptom is
obvious when nginx is serving a debian repository and apt is
downloading massive of packages. See [1]. It becomes more obvious
when `keepalive_requests` is lower or nginx is under a relative
higher load, and it disappears when specifying
`lingering_close always`.

[1]: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=973861#10

diff -r 07b0bee87f32 -r 73aa64bd29f3 src/http/ngx_http_request.c
--- a/src/http/ngx_http_request.c	Wed Dec 21 14:53:27 2022 +0300
+++ b/src/http/ngx_http_request.c	Wed Jan 18 23:17:48 2023 +0800
@@ -2749,6 +2749,10 @@
         return;
     }
 
+    if (!r->keepalive && r->connection->requests > 1) {
+        r->lingering_close = 1;
+    }
+
     if (clcf->lingering_close == NGX_HTTP_LINGERING_ALWAYS
         || (clcf->lingering_close == NGX_HTTP_LINGERING_ON
             && (r->lingering_close


More information about the nginx-devel mailing list