openresty / openresty/lua-nginx-module

[discuss]: support use native Lua coroutine in `init_worker` phase

Open
#2,438 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
11.8k
Forks
2.1k
Avg merge
6h 1m
Merged PRs (30d)
6

Description

According to the documentation here: https://github.com/openresty/lua-nginx-module#coroutinecreate

coroutine.create can be used in init_by_lua*, header_filter_by_lua*, body_filter_by_lua*, because OpenResty would replace OpenResty's coroutine with the Lua native coroutine in these phases, which can not yield.

code: https://github.com/openresty/lua-nginx-module/blob/d34d3c545e80bccbd8110235d73474c11895bd2e/src/ngx_http_lua_coroutine.c#L336-L375

And also here: https://github.com/openresty/lua-resty-core/blob/master/lib/resty/core/coroutine.lua#L4-L24

Now the problem is: If I call coroutine.create in init_worker phase, match the

                if ctx ~= 0x020 and ctx ~= 0x040 then
                    return ours(...)
                end

And then return the OpenResty's coroutine. (ctx of init_worker is 0x0100)

This would cause ean rror API disabled in the context of init_worker_by_lua* because check here: https://github.com/openresty/lua-nginx-module/blob/d34d3c545e80bccbd8110235d73474c11895bd2e/src/ngx_http_lua_coroutine.c#L117

So I suggest returning the Lua native coroutine in the init_worker phase, change like

                if ctx ~= 0x020 and ctx ~= 0x040 and ctx ~= 0x0100 then
                    return ours(...)
                end

This may allow users to use native Lua coroutine in init_worker phase.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read lib/resty/core/coroutine.lua around lines 4-24 and src/ngx_http_lua_coroutine.c around lines 117 and 336-375. Trace how coroutine.create is selected by the phase context, then inspect the init_worker_by_lua* API restrictions. Done means the requested coroutine behavior is supported in init_worker without triggering the disabled-context error.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, lua, nginx
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.