openresty / openresty/lua-nginx-module
Using outer scope required modules inside an ngx.timer.at handler
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 11.8k
- Forks
- 2.1k
- Avg merge
- 6h 1m
- Merged PRs (30d)
- 6
Description
Hello,
I've encountered an error which I understand the source to but unsure what's the best practice here.
I have the following example module respond.lua:
local ngx = require 'ngx'
local config = require 'config'
local required_module = require 'module_with_function'
local function handler(_, arg)
required_module.fn(arg)
end
local function main()
ngx.timer.at(0, handler, 'test')
ngx.status = config.code
ngx.say(config.response)
ngx.exit(config.code)
end
main()
This would result in attempt to index upvalue 'fn' error thrown from the handler function.
I deduced this is happening because the ngx.exit is called before the call to fn but this context sharing is unintuitive.
If I add a simple sleep before the call to ngx.exit it works just fine meaning the handler function does share the same context as the encapsulating module.
I resorted to adding another require inside the handler function but I would appreciate if there was a clear-cut answer on whether or not modules defined outside the ngx.timer.at handler function are available in it.
For clarification, lua_package_path is set up correctly.
OpenResty: version 1.25.3.1-2
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the behavior from the respond.lua example using OpenResty 1.25.3.1-2, focusing on the ngx.timer.at handler and the outer required module. Read the relevant timer and module-loading behavior, then clarify whether the example is expected and document the supported practice or the required change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua, nginx
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100