Skip to content

Commit 2bb2955

Browse files
authored
change: we now avoid running init_by_lua* in signaller processes and when testing the nginx configuration. #c04bc97
1 parent c8b878d commit 2bb2955

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/ngx_stream_lua_module.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ ngx_stream_lua_init(ngx_conf_t *cf)
470470

471471
ngx_pool_cleanup_t *cln;
472472

473+
if (ngx_process == NGX_PROCESS_SIGNALLER || ngx_test_config) {
474+
return NGX_OK;
475+
}
476+
473477
lmcf = ngx_stream_conf_get_module_main_conf(cf,
474478
ngx_stream_lua_module);
475479

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
use Test::Nginx::Socket::Lua::Stream;
2+
3+
repeat_each(2);
4+
5+
plan tests => repeat_each() * (blocks() * 2);
6+
7+
$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
8+
9+
my $html_dir = $ENV{TEST_NGINX_HTML_DIR};
10+
my $stream_config = <<_EOC_;
11+
init_by_lua_block {
12+
function set_up_ngx_tmp_conf()
13+
local conf = [[
14+
events {
15+
worker_connections 64;
16+
}
17+
stream {
18+
init_by_lua_block {
19+
ngx.log(ngx.ERR, "run init_by_lua")
20+
}
21+
}
22+
]]
23+
24+
assert(os.execute("mkdir -p $html_dir/logs"))
25+
26+
local conf_file = "$html_dir/nginx.conf"
27+
local f, err = io.open(conf_file, "w")
28+
if not f then
29+
ngx.log(ngx.ERR, err)
30+
return
31+
end
32+
33+
assert(f:write(conf))
34+
35+
return conf_file
36+
end
37+
38+
function get_ngx_bin_path()
39+
local ffi = require "ffi"
40+
ffi.cdef[[char **ngx_argv;]]
41+
return ffi.string(ffi.C.ngx_argv[0])
42+
end
43+
}
44+
_EOC_
45+
46+
add_block_preprocessor(sub {
47+
my $block = shift;
48+
49+
if (!defined $block->stream_config) {
50+
$block->set_value("stream_config", $stream_config);
51+
}
52+
});
53+
54+
log_level("warn");
55+
no_long_string();
56+
run_tests();
57+
58+
__DATA__
59+
60+
=== TEST 1: ensure init_by_lua* is not run in signaller process
61+
--- stream_server_config
62+
content_by_lua_block {
63+
local conf_file = set_up_ngx_tmp_conf()
64+
local nginx = get_ngx_bin_path()
65+
66+
local cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -s reopen"
67+
local p, err = io.popen(cmd)
68+
if not p then
69+
ngx.log(ngx.ERR, err)
70+
return
71+
end
72+
73+
local out, err = p:read("*a")
74+
if not out then
75+
ngx.log(ngx.ERR, err)
76+
77+
else
78+
ngx.log(ngx.WARN, out)
79+
end
80+
}
81+
--- no_error_log eval
82+
qr/\[error\] .*? init_by_lua:\d+: run init_by_lua/
83+
84+
85+
86+
=== TEST 2: init_by_lua* does not run when testing Nginx configuration
87+
--- stream_server_config
88+
content_by_lua_block {
89+
local conf_file = set_up_ngx_tmp_conf()
90+
local nginx = get_ngx_bin_path()
91+
92+
local cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -t"
93+
local p, err = io.popen(cmd)
94+
if not p then
95+
ngx.log(ngx.ERR, err)
96+
return
97+
end
98+
99+
local out, err = p:read("*a")
100+
if not out then
101+
ngx.log(ngx.ERR, err)
102+
103+
else
104+
ngx.log(ngx.WARN, out)
105+
end
106+
107+
local cmd = nginx .. " -p $TEST_NGINX_HTML_DIR -c " .. conf_file .. " -T"
108+
local p, err = io.popen(cmd)
109+
if not p then
110+
ngx.log(ngx.ERR, err)
111+
return
112+
end
113+
114+
local out, err = p:read("*a")
115+
if not out then
116+
ngx.log(ngx.ERR, err)
117+
118+
else
119+
ngx.log(ngx.WARN, out)
120+
end
121+
}
122+
--- no_error_log eval
123+
qr/\[error\] .*? init_by_lua:\d+: run init_by_lua/

0 commit comments

Comments
 (0)