Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/ngx_http_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3168,16 +3168,29 @@ ngx_http_lua_param_set(lua_State *L)
ngx_http_lua_co_ctx_t *
ngx_http_lua_get_co_ctx(lua_State *L, ngx_http_lua_ctx_t *ctx)
{
#ifdef HAVE_LUA_EXDATA2
return (ngx_http_lua_co_ctx_t *) lua_getexdata2(L);
#else
ngx_uint_t i;
ngx_list_part_t *part;
ngx_http_lua_co_ctx_t *coctx;
#ifdef HAVE_LUA_EXDATA2
ngx_http_lua_co_ctx_t *exdata_coctx;

exdata_coctx = (ngx_http_lua_co_ctx_t *) lua_getexdata2(L);
if (exdata_coctx == NULL) {
return NULL;
}

/* The exdata2 coctx is request-pool allocated and may be stale. Do not
* dereference it until it matches storage owned by the current ctx. */
if (exdata_coctx == &ctx->entry_co_ctx) {
return L == ctx->entry_co_ctx.co ? exdata_coctx : NULL;
}

#else

if (L == ctx->entry_co_ctx.co) {
return &ctx->entry_co_ctx;
}
#endif

if (ctx->user_co_ctx == NULL) {
return NULL;
Expand All @@ -3200,13 +3213,18 @@ ngx_http_lua_get_co_ctx(lua_State *L, ngx_http_lua_ctx_t *ctx)
i = 0;
}

#ifdef HAVE_LUA_EXDATA2
if (exdata_coctx == &coctx[i]) {
return L == coctx[i].co ? exdata_coctx : NULL;
}
#else
if (coctx[i].co == L) {
return &coctx[i];
}
#endif
}

return NULL;
#endif
}


Expand Down
57 changes: 57 additions & 0 deletions t/197-coroutine-request-cleanup.t
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,60 @@ lua clean up the timer for pending ngx.sleep
--- no_error_log
[error]
[alert]



=== TEST 3: timers reject coroutines owned by another request context
--- config
location = /t {
content_by_lua_block {
local function inspect(premature, label, retained)
assert(not premature)

ngx.log(ngx.WARN, label, " status: ",
coroutine.status(retained))

local ok, err = pcall(coroutine.resume, retained)
ngx.log(ngx.WARN, label, " resume rejected: ",
not ok
and string.find(err, "no co ctx found", 1, true)
~= nil)

local local_co = coroutine.create(function()
return "local coroutine"
end)
local resumed, value = coroutine.resume(local_co)
ngx.log(ngx.WARN, label, " local resume: ",
resumed and value == "local coroutine")
end

local overlapping = coroutine.create(function()
return "foreign coroutine"
end)
local expired = coroutine.create(function()
return "stale coroutine"
end)

assert(ngx.timer.at(0, inspect, "overlapping", overlapping))
assert(ngx.timer.at(0.05, inspect, "expired", expired))

ngx.sleep(0.01)
ngx.say("timers scheduled: OK")
}
}
--- request
GET /t
--- response_body
timers scheduled: OK
--- wait: 0.1
--- grep_error_log eval: qr/(?:overlapping|expired) (?:status|resume rejected|local resume): [^,\n]+/
--- grep_error_log_out
overlapping status: dead
overlapping resume rejected: true
overlapping local resume: true
expired status: dead
expired resume rejected: true
expired local resume: true
--- no_error_log
[error]
[alert]