Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bugfix: cosocket API could not be used before ngx.location.capture an…

…d its friends for fast network access: tcpsock:send() method did not reset u->waiting properly. thanks 欧远宁.
  • Loading branch information...
commit 11a13f06940c0515e50dd144f4ea84b0948e10af 1 parent 89af745
Yichun Zhang agentzh authored
26 src/ngx_http_lua_socket.c
View
@@ -464,6 +464,9 @@ ngx_http_lua_socket_tcp_connect(lua_State *L)
ctx->data = u;
ctx->socket_busy = 1;
+
+ dd("setting socket_ready to 0");
+
ctx->socket_ready = 0;
u->waiting = 1;
@@ -502,6 +505,8 @@ ngx_http_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
L = lctx->cc;
+ dd("setting socket_ready to 1");
+
lctx->socket_busy = 0;
lctx->socket_ready = 1;
@@ -746,6 +751,8 @@ ngx_http_lua_socket_resolve_retval_handler(ngx_http_request_t *r,
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
+ dd("setting socket_ready to 1");
+
ctx->socket_busy = 0;
ctx->socket_ready = 1;
@@ -769,7 +776,10 @@ ngx_http_lua_socket_resolve_retval_handler(ngx_http_request_t *r,
u->prepare_retvals = ngx_http_lua_socket_tcp_connect_retval_handler;
+ dd("setting socket_ready to 0");
+
ctx->socket_busy = 1;
+
ctx->socket_ready = 0;
if (ctx->entered_content_phase) {
@@ -1005,6 +1015,8 @@ ngx_http_lua_socket_tcp_receive(lua_State *L)
ctx->data = u;
u->prepare_retvals = ngx_http_lua_socket_tcp_receive_retval_handler;
+ dd("setting socket_ready to 0");
+
ctx->socket_busy = 1;
ctx->socket_ready = 0;
@@ -1465,6 +1477,10 @@ ngx_http_lua_socket_tcp_send(lua_State *L)
u->writer.pool = r->pool;
}
+#if 1
+ u->waiting = 0;
+#endif
+
rc = ngx_http_lua_socket_send(r, u);
dd("socket send returned %d", (int) rc);
@@ -1490,6 +1506,8 @@ ngx_http_lua_socket_tcp_send(lua_State *L)
ctx->data = u;
u->prepare_retvals = ngx_http_lua_socket_tcp_send_retval_handler;
+ dd("setting socket_ready to 0");
+
ctx->socket_busy = 1;
ctx->socket_ready = 0;
@@ -1798,6 +1816,8 @@ ngx_http_lua_socket_handle_success(ngx_http_request_t *r,
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
+ dd("setting socket_ready to 1");
+
ctx->socket_busy = 0;
ctx->socket_ready = 1;
@@ -1826,6 +1846,8 @@ ngx_http_lua_socket_handle_error(ngx_http_request_t *r,
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
+ dd("setting socket_ready to 1");
+
ctx->socket_busy = 0;
ctx->socket_ready = 1;
@@ -1885,6 +1907,8 @@ ngx_http_lua_socket_connected_handler(ngx_http_request_t *r,
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
+ dd("setting socket_ready to 1");
+
ctx->socket_busy = 0;
ctx->socket_ready = 1;
@@ -2221,6 +2245,8 @@ ngx_http_lua_socket_receiveuntil_iterator(lua_State *L)
ctx->data = u;
u->prepare_retvals = ngx_http_lua_socket_tcp_receive_retval_handler;
+ dd("setting socket_ready to 0");
+
ctx->socket_busy = 1;
ctx->socket_ready = 0;
4 src/ngx_http_lua_util.c
View
@@ -1067,6 +1067,8 @@ ngx_http_lua_wev_handler(ngx_http_request_t *r)
dd("resuming socket api");
+ dd("setting socket_ready to 0");
+
ctx->socket_ready = 0;
u = ctx->data;
@@ -1120,6 +1122,8 @@ ngx_http_lua_wev_handler(ngx_http_request_t *r)
nret = ctx->nsubreqs;
+ dd("location capture nret: %d", (int) nret);
+
goto run;
}
71 t/058-tcp-socket.t
View
@@ -5,7 +5,7 @@ use Test::Nginx::Socket;
repeat_each(2);
-plan tests => repeat_each() * (blocks() * 2 + 3);
+plan tests => repeat_each() * (blocks() * 2 + 4);
our $HtmlDir = html_dir;
@@ -13,7 +13,7 @@ $ENV{TEST_NGINX_CLIENT_PORT} ||= server_port();
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';
-no_long_string();
+#no_long_string();
no_diff();
run_tests();
@@ -1681,3 +1681,70 @@ GET /t
--- error_log
bad argument #1 to 'send' (bad data type userdata found)
+
+
+=== TEST 29: cosocket before location capture (tcpsock:send did not clear u->waiting)
+--- config
+ server_tokens off;
+ location /t {
+ #set $port 5000;
+ set $port $TEST_NGINX_MEMCACHED_PORT;
+
+ content_by_lua '
+ local sock = ngx.socket.tcp()
+ local port = ngx.var.port
+
+ local ok, err = sock:connect("127.0.0.1", port)
+ if not ok then
+ ngx.say("failed to connect: ", err)
+ return
+ end
+
+ ngx.say("connected: ", ok)
+
+ local req = "flush_all\\r\\n"
+
+ local bytes, err = sock:send(req)
+ if not bytes then
+ ngx.say("failed to send request: ", err)
+ return
+ end
+ ngx.say("request sent: ", bytes)
+
+ local line, err, part = sock:receive()
+ if line then
+ ngx.say("received: ", line)
+
+ else
+ ngx.say("failed to receive a line: ", err, " [", part, "]")
+ end
+
+ ok, err = sock:close()
+ ngx.say("close: ", ok, " ", err)
+
+ local resp = ngx.location.capture("/memc")
+ if type(resp) ~= "table" then
+ ngx.say("bad resp: type ", type(resp), ": ", resp)
+ return
+ end
+
+ ngx.print("subrequest: ", resp.status, ", ", resp.body)
+ ';
+ }
+
+ location /memc {
+ set $memc_cmd flush_all;
+ memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
+ }
+--- request
+GET /t
+--- response_body eval
+"connected: 1
+request sent: 11
+received: OK
+close: 1 nil
+subrequest: 200, OK\r
+"
+--- no_error_log
+[error]
+
Please sign in to comment.
Something went wrong with that request. Please try again.