@@ -22,6 +22,46 @@ our $HttpConfig = qq{
2222 }
2323} ;
2424
25+ # Same as $HttpConfig, but fires a request from a request-less phase
26+ # (an init_worker timer) where ngx.var is disabled.
27+ our $ HttpConfigInitWorker = qq {
28+ lua_package_path "$ pwd /lib/?.lua;/usr/local/share/lua/5.1/?.lua;;";
29+ error_log logs/error.log debug;
30+ resolver 8.8.8.8 ipv6=off;
31+
32+ init_by_lua_block {
33+ if $ ENV {TEST_COVERAGE} == 1 then
34+ jit. off()
35+ require (" luacov.runner" ). init()
36+ end
37+
38+ require (" resty.http" ). debug(true)
39+ }
40+
41+ init_worker_by_lua_block {
42+ local function make_request(premature)
43+ if premature then
44+ return
45+ end
46+
47+ local http = require " resty.http"
48+ local httpc = http. new ()
49+ local res, err = httpc: request_uri(" http://www.google.com" )
50+ if err then
51+ ngx. log (ngx. ERR, " init_worker request failed: " , err)
52+ end
53+ ngx. log (ngx. INFO, " init_worker request completed" )
54+ end
55+
56+ -- cosockets are unavailable directly in init_worker, so defer to a
57+ -- timer. The timer phase is also request-less (ngx. var disabled).
58+ local ok , err = ngx. timer. at(0 , make_request)
59+ if not ok then
60+ ngx. log (ngx. ERR, " failed to create timer: " , err)
61+ end
62+ }
63+ } ;
64+
2565no_long_string();
2666# no_diff();
2767
@@ -93,3 +133,24 @@ GET /lua
93133traceparent: 00 -000000000000000019f4e02c82857913-11488c6e00d1d248-01
94134-- - error_log
95135traceparent: 00 -00000000000000006633c2d00527dd33-1af98f7e6ecd16ff-01
136+
137+
138+ === TEST 4 : A request from a request-less phase does not error reading ngx. var
139+ -- - http_config eval: $::HttpConfigInitWorker
140+ -- - config
141+ location / lua {
142+ content_by_lua_block {
143+ -- give the init_worker timer time to fire and finish
144+ ngx. sleep (0.5 )
145+ ngx. say (" ok" )
146+ }
147+ }
148+ -- - request
149+ GET / lua
150+ -- - response_body
151+ ok
152+ -- - no_error_log
153+ [error]
154+ API disabled in the context of ngx. timer
155+ -- - error_log
156+ init_worker request completed
0 commit comments