bugfix: memory leak would happen when accessing a location using ngx.ctx in Lua contexts other than log_by_lua* but there is also another location that uses log_by_lua*.

This commit is contained in:
agentzh (章亦春) 2012-06-08 11:54:19 +08:00
Родитель 445b916a72
Коммит 285b72716e
2 изменённых файлов: 65 добавлений и 10 удалений

Просмотреть файл

@ -676,9 +676,10 @@ void
ngx_http_lua_request_cleanup(void *data)
{
ngx_http_request_t *r = data;
ngx_http_lua_main_conf_t *lmcf;
ngx_http_lua_ctx_t *ctx;
lua_State *L;
ngx_http_lua_ctx_t *ctx;
ngx_http_lua_loc_conf_t *llcf;
ngx_http_lua_main_conf_t *lmcf;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua request cleanup");
@ -708,14 +709,19 @@ ngx_http_lua_request_cleanup(void *data)
/* we cannot release the ngx.ctx table if we have log_by_lua* hooks
* because request cleanup runs before log phase handlers */
if (ctx->ctx_ref != LUA_NOREF && !lmcf->requires_log) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua release ngx.ctx");
if (ctx->ctx_ref != LUA_NOREF) {
lua_getfield(L, LUA_REGISTRYINDEX, NGX_LUA_REQ_CTX_REF);
luaL_unref(L, -1, ctx->ctx_ref);
ctx->ctx_ref = LUA_NOREF;
lua_pop(L, 1);
llcf = ngx_http_get_module_loc_conf(r, ngx_http_lua_module);
if (llcf->log_handler == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"lua release ngx.ctx");
lua_getfield(L, LUA_REGISTRYINDEX, NGX_LUA_REQ_CTX_REF);
luaL_unref(L, -1, ctx->ctx_ref);
ctx->ctx_ref = LUA_NOREF;
lua_pop(L, 1);
}
}
if (ctx->cc_ref == LUA_NOREF) {

Просмотреть файл

@ -10,7 +10,7 @@ log_level('debug');
repeat_each(2);
plan tests => repeat_each() * (blocks() * 3 + 3);
plan tests => repeat_each() * (blocks() * 3 + 4);
#no_diff();
#no_long_string();
@ -140,3 +140,52 @@ qr{/lua200: [12]}
--- no_error_log
[error]
=== TEST 7: ngx.ctx used in different locations and different ctx (1)
--- config
location /t {
echo hello;
log_by_lua '
ngx.log(ngx.ERR, "ngx.ctx.counter: ", ngx.ctx.counter)
';
}
location /t2 {
content_by_lua '
ngx.ctx.counter = 32
ngx.say("hello")
';
}
--- request
GET /t
--- response_body
hello
--- error_log
ngx.ctx.counter: nil
lua release ngx.ctx
=== TEST 8: ngx.ctx used in different locations and different ctx (2)
--- config
location /t {
echo hello;
log_by_lua '
ngx.log(ngx.ERR, "ngx.ctx.counter: ", ngx.ctx.counter)
';
}
location /t2 {
content_by_lua '
ngx.ctx.counter = 32
ngx.say(ngx.ctx.counter)
';
}
--- request
GET /t2
--- response_body
32
--- error_log
lua release ngx.ctx