@@ -156,3 +156,203 @@ my $s = "ngx.say('ok')\n";
156156ok
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]
0 commit comments