File tree Expand file tree Collapse file tree 3 files changed +55
-4
lines changed Expand file tree Collapse file tree 3 files changed +55
-4
lines changed Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ use Test::Nginx::Socket::Lua;
55
66repeat_each(2);
77
8- plan tests => repeat_each() * 172 ;
8+ plan tests => repeat_each() * 175 ;
99
1010our $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+
Original file line number Diff line number Diff line change @@ -469,7 +469,7 @@ receive: nil closed
469469send: nil closed
470470close : nil closed
471471getreusedtimes: nil closed
472- setkeepalive: nil socket busy connecting
472+ setkeepalive: nil closed
473473connect : nil socket busy connecting
474474connect : nil some2. agentzh. org could not be resolved (110 : Operation timed out)
475475close : nil closed
You can’t perform that action at this time.
0 commit comments