-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using outer scope required modules inside an ngx.timer.at handler #2312
Comments
Hey, the problem seems like the lifetime of request context variables. But the problem arises only when you try to call the name
Or
You pass the module or the function as a parameter to the |
Hey @Rockybilly Thanks for the quick response! I'm not sure if LuaJIT passes by value or reference so I'm assuming "by reference" here. Won't the passed function get the same treatment as calling the function from within the handler and get removed once the request finishes? Any chance you could point me to the code that frees up the memory on Appreciate your help here 🙏 |
To your first question, the contributers are probably better equipped to answer. However my guess is, a garbage collector would be reference counting the references before freeing them. So when a timer is set to run, the function body has never been run when the request context is freed, so no reference count to hold on to the value. But when you use it in function parameters, the reference count would be increased and it would not be freed until function is done running and the parameter goes out of scope. I can at least say that I tried it and didn't receive any errors. To your second question, zero harm in requiring the module in the function. If performance and micro-optimization is one of your concerns, it would mean one extra local variable declaration, function call and table lookup ( For your third question, However, for testing, you can set your timer for like for 1 second delay, guarantees that the request context is gone for sure. And try something like |
I appreciate your help here @Rockybilly. I do see the behavior of referencing an outer scope variable inside the handler function without passing them as arguments, in other projects. I assume everything works for them since the request context gets freed long after the handler finishes. I would appreciate an answer from one of the contributors on what's the recommended practice here if one of them stumbles upon this issue |
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
:This would result in
attempt to index upvalue 'fn'
error thrown from thehandler
function.I deduced this is happening because the
ngx.exit
is called before the call tofn
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 thehandler
function but I would appreciate if there was a clear-cut answer on whether or not modules defined outside thengx.timer.at
handler function are available in it.For clarification,
lua_package_path
is set up correctly.OpenResty:
version 1.25.3.1-2
The text was updated successfully, but these errors were encountered: