Skip to content

Commit c3d3f6e

Browse files
bugfix: nginx crash when resolve an not exist domain. (#266)
1 parent b3f229a commit c3d3f6e

File tree

4 files changed

+205
-16
lines changed

4 files changed

+205
-16
lines changed

src/ngx_stream_lua_socket_tcp.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ static void ngx_stream_lua_socket_free_pool(ngx_log_t *log,
172172
static int ngx_stream_lua_socket_shutdown_pool(lua_State *L);
173173
static void ngx_stream_lua_socket_shutdown_pool_helper(
174174
ngx_stream_lua_socket_pool_t *spool);
175-
static void
176-
ngx_stream_lua_socket_empty_resolve_handler(ngx_resolver_ctx_t *ctx);
177175
static int ngx_stream_lua_socket_prepare_error_retvals(
178176
ngx_stream_lua_request_t *r, ngx_stream_lua_socket_tcp_upstream_t *u,
179177
lua_State *L, ngx_uint_t ft_type);
@@ -1136,13 +1134,6 @@ ngx_stream_lua_socket_tcp_connect(lua_State *L)
11361134
}
11371135

11381136

1139-
static void
1140-
ngx_stream_lua_socket_empty_resolve_handler(ngx_resolver_ctx_t *ctx)
1141-
{
1142-
/* do nothing */
1143-
}
1144-
1145-
11461137
static void
11471138
ngx_stream_lua_socket_resolve_handler(ngx_resolver_ctx_t *ctx)
11481139
{
@@ -6174,10 +6165,8 @@ ngx_stream_lua_tcp_resolve_cleanup(void *data)
61746165
return;
61756166
}
61766167

6177-
/* just to be safer */
6178-
rctx->handler = ngx_stream_lua_socket_empty_resolve_handler;
6179-
6180-
ngx_resolve_name_done(rctx);
6168+
/* postpone free the rctx in the handler */
6169+
rctx->handler = ngx_resolve_name_done;
61816170
}
61826171

61836172

src/ngx_stream_lua_socket_udp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1673,7 +1673,8 @@ ngx_stream_lua_udp_resolve_cleanup(void *data)
16731673
return;
16741674
}
16751675

1676-
ngx_resolve_name_done(rctx);
1676+
/* postpone free the rctx in the handler */
1677+
rctx->handler = ngx_resolve_name_done;
16771678
}
16781679

16791680

t/014-bugs.t

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,203 @@ my $s = "ngx.say('ok')\n";
156156
ok
157157
--- no_error_log
158158
[error]
159+
160+
161+
162+
=== TEST 6: tcp: nginx crash when resolve an not exist domain in ngx.thread.spawn
163+
https://github.com/openresty/lua-nginx-module/issues/1915
164+
--- stream_config eval
165+
"lua_package_path '$::HtmlDir/?.lua;./?.lua;;';"
166+
--- stream_server_config
167+
resolver $TEST_NGINX_RESOLVER ipv6=off;
168+
content_by_lua_block {
169+
local function tcp(host, port)
170+
local sock = ngx.socket.tcp()
171+
local ok,err = sock:connect(host, port)
172+
if not ok then
173+
ngx.log(ngx.WARN, "failed: ", err)
174+
sock:close()
175+
return false
176+
end
177+
178+
sock:close()
179+
return true
180+
end
181+
182+
local host = "www.notexistdomain.com"
183+
local port = 80
184+
185+
local threads = {}
186+
for i = 1, 3 do
187+
threads[i] = ngx.thread.spawn(tcp, host, port)
188+
end
189+
190+
local ok, res = ngx.thread.wait(threads[1],threads[2],threads[3])
191+
if not ok then
192+
ngx.say("failed to wait thread")
193+
return
194+
end
195+
196+
ngx.say("res: ", res)
197+
198+
for i = 1, 3 do
199+
ngx.thread.kill(threads[i])
200+
end
201+
}
202+
203+
--- request
204+
GET /t
205+
--- response_body
206+
res: false
207+
--- error_log
208+
www.notexistdomain.com could not be resolved
209+
210+
211+
212+
=== TEST 7: domain exists with tcp socket
213+
https://github.com/openresty/lua-nginx-module/issues/1915
214+
--- stream_config eval
215+
"lua_package_path '$::HtmlDir/?.lua;./?.lua;;';"
216+
--- stream_server_config
217+
resolver $TEST_NGINX_RESOLVER ipv6=off;
218+
content_by_lua_block {
219+
local function tcp(host, port)
220+
local sock = ngx.socket.tcp()
221+
local ok,err = sock:connect(host, port)
222+
if not ok then
223+
ngx.log(ngx.WARN, "failed: ", err)
224+
sock:close()
225+
return false
226+
end
227+
228+
sock:close()
229+
return true
230+
end
231+
232+
local host = "www.openresty.org"
233+
local port = 80
234+
235+
local threads = {}
236+
for i = 1, 3 do
237+
threads[i] = ngx.thread.spawn(tcp, host, port)
238+
end
239+
240+
local ok, res = ngx.thread.wait(threads[1],threads[2],threads[3])
241+
if not ok then
242+
ngx.say("failed to wait thread")
243+
return
244+
end
245+
246+
ngx.say("res: ", res)
247+
248+
for i = 1, 3 do
249+
ngx.thread.kill(threads[i])
250+
end
251+
}
252+
253+
--- request
254+
GET /t
255+
--- response_body
256+
res: true
257+
--- no_error_log
258+
[error]
259+
260+
261+
262+
=== TEST 8: domain exists with udp socket
263+
https://github.com/openresty/lua-nginx-module/issues/1915
264+
--- stream_config eval
265+
"lua_package_path '$::HtmlDir/?.lua;./?.lua;;';"
266+
--- stream_server_config
267+
resolver $TEST_NGINX_RESOLVER ipv6=off;
268+
content_by_lua_block {
269+
local function udp(host, port)
270+
local sock = ngx.socket.udp()
271+
local ok,err = sock:setpeername(host, port)
272+
if not ok then
273+
ngx.log(ngx.WARN, "failed: ", err)
274+
sock:close()
275+
return false
276+
end
277+
278+
sock:close()
279+
return true
280+
end
281+
282+
local host = "www.notexistdomain.com"
283+
local port = 80
284+
285+
local threads = {}
286+
for i = 1, 3 do
287+
threads[i] = ngx.thread.spawn(udp, host, port)
288+
end
289+
290+
local ok, res = ngx.thread.wait(threads[1],threads[2],threads[3])
291+
if not ok then
292+
ngx.say("failed to wait thread")
293+
return
294+
end
295+
296+
ngx.say("res: ", res)
297+
298+
for i = 1, 3 do
299+
ngx.thread.kill(threads[i])
300+
end
301+
}
302+
303+
--- request
304+
GET /t
305+
--- response_body
306+
res: false
307+
--- error_log
308+
www.notexistdomain.com could not be resolved
309+
310+
311+
312+
=== TEST 9: udp: nginx crash when resolve an not exist domain in ngx.thread.spawn
313+
https://github.com/openresty/lua-nginx-module/issues/1915
314+
--- stream_config eval
315+
"lua_package_path '$::HtmlDir/?.lua;./?.lua;;';"
316+
--- stream_server_config
317+
resolver $TEST_NGINX_RESOLVER ipv6=off;
318+
content_by_lua_block {
319+
local function udp(host, port)
320+
local sock = ngx.socket.udp()
321+
local ok,err = sock:setpeername(host, port)
322+
if not ok then
323+
ngx.log(ngx.WARN, "failed: ", err)
324+
sock:close()
325+
return false
326+
end
327+
328+
sock:close()
329+
return true
330+
end
331+
332+
local host = "www.openresty.org"
333+
local port = 80
334+
335+
local threads = {}
336+
for i = 1, 3 do
337+
threads[i] = ngx.thread.spawn(udp, host, port)
338+
end
339+
340+
local ok, res = ngx.thread.wait(threads[1],threads[2],threads[3])
341+
if not ok then
342+
ngx.say("failed to wait thread")
343+
return
344+
end
345+
346+
ngx.say("res: ", res)
347+
348+
for i = 1, 3 do
349+
ngx.thread.kill(threads[i])
350+
end
351+
}
352+
353+
--- request
354+
GET /t
355+
--- response_body
356+
res: true
357+
--- no_error_log
358+
[error]

t/127-uthread-kill.t

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ our $StapScript = $t::StapThread::StapScript;
77

88
repeat_each(2);
99

10-
plan tests => repeat_each() * (blocks() * 5 + 2);
10+
plan tests => repeat_each() * (blocks() * 5 + 1);
1111

1212
$ENV{TEST_NGINX_RESOLVER} ||= '8.8.8.8';
1313
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= '11211';
@@ -171,7 +171,6 @@ killed
171171
[error]
172172
--- error_log
173173
lua tcp socket abort resolver
174-
resolve name done: -2
175174

176175

177176

0 commit comments

Comments
 (0)