Skip to content

Commit 11a13f0

Browse files
committed
bugfix: cosocket API could not be used before ngx.location.capture and its friends for fast network access: tcpsock:send() method did not reset u->waiting properly. thanks 欧远宁.
1 parent 89af745 commit 11a13f0

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

src/ngx_http_lua_socket.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
464464

465465
ctx->data = u;
466466
ctx->socket_busy = 1;
467+
468+
dd("setting socket_ready to 0");
469+
467470
ctx->socket_ready = 0;
468471

469472
u->waiting = 1;
@@ -502,6 +505,8 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
502505

503506
L = lctx->cc;
504507

508+
dd("setting socket_ready to 1");
509+
505510
lctx->socket_busy = 0;
506511
lctx->socket_ready = 1;
507512

@@ -746,6 +751,8 @@ ngx_http_lua_socket_resolve_retval_handler(ngx_http_request_t *r,
746751

747752
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
748753

754+
dd("setting socket_ready to 1");
755+
749756
ctx->socket_busy = 0;
750757
ctx->socket_ready = 1;
751758

@@ -769,7 +776,10 @@ ngx_http_lua_socket_resolve_retval_handler(ngx_http_request_t *r,
769776

770777
u->prepare_retvals = ngx_http_lua_socket_tcp_connect_retval_handler;
771778

779+
dd("setting socket_ready to 0");
780+
772781
ctx->socket_busy = 1;
782+
773783
ctx->socket_ready = 0;
774784

775785
if (ctx->entered_content_phase) {
@@ -1005,6 +1015,8 @@ ngx_http_lua_socket_tcp_receive(lua_State *L)
10051015
ctx->data = u;
10061016
u->prepare_retvals = ngx_http_lua_socket_tcp_receive_retval_handler;
10071017

1018+
dd("setting socket_ready to 0");
1019+
10081020
ctx->socket_busy = 1;
10091021
ctx->socket_ready = 0;
10101022

@@ -1465,6 +1477,10 @@ ngx_http_lua_socket_tcp_send(lua_State *L)
14651477
u->writer.pool = r->pool;
14661478
}
14671479

1480+
#if 1
1481+
u->waiting = 0;
1482+
#endif
1483+
14681484
rc = ngx_http_lua_socket_send(r, u);
14691485

14701486
dd("socket send returned %d", (int) rc);
@@ -1490,6 +1506,8 @@ ngx_http_lua_socket_tcp_send(lua_State *L)
14901506
ctx->data = u;
14911507
u->prepare_retvals = ngx_http_lua_socket_tcp_send_retval_handler;
14921508

1509+
dd("setting socket_ready to 0");
1510+
14931511
ctx->socket_busy = 1;
14941512
ctx->socket_ready = 0;
14951513

@@ -1798,6 +1816,8 @@ ngx_http_lua_socket_handle_success(ngx_http_request_t *r,
17981816

17991817
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
18001818

1819+
dd("setting socket_ready to 1");
1820+
18011821
ctx->socket_busy = 0;
18021822
ctx->socket_ready = 1;
18031823

@@ -1826,6 +1846,8 @@ ngx_http_lua_socket_handle_error(ngx_http_request_t *r,
18261846

18271847
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
18281848

1849+
dd("setting socket_ready to 1");
1850+
18291851
ctx->socket_busy = 0;
18301852
ctx->socket_ready = 1;
18311853

@@ -1885,6 +1907,8 @@ ngx_http_lua_socket_connected_handler(ngx_http_request_t *r,
18851907

18861908
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
18871909

1910+
dd("setting socket_ready to 1");
1911+
18881912
ctx->socket_busy = 0;
18891913
ctx->socket_ready = 1;
18901914

@@ -2221,6 +2245,8 @@ ngx_http_lua_socket_receiveuntil_iterator(lua_State *L)
22212245
ctx->data = u;
22222246
u->prepare_retvals = ngx_http_lua_socket_tcp_receive_retval_handler;
22232247

2248+
dd("setting socket_ready to 0");
2249+
22242250
ctx->socket_busy = 1;
22252251
ctx->socket_ready = 0;
22262252

src/ngx_http_lua_util.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,8 @@ ngx_http_lua_wev_handler(ngx_http_request_t *r)
10671067

10681068
dd("resuming socket api");
10691069

1070+
dd("setting socket_ready to 0");
1071+
10701072
ctx->socket_ready = 0;
10711073

10721074
u = ctx->data;
@@ -1120,6 +1122,8 @@ ngx_http_lua_wev_handler(ngx_http_request_t *r)
11201122

11211123
nret = ctx->nsubreqs;
11221124

1125+
dd("location capture nret: %d", (int) nret);
1126+
11231127
goto run;
11241128
}
11251129

t/058-tcp-socket.t

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use Test::Nginx::Socket;
55

66
repeat_each(2);
77

8-
plan tests => repeat_each() * (blocks() * 2 + 3);
8+
plan tests => repeat_each() * (blocks() * 2 + 4);
99

1010
our $HtmlDir = html_dir;
1111

1212
$ENV{TEST_NGINX_CLIENT_PORT} ||= server_port();
1313
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
1414
$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';
1515

16-
no_long_string();
16+
#no_long_string();
1717
no_diff();
1818
run_tests();
1919

@@ -1681,3 +1681,70 @@ GET /t
16811681
--- error_log
16821682
bad argument #1 to 'send' (bad data type userdata found)
16831683
1684+
1685+
1686+
=== TEST 29: cosocket before location capture (tcpsock:send did not clear u->waiting)
1687+
--- config
1688+
server_tokens off;
1689+
location /t {
1690+
#set $port 5000;
1691+
set $port $TEST_NGINX_MEMCACHED_PORT;
1692+
1693+
content_by_lua '
1694+
local sock = ngx.socket.tcp()
1695+
local port = ngx.var.port
1696+
1697+
local ok, err = sock:connect("127.0.0.1", port)
1698+
if not ok then
1699+
ngx.say("failed to connect: ", err)
1700+
return
1701+
end
1702+
1703+
ngx.say("connected: ", ok)
1704+
1705+
local req = "flush_all\\r\\n"
1706+
1707+
local bytes, err = sock:send(req)
1708+
if not bytes then
1709+
ngx.say("failed to send request: ", err)
1710+
return
1711+
end
1712+
ngx.say("request sent: ", bytes)
1713+
1714+
local line, err, part = sock:receive()
1715+
if line then
1716+
ngx.say("received: ", line)
1717+
1718+
else
1719+
ngx.say("failed to receive a line: ", err, " [", part, "]")
1720+
end
1721+
1722+
ok, err = sock:close()
1723+
ngx.say("close: ", ok, " ", err)
1724+
1725+
local resp = ngx.location.capture("/memc")
1726+
if type(resp) ~= "table" then
1727+
ngx.say("bad resp: type ", type(resp), ": ", resp)
1728+
return
1729+
end
1730+
1731+
ngx.print("subrequest: ", resp.status, ", ", resp.body)
1732+
';
1733+
}
1734+
1735+
location /memc {
1736+
set $memc_cmd flush_all;
1737+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
1738+
}
1739+
--- request
1740+
GET /t
1741+
--- response_body eval
1742+
"connected: 1
1743+
request sent: 11
1744+
received: OK
1745+
close: 1 nil
1746+
subrequest: 200, OK\r
1747+
"
1748+
--- no_error_log
1749+
[error]
1750+

0 commit comments

Comments
 (0)