Skip to content

Commit bee1cab

Browse files
committed
bugfix: ngx.ctx was not accessible at all in set_by_lua*. thanks Pierre.
1 parent 4e56036 commit bee1cab

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/ngx_http_lua_setby.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ ngx_http_lua_set_by_chunk(lua_State *L, ngx_http_request_t *r, ngx_str_t *val,
4040
ngx_pool_t *old_pool;
4141
#endif
4242

43+
ngx_http_lua_ctx_t *ctx;
44+
45+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
46+
47+
if (ctx == NULL) {
48+
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_lua_ctx_t));
49+
if (ctx == NULL) {
50+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
51+
}
52+
53+
dd("setting new ctx: ctx = %p", ctx);
54+
55+
ctx->cc_ref = LUA_NOREF;
56+
ctx->ctx_ref = LUA_NOREF;
57+
58+
ngx_http_set_ctx(r, ctx, ngx_http_lua_module);
59+
}
60+
4361
/* set Lua VM panic handler */
4462
lua_atpanic(L, ngx_http_lua_atpanic);
4563

t/033-ctx.t

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use Test::Nginx::Socket;
1010
repeat_each(2);
1111
#repeat_each(1);
1212

13-
plan tests => repeat_each() * (blocks() * 2 + 1);
13+
plan tests => repeat_each() * (blocks() * 2 + 2);
1414

1515
#no_diff();
1616
#no_long_string();
@@ -180,3 +180,19 @@ dog = hello
180180
dog = hiya
181181
parent: nil
182182

183+
184+
185+
=== TEST 8: set_by_lua
186+
--- config
187+
location /lua {
188+
set_by_lua $bar 'ngx.ctx.foo = 3 return 4';
189+
set_by_lua $foo 'return ngx.ctx.foo';
190+
echo "foo = $foo, bar = $bar";
191+
}
192+
--- request
193+
GET /lua
194+
--- response_body
195+
foo = 3, bar = 4
196+
--- no_error_log
197+
[error]
198+

0 commit comments

Comments
 (0)