Skip to content

Commit d00976d

Browse files
committed
bugfix: nginx could crash on request fialization when running the cosocket cleanup handler due to the lack of check of the ctx pointer. thanks shaneeb for reporting this in github issue #110.
1 parent b440e5b commit d00976d

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

src/ngx_http_lua_socket.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2036,16 +2036,19 @@ ngx_http_lua_socket_finalize(ngx_http_request_t *r,
20362036
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
20372037
"lua finalize socket");
20382038

2039-
if (u->bufs_in) {
2039+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
20402040

2041-
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
2041+
if (ctx && u->bufs_in) {
20422042

20432043
ll = &u->bufs_in;
20442044
for (cl = u->bufs_in; cl; cl = cl->next) {
2045+
dd("bufs_in chain: %p, next %p", cl, cl->next);
20452046
cl->buf->pos = cl->buf->last;
20462047
ll = &cl->next;
20472048
}
20482049

2050+
dd("ctx: %p", ctx);
2051+
dd("free recv bufs: %p", ctx->free_recv_bufs);
20492052
*ll = ctx->free_recv_bufs;
20502053
ctx->free_recv_bufs = u->bufs_in;
20512054
u->bufs_in = NULL;

t/068-socket-keepalive.t

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

66
repeat_each(2);
77

8-
plan tests => repeat_each() * (blocks() * 5 + 10);
8+
plan tests => repeat_each() * (blocks() * 5 + 8);
99

1010
our $HtmlDir = html_dir;
1111

@@ -899,3 +899,62 @@ lua socket get keepalive peer: using connection
899899
[error]
900900
[alert]
901901

902+
903+
904+
=== TEST 13: github issue #110: ngx.exit with HTTP_NOT_FOUND causes worker process to exit
905+
--- http_config eval
906+
"lua_package_path '$::HtmlDir/?.lua;./?.lua';"
907+
--- config
908+
error_page 404 /404.html;
909+
location /t {
910+
set $port $TEST_NGINX_MEMCACHED_PORT;
911+
access_by_lua '
912+
local test = require "test"
913+
local port = ngx.var.port
914+
test.go(port)
915+
ngx.exit(404)
916+
';
917+
echo hello;
918+
}
919+
--- user_files
920+
>>> 404.html
921+
Not found, dear...
922+
>>> test.lua
923+
module("test", package.seeall)
924+
925+
function go(port)
926+
local sock = ngx.socket.tcp()
927+
local ok, err = sock:connect("127.0.0.1", port)
928+
if not ok then
929+
ngx.log(ngx.ERR, "failed to connect: ", err)
930+
return
931+
end
932+
933+
local req = "flush_all\r\n"
934+
935+
local bytes, err = sock:send(req)
936+
if not bytes then
937+
ngx.log(ngx.ERR, "failed to send request: ", err)
938+
return
939+
end
940+
941+
local line, err, part = sock:receive()
942+
if not line then
943+
ngx.log(ngx.ERR, "failed to receive a line: ", err, " [", part, "]")
944+
return
945+
end
946+
947+
-- local ok, err = sock:setkeepalive()
948+
-- if not ok then
949+
-- ngx.log(ngx.ERR, "failed to set reusable: ", err)
950+
-- return
951+
-- end
952+
end
953+
--- request
954+
GET /t
955+
--- response_body
956+
Not found, dear...
957+
--- error_code: 404
958+
--- no_error_log
959+
[error]
960+

0 commit comments

Comments
 (0)