Repository navigation
bugfix: server_rewrite_by_lua* used the global VM when lua_code_cache off - #2531
Open
xiaobiaozhao wants to merge 1 commit into
Open
xiaobiaozhao wants to merge 1 commit into
xiaobiaozhao wants to merge 1 commit into
Conversation
… off. ngx_http_lua_server_rewrite_handler() fetched the Lua VM before the request context was created. With "lua_code_cache off", create_ctx() builds a fresh per-request VM in ctx->vm_state, so fetching the VM too early always returned the persistent global VM. That defeated per-request code caching, reused global coroutine threads, and caused a VM mismatch on yield/resume which aborted requests with "attempt to index field 'ctx' (a nil value)". Fetch the VM after create_ctx() and pass ctx, mirroring the rewrite phase handler. Also reset server_rewrite_src_ref to LUA_REFNIL when the code cache is disabled so inline/file chunks are recompiled per request.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.
Problem
When
lua_code_cache off;is configured,server_rewrite_by_lua*(
server_rewrite_by_lua_block/server_rewrite_by_lua_file) does notwork correctly. If the handler touches
ngx.ctxor yields (e.g.ngx.sleep, cosockets), the request is aborted with a 500 error:Even without yielding, the per-request code cache is not honored: edited
Lua files are not reloaded across requests as
lua_code_cache offrequires.
Environment
ngx_http_lua_version = 10032)--with-debug -DNGX_LUA_USE_ASSERT, OpenResty LuaJITRoot cause
ngx_http_lua_server_rewrite_handler()fetched the Lua VM before therequest context was created:
With
lua_code_cache off,ngx_http_lua_create_ctx()creates a freshper-request Lua VM stored in
ctx->vm_state. Because the VM was fetchedbefore
create_ctx(),ngx_http_lua_get_lua_vm(r, NULL)fell back to thepersistent global VM (
lmcf->lua). Consequences:lua_code_cache offfor this phase.ngx_http_lua_new_thread()sawL == lmcf->luaand reused cachedglobal coroutine threads instead of per-request ones.
mismatched VMs, so
ngx.ctx(stored per-request) was nil in thecontent phase, aborting the request as shown above.
The sibling rewrite phase handler (
ngx_http_lua_rewriteby.c) does nothave this problem: it fetches the VM inside the inline/file handlers,
i.e. after
create_ctx()has run.Fix
create_ctx()and pass thectxexplicitly:L = ngx_http_lua_get_lua_vm(r, ctx);lscf->srv.server_rewrite_src_ref = LUA_REFNILwhen!llcf->enable_code_cache, in both the inline and file handlers, sochunks are recompiled per request (mirrors
rewrite_src_refhandling).Minimal reproduction