Skip to content

Commit 055bb17

Browse files
spacewanderthibaultcha
authored andcommitted
bugfix: fixed a segfault introduced in 7c2b58e when testing an nginx configuration containing 'lua_shared_dict' directive(s).
In commit 7c2b58e, we avoided running `init_by_lua*` in signaller processes and when testing the Nginx configuration. However, when the Nginx configuration contains `lua_shared_dict` directives, the execution of `init_by_lua*` will be postponded when testing the configuration. This case was not handled and led to the segmentation fault reported in #1462.
1 parent aef03eb commit 055bb17

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

src/ngx_http_lua_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ ngx_http_lua_shared_memory_init(ngx_shm_zone_t *shm_zone, void *data)
195195
lmcf->shm_zones_inited++;
196196

197197
if (lmcf->shm_zones_inited == lmcf->shm_zones->nelts
198-
&& lmcf->init_handler)
198+
&& lmcf->init_handler && !ngx_test_config)
199199
{
200200
saved_cycle = ngx_cycle;
201201
ngx_cycle = ctx->cycle;

t/160-disable-init-by-lua.t

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ $ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
99
my $html_dir = $ENV{TEST_NGINX_HTML_DIR};
1010
my $http_config = <<_EOC_;
1111
init_by_lua_block {
12-
function set_up_ngx_tmp_conf()
13-
local conf = [[
14-
events {
15-
worker_connections 64;
16-
}
17-
http {
18-
init_by_lua_block {
19-
ngx.log(ngx.ERR, "run init_by_lua")
12+
function set_up_ngx_tmp_conf(conf)
13+
if conf == nil then
14+
conf = [[
15+
events {
16+
worker_connections 64;
2017
}
21-
}
22-
]]
18+
http {
19+
init_by_lua_block {
20+
ngx.log(ngx.ERR, "run init_by_lua")
21+
}
22+
}
23+
]]
24+
end
2325
2426
assert(os.execute("mkdir -p $html_dir/logs"))
2527
@@ -129,3 +131,57 @@ qr/\[error\] .*? init_by_lua:\d+: run init_by_lua/
129131
}
130132
--- no_error_log eval
131133
qr/\[error\] .*? init_by_lua:\d+: run init_by_lua/
134+
135+
136+
137+
=== TEST 3: init_by_lua* does not run when testing Nginx configuration which contains 'lua_shared_dict' (GitHub #1462)
138+
--- config
139+
location = /t {
140+
content_by_lua_block {
141+
local conf = [[
142+
events {
143+
worker_connections 64;
144+
}
145+
http {
146+
lua_shared_dict test 64k;
147+
init_by_lua_block {
148+
ngx.log(ngx.ERR, "run init_by_lua with lua_shared_dict")
149+
}
150+
}
151+
]]
152+
local conf_file = set_up_ngx_tmp_conf(conf)
153+
local nginx = get_ngx_bin_path()
154+
155+
local cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -t"
156+
local p, err = io.popen(cmd)
157+
if not p then
158+
ngx.log(ngx.ERR, err)
159+
return
160+
end
161+
162+
local out, err = p:read("*a")
163+
if not out then
164+
ngx.log(ngx.ERR, err)
165+
166+
else
167+
ngx.log(ngx.WARN, out)
168+
end
169+
170+
local cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -T"
171+
local p, err = io.popen(cmd)
172+
if not p then
173+
ngx.log(ngx.ERR, err)
174+
return
175+
end
176+
177+
local out, err = p:read("*a")
178+
if not out then
179+
ngx.log(ngx.ERR, err)
180+
181+
else
182+
ngx.log(ngx.WARN, out)
183+
end
184+
}
185+
}
186+
--- no_error_log eval
187+
qr/\[error\] .*? init_by_lua:\d+: run init_by_lua with lua_shared_dict/

0 commit comments

Comments
 (0)