Skip to content

Commit f64ec8c

Browse files
doujiang24agentzh
authored andcommitted
bugfix: tcpsock:setkeepalive: worker processes might take too long to gracefully shut down when the keep alive connections take a long max idle time.
Now we avoid putting any new connections into the pool when nginx is already shutting down. Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent e94f2e5 commit f64ec8c

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/ngx_http_lua_socket_tcp.c

+10
Original file line numberDiff line numberDiff line change
@@ -4690,6 +4690,14 @@ ngx_http_lua_socket_tcp_setkeepalive(lua_State *L)
46904690
return 2;
46914691
}
46924692

4693+
if (ngx_terminate || ngx_exiting) {
4694+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
4695+
"lua tcp socket set keepalive while process exiting, "
4696+
"closing connection %p", c);
4697+
4698+
goto finalize;
4699+
}
4700+
46934701
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
46944702
"lua tcp socket set keepalive: saving connection %p", c);
46954703

@@ -4854,6 +4862,8 @@ ngx_http_lua_socket_tcp_setkeepalive(lua_State *L)
48544862
}
48554863
}
48564864

4865+
finalize:
4866+
48574867
#if 1
48584868
ngx_http_lua_socket_tcp_finalize(r, u);
48594869
#endif

t/157-socket-keepalive-hup.t

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
3+
our $SkipReason;
4+
5+
BEGIN {
6+
if ($ENV{TEST_NGINX_CHECK_LEAK}) {
7+
$SkipReason = "unavailable for the hup tests";
8+
9+
} else {
10+
$ENV{TEST_NGINX_USE_HUP} = 1;
11+
undef $ENV{TEST_NGINX_USE_STAP};
12+
}
13+
}
14+
15+
use Test::Nginx::Socket::Lua $SkipReason ? (skip_all => $SkipReason) : ();
16+
17+
repeat_each(2);
18+
19+
plan tests => repeat_each() * (blocks() * 8);
20+
21+
#no_diff();
22+
no_long_string();
23+
24+
worker_connections(1024);
25+
run_tests();
26+
27+
__DATA__
28+
29+
=== TEST 1: exiting
30+
--- config
31+
location /t {
32+
set $port $TEST_NGINX_SERVER_PORT;
33+
34+
content_by_lua_block {
35+
local f, err = io.open("t/servroot/logs/nginx.pid", "r")
36+
if not f then
37+
ngx.say("failed to open nginx.pid: ", err)
38+
return
39+
end
40+
41+
local pid = f:read()
42+
-- ngx.say("master pid: [", pid, "]")
43+
44+
f:close()
45+
46+
local i = 0
47+
local port = ngx.var.port
48+
49+
local function f(premature)
50+
print("timer prematurely expired: ", premature)
51+
52+
local sock = ngx.socket.tcp()
53+
54+
local ok, err = sock:connect("127.0.0.1", port)
55+
if not ok then
56+
print("failed to connect: ", err)
57+
return
58+
end
59+
60+
local ok, err = sock:setkeepalive()
61+
if not ok then
62+
print("failed to setkeepalive: ", err)
63+
return
64+
end
65+
66+
print("setkeepalive successfully")
67+
end
68+
local ok, err = ngx.timer.at(3, f)
69+
if not ok then
70+
ngx.say("failed to set timer: ", err)
71+
return
72+
end
73+
ngx.say("registered timer")
74+
os.execute("kill -HUP " .. pid)
75+
}
76+
}
77+
--- request
78+
GET /t
79+
80+
--- response_body
81+
registered timer
82+
83+
--- wait: 0.3
84+
--- no_error_log
85+
[error]
86+
[alert]
87+
[crit]
88+
--- error_log
89+
timer prematurely expired: true
90+
setkeepalive successfully
91+
lua tcp socket set keepalive while process exiting, closing connection

0 commit comments

Comments
 (0)