ngx_http_discard_request_body may make keepalive connection hang in CLOSE_WAIT

Sergey Kandaurov pluknet at nginx.com
Fri Jan 10 18:00:21 UTC 2020


> On 7 Jan 2020, at 19:12, ak638 <nginx-forum at forum.nginx.org> wrote:
> 
> Hi,
> 
> I wonder if it's a bug. It confused me.
> recv in ngx_http_discard_request_body return 0, but ignored(suppose to close
> conntection soon). So the connection will stay in keepalive timer untill
> timeout, while client already closed and server stay in CLOSE_WAIT.

Hello.

This is a known issue.  It would be nice if
you could try and report back if this patch helped you.

# HG changeset patch
# User Sergey Kandaurov <pluknet at nginx.com>
# Date 1534236841 -10800
#      Tue Aug 14 11:54:01 2018 +0300
# Node ID b71df78c7dd02f0adf817a5af1931e8e4e9365d0
# Parent  70c6b08973a02551612da4a4273757dc77c70ae2
Cancel keepalive and lingering close on EOF better.

Unlike in 75e908236701, which added the logic to ngx_http_finalize_request(),
this change moves it to a more generic routine ngx_http_finalize_connection()
to cover cases when a request is finalized with NGX_DONE.

In particular, this fixes unwanted connection transition into the keepalive
state after receiving EOF while discarding request body.

diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -2497,11 +2497,6 @@ ngx_http_finalize_request(ngx_http_reque
        ngx_del_timer(c->write);
    }

-    if (c->read->eof) {
-        ngx_http_close_request(r, 0);
-        return;
-    }
-
    ngx_http_finalize_connection(r);
}

@@ -2600,6 +2595,11 @@ ngx_http_finalize_connection(ngx_http_re

    r = r->main;

+    if (r->connection->read->eof) {
+        ngx_http_close_request(r, 0);
+        return;
+    }
+
    if (r->reading_body) {
        r->keepalive = 0;
        r->lingering_close = 1;


-- 
Sergey Kandaurov



More information about the nginx mailing list