Skip to content

Commit fe0e22d

Browse files
committed
bugfix: tcpsock:setkeepalive(): we did not check NULL connection pointers properly. thanks Yang Yue for the report.
1 parent a05073d commit fe0e22d

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4335,7 +4335,8 @@ ngx_http_lua_socket_tcp_getreusedtimes(lua_State *L)
43354335
}
43364336

43374337

4338-
static int ngx_http_lua_socket_tcp_setkeepalive(lua_State *L)
4338+
static int
4339+
ngx_http_lua_socket_tcp_setkeepalive(lua_State *L)
43394340
{
43404341
ngx_http_lua_loc_conf_t *llcf;
43414342
ngx_http_lua_socket_tcp_upstream_t *u;
@@ -4380,12 +4381,18 @@ static int ngx_http_lua_socket_tcp_setkeepalive(lua_State *L)
43804381
u = lua_touserdata(L, -1);
43814382
lua_pop(L, 1);
43824383

4384+
if (u == NULL) {
4385+
lua_pushnil(L);
4386+
lua_pushliteral(L, "closed");
4387+
return 2;
4388+
}
4389+
43834390
/* stack: obj cache key */
43844391

43854392
pc = &u->peer;
43864393
c = pc->connection;
43874394

4388-
if (u == NULL || pc == NULL || u->read_closed || u->write_closed) {
4395+
if (c == NULL || u->read_closed || u->write_closed) {
43894396
lua_pushnil(L);
43904397
lua_pushliteral(L, "closed");
43914398
return 2;

t/058-tcp-socket.t

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use Test::Nginx::Socket::Lua;
55

66
repeat_each(2);
77

8-
plan tests => repeat_each() * 172;
8+
plan tests => repeat_each() * 175;
99

1010
our $HtmlDir = html_dir;
1111

@@ -3300,3 +3300,47 @@ close: 1 nil
33003300
--- no_error_log
33013301
[error]
33023302
3303+
3304+
3305+
=== TEST 55: kill a thread with a connecting socket
3306+
--- config
3307+
server_tokens off;
3308+
lua_socket_connect_timeout 1s;
3309+
resolver $TEST_NGINX_RESOLVER;
3310+
resolver_timeout 3s;
3311+
location /t {
3312+
content_by_lua '
3313+
local sock
3314+
3315+
local thr = ngx.thread.spawn(function ()
3316+
sock = ngx.socket.tcp()
3317+
local ok, err = sock:connect("agentzh.org", 12345)
3318+
if not ok then
3319+
ngx.say("failed to connect: ", err)
3320+
return
3321+
end
3322+
3323+
ngx.say("connected: ", ok)
3324+
end)
3325+
3326+
ngx.sleep(0.002)
3327+
ngx.thread.kill(thr)
3328+
ngx.sleep(0.001)
3329+
3330+
local ok, err = sock:setkeepalive()
3331+
if not ok then
3332+
ngx.say("failed to setkeepalive: ", err)
3333+
else
3334+
ngx.say("setkeepalive: ", ok)
3335+
end
3336+
';
3337+
}
3338+
3339+
--- request
3340+
GET /t
3341+
--- response_body
3342+
failed to setkeepalive: closed
3343+
--- error_log
3344+
lua tcp socket connect timeout: 100
3345+
--- timeout: 10
3346+

t/128-duplex-tcp-socket.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ receive: nil closed
469469
send: nil closed
470470
close: nil closed
471471
getreusedtimes: nil closed
472-
setkeepalive: nil socket busy connecting
472+
setkeepalive: nil closed
473473
connect: nil socket busy connecting
474474
connect: nil some2.agentzh.org could not be resolved (110: Operation timed out)
475475
close: nil closed

0 commit comments

Comments
 (0)