ngx_http_discard_request_body may make keepalive connection hang in CLOSE_WAIT

classic Classic list List threaded Threaded
2 messages Options
xrd
Reply | Threaded
Open this post in threaded view
|

ngx_http_discard_request_body may make keepalive connection hang in CLOSE_WAIT

xrd
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.

nginx version: nginx/1.17.6
built by gcc 4.4.6 20110731 (Red Hat 4.4.6-4) (GCC)
built with OpenSSL 1.1.1c  28 May 2019
TLS SNI support enabled
configure arguments: --with-debug --prefix=/home/qspace/nginx --user=qspace
--group=users --with-http_ssl_module --with-http_v2_module
--with-http_gzip_static_module --with-http_stub_status_module
--with-openssl=../openssl-1.1.1c --with-pcre=../pcre-4.3 --with-stream

nginx.conf:
keepalive_timeout  65;
keepalive_requests 2048;
limit_conn_zone $binary_remote_addr zone=connperip:10m;
limit_conn connperip 1; (make it redirect to /503.html)

request:
body not empty, i.e, content_len != 0
e.g. curl "http://127.0.0.1" -F "file=@some_file" (close conntection after
curl exit)

ngx_http_core_generic_phase -> ngx_http_finalize_request->
ngx_http_special_response_handler -> ngx_http_discard_request_body

------------------------------------------------------------------------------------------------------------------
nginx debug log:
125812:2020/01/07 16:04:18 [debug] 12255#0: *102094 **http run request:
"/50x.html?"**
125815:2020/01/07 16:04:18 [debug] 12255#0: *102094 **http read discarded
body**
125820:2020/01/07 16:04:18 [debug] 12255#0: *102094 **recv: eof:1,
avail:-1**
125827:2020/01/07 16:04:18 [debug] 12255#0: *102094 **recv: fd:14 0 of
4096**
125831:2020/01/07 16:04:18 [debug] 12255#0: *102094 http finalize request:
-4, "/50x.html?" a:1, c:1
125834:2020/01/07 16:04:18 [debug] 12255#0: *102094 **set http keepalive
handler**
125837:2020/01/07 16:04:18 [debug] 12255#0: *102094 http close request
125841:2020/01/07 16:04:18 [debug] 12255#0: *102094 http log handler
125862:2020/01/07 16:04:18 [debug] 12255#0: *102094 run cleanup:
00000000028D3F10
125867:2020/01/07 16:04:18 [debug] 12255#0: *102094 file cleanup: fd:15
125872:2020/01/07 16:04:18 [debug] 12255#0: *102094 free: 00000000028D3190,
unused: 48
125877:2020/01/07 16:04:18 [debug] 12255#0: *102094 free: 00000000028D41A0,
unused: 2655
125881:2020/01/07 16:04:18 [debug] 12255#0: *102094 free: 0000000002883620
125883:2020/01/07 16:04:18 [debug] 12255#0: *102094 hc free:
0000000000000000
125887:2020/01/07 16:04:18 [debug] 12255#0: *102094 hc busy:
0000000000000000 0
125890:2020/01/07 16:04:18 [debug] 12255#0: *102094 reusable connection: 1
125896:2020/01/07 16:04:18 [debug] 12255#0: *102094 event timer del: 14:
8799496244
125900:2020/01/07 16:04:18 [debug] 12255#0: *102094 event timer add: 14:
65000:8799556448
**...... after 65 second (keepalive_timeout)**
3928328:2020/01/07 16:05:23 [debug] 12255#0: *102094 event timer del: 14:
8799556448
3928329:2020/01/07 16:05:23 [debug] 12255#0: *102094 http keepalive handler
3928330:2020/01/07 16:05:23 [debug] 12255#0: *102094 **close http
connection: 14**
3928331:2020/01/07 16:05:23 [debug] 12255#0: *102094 reusable connection: 0
3928332:2020/01/07 16:05:23 [debug] 12255#0: *102094 free: 0000000000000000
3928333:2020/01/07 16:05:23 [debug] 12255#0: *102094 free: 00000000028D1250,
unused: 136


Thanks!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286665,286665#msg-286665

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: ngx_http_discard_request_body may make keepalive connection hang in CLOSE_WAIT

Sergey Kandaurov

> On 7 Jan 2020, at 19:12, ak638 <[hidden email]> 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 <[hidden email]>
# 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

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx