Skip to content

Commit 66294b9

Browse files
bugfix: ambiguous error message 'connection in dubious state' when connection is closed. (#2016)
1 parent 77540f7 commit 66294b9

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

src/ngx_http_lua_socket_tcp.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5188,7 +5188,12 @@ ngx_http_lua_socket_tcp_setkeepalive(lua_State *L)
51885188
pc = &u->peer;
51895189
c = pc->connection;
51905190

5191-
if (c == NULL || u->read_closed || u->write_closed) {
5191+
/* When the server closes the connection,
5192+
* epoll will return EPOLLRDHUP event and nginx will set pending_eof.
5193+
*/
5194+
if (c == NULL || u->read_closed || u->write_closed
5195+
|| c->read->eof || c->read->pending_eof)
5196+
{
51925197
lua_pushnil(L);
51935198
lua_pushliteral(L, "closed");
51945199
return 2;
@@ -5218,8 +5223,7 @@ ngx_http_lua_socket_tcp_setkeepalive(lua_State *L)
52185223
return 2;
52195224
}
52205225

5221-
if (c->read->eof
5222-
|| c->read->error
5226+
if (c->read->error
52235227
|| c->read->timedout
52245228
|| c->write->error
52255229
|| c->write->timedout)

t/058-tcp-socket.t

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua;
44

55
repeat_each(2);
66

7-
plan tests => repeat_each() * 231;
7+
plan tests => repeat_each() * (blocks() * 3 + 21);
88

99
our $HtmlDir = html_dir;
1010

@@ -4367,3 +4367,69 @@ connect failed: missing the port number
43674367
finish
43684368
--- no_error_log
43694369
[error]
4370+
4371+
4372+
4373+
=== TEST 73: reset the buffer pos when keepalive
4374+
--- config
4375+
server_tokens off;
4376+
location /t {
4377+
set $port $TEST_NGINX_SERVER_PORT;
4378+
4379+
content_by_lua_block {
4380+
for i = 1, 10
4381+
do
4382+
local sock = ngx.socket.tcp()
4383+
local port = ngx.var.port
4384+
local ok, err = sock:connect("127.0.0.1", port)
4385+
if not ok then
4386+
ngx.say("failed to connect: ", err)
4387+
return
4388+
end
4389+
4390+
local req = "GET /hi HTTP/1.1\r\nHost: localhost\r\n\r\n"
4391+
4392+
local bytes, err = sock:send(req)
4393+
if not bytes then
4394+
ngx.say("failed to send request: ", err)
4395+
return
4396+
end
4397+
4398+
local line, err, part = sock:receive()
4399+
if not line then
4400+
ngx.say("receive err: ", err)
4401+
return
4402+
end
4403+
4404+
data, err = sock:receiveany(4096)
4405+
if not data then
4406+
ngx.say("receiveany er: ", err)
4407+
return
4408+
end
4409+
4410+
ok, err = sock:setkeepalive(10000, 32)
4411+
if not ok then
4412+
ngx.say("reused times: ", i, ", setkeepalive err: ", err)
4413+
return
4414+
end
4415+
end
4416+
ngx.say("END")
4417+
}
4418+
}
4419+
4420+
location /hi {
4421+
keepalive_requests 3;
4422+
content_by_lua_block {
4423+
ngx.say("Hello")
4424+
}
4425+
4426+
more_clear_headers Date;
4427+
}
4428+
4429+
--- request
4430+
GET /t
4431+
--- response_body
4432+
reused times: 3, setkeepalive err: closed
4433+
--- no_error_log
4434+
[error]
4435+
--- skip_eval: 3: $ENV{TEST_NGINX_EVENT_TYPE} && $ENV{TEST_NGINX_EVENT_TYPE} ne 'epoll'

t/071-idle-socket.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ Transfer-Encoding: chunked\r
367367
Connection: close\r
368368
\r
369369
6\r
370-
failed to set keepalive: (?:unread data in buffer|connection in dubious state)
370+
failed to set keepalive: (?:unread data in buffer|closed|connection in dubious state)
371371
}
372372
--- no_error_log
373373
[error]

0 commit comments

Comments
 (0)