Skip to content

Commit 8f4c485

Browse files
committed
bugfix: coroutines might incorrectly enter the "dead" state even right after creation with coroutine.create(). thanks James Hurst for the report.
1 parent 8d14598 commit 8f4c485

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/ngx_http_lua_coroutine.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,16 @@ ngx_http_lua_coroutine_create_helper(lua_State *L, ngx_http_request_t *r,
8181

8282
ngx_http_lua_probe_user_coroutine_create(r, L, co);
8383

84-
coctx = ngx_http_lua_create_co_ctx(r, ctx);
84+
coctx = ngx_http_lua_get_co_ctx(co, ctx);
8585
if (coctx == NULL) {
86-
return luaL_error(L, "out of memory");
86+
coctx = ngx_http_lua_create_co_ctx(r, ctx);
87+
if (coctx == NULL) {
88+
return luaL_error(L, "out of memory");
89+
}
90+
91+
} else {
92+
ngx_memzero(coctx, sizeof(ngx_http_lua_co_ctx_t));
93+
coctx->co_ref = LUA_NOREF;
8794
}
8895

8996
coctx->co = co;

t/091-coroutine.t

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,3 +1158,35 @@ data
11581158
--- no_error_log
11591159
[error]
11601160
1161+
1162+
1163+
=== TEST 28: coroutine context collicisions
1164+
--- config
1165+
location /lua {
1166+
content_by_lua '
1167+
local cc, cr, cy = coroutine.create, coroutine.resume, coroutine.yield
1168+
1169+
function f()
1170+
return 3
1171+
end
1172+
1173+
for i = 1, 10 do
1174+
collectgarbage()
1175+
local c = cc(f)
1176+
if coroutine.status(c) == "dead" then
1177+
ngx.say("found a dead coroutine")
1178+
return
1179+
end
1180+
cr(c)
1181+
end
1182+
ngx.say("ok")
1183+
';
1184+
}
1185+
--- request
1186+
GET /lua
1187+
--- stap2 eval: $::StapScript
1188+
--- response_body
1189+
ok
1190+
--- no_error_log
1191+
[error]
1192+

0 commit comments

Comments
 (0)