Skip to content

Commit 2d82f07

Browse files
committed
bugfix: we now only recycle light threads which are "entry threads" otherwise it is not safe to recycle user created threads since we have no idea whether the user may still hold refs to them.
1 parent b281121 commit 2d82f07

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/ngx_http_lua_uthread.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ ngx_http_lua_uthread_spawn(lua_State *L)
9191
coctx->parent_co_ctx = ctx->cur_co_ctx;
9292
ctx->cur_co_ctx = coctx;
9393

94+
ngx_http_lua_attach_co_ctx_to_L(coctx->co, coctx);
95+
9496
ngx_http_lua_probe_user_thread_spawn(r, L, coctx->co);
9597

9698
dd("yielding with arg %s, top=%d, index-1:%s", luaL_typename(L, -1),

src/ngx_http_lua_util.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,10 @@ ngx_http_lua_del_thread(ngx_http_request_t *r, lua_State *L,
422422
#ifdef HAVE_LUA_RESETTHREAD
423423
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
424424

425-
if (L == lmcf->lua && !ngx_queue_empty(&lmcf->free_lua_threads)) {
425+
if (ctx != NULL
426+
&& coctx->co == ctx->entry_co_ctx.co
427+
&& L == lmcf->lua && !ngx_queue_empty(&lmcf->free_lua_threads))
428+
{
426429
lua_resetthread(L, coctx->co);
427430
q = ngx_queue_head(&lmcf->free_lua_threads);
428431
tref = ngx_queue_data(q, ngx_http_lua_thread_ref_t, queue);

src/ngx_http_lua_util.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,14 @@ ngx_http_lua_free_thread(ngx_http_request_t *r, lua_State *L, int co_ref,
560560
#ifdef HAVE_LUA_RESETTHREAD
561561
ngx_queue_t *q;
562562
ngx_http_lua_thread_ref_t *tref ;
563+
ngx_http_lua_ctx_t *ctx;
563564

564-
if (L == lmcf->lua && !ngx_queue_empty(&lmcf->free_lua_threads)) {
565+
ctx = ngx_http_get_module_ctx(r, ngx_http_lua_module);
566+
if (ctx != NULL
567+
&& L == ctx->entry_co_ctx.co
568+
&& L == lmcf->lua
569+
&& !ngx_queue_empty(&lmcf->free_lua_threads))
570+
{
565571
lua_resetthread(L, co);
566572

567573
q = ngx_queue_head(&lmcf->free_lua_threads);

0 commit comments

Comments
 (0)