Skip to content

Commit d6bc02e

Browse files
committed
bugfix: the coroutine API table introduced by require("coroutine") was not working in the context of ngx_lua. thanks Paul K and Pierre-Yves Gérardy for the report (in #381).
1 parent c2e2948 commit d6bc02e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/ngx_http_lua_coroutine.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ ngx_http_lua_inject_coroutine_api(ngx_log_t *log, lua_State *L)
297297
"coroutine.wrap = function(f)\n"
298298
"local co = create(f)\n"
299299
"return function(...) return select(2, resume(co, ...)) end\n"
300-
"end";
300+
"end\n"
301+
"package.loaded.coroutine = coroutine";
301302

302303
#if 0
303304
"debug.sethook(function () collectgarbage() end, 'rl', 1)"

t/091-coroutine.t

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,3 +1190,42 @@ ok
11901190
--- no_error_log
11911191
[error]
11921192
1193+
1194+
1195+
=== TEST 29: require "coroutine"
1196+
--- config
1197+
location /lua {
1198+
content_by_lua '
1199+
local coroutine = require "coroutine"
1200+
local cc, cr, cy = coroutine.create, coroutine.resume, coroutine.yield
1201+
1202+
function f()
1203+
local cnt = 0
1204+
for i = 1, 20 do
1205+
ngx.say("Hello, ", cnt)
1206+
ngx.sleep(0.001)
1207+
cy()
1208+
cnt = cnt + 1
1209+
end
1210+
end
1211+
1212+
local c = cc(f)
1213+
for i=1,3 do
1214+
cr(c)
1215+
ngx.say("***")
1216+
end
1217+
';
1218+
}
1219+
--- request
1220+
GET /lua
1221+
--- stap2 eval: $::StapScript
1222+
--- response_body
1223+
Hello, 0
1224+
***
1225+
Hello, 1
1226+
***
1227+
Hello, 2
1228+
***
1229+
--- no_error_log
1230+
[error]
1231+

0 commit comments

Comments
 (0)