skimdz86 opened a new issue, #9750: URL: https://github.com/apache/apisix/issues/9750
### Current Behavior When registering a custom variable in the ctx parameter (https://apisix.apache.org/docs/apisix/plugin-develop/#ctx-parameter), the value of the variable is not request-scoped, but it is shared by requests served by the same apisix thread. This only happens if i register the variable with core.ctx.register_var("variable_name", function(ctx) return value end) and not if I set it with ctx.var.variable_name = value ### Expected Behavior THe expected behaviour should be that the variable is cleared at the end of each http request and not available to subsequent requests. ### Error Logs These are not error logs, but the output logs that demonstrate the behaviour: 2023/06/28 18:28:00 [notice] 1253#1253: *170392 [lua] my_plugin.lua:44: retrieve_info(): re-using cached value: "\/test1", client: 127.0.0.1, server: _, request: "GET /test1 HTTP/1.1", host: "localhost:9080" 2023/06/28 18:28:01 [notice] 1253#1253: *170471 [lua] my_plugin.lua:44: retrieve_info(): re-using cached value: "\/test1", client: 127.0.0.1, server: _, request: "GET /test1 HTTP/1.1", host: "localhost:9080" 2023/06/28 18:28:02 [notice] 1253#1253: *170513 [lua] my_plugin.lua:44: retrieve_info(): re-using cached value: "**\/test1**", client: 127.0.0.1, server: _, request: "**GET /test2** HTTP/1.1", host: "localhost:9080" As you can see, each route should use its path as value of the variable, but here 3 different calls to 2 different routes give the same value. ### Steps to Reproduce 1. run apisix (any version >3.1) 2. deploy a custom plugin with the following code, named my_plugin.lua (so put the plugin under plugin folder and add it to the list in config.yaml) ``` local ngx = ngx local core = require("apisix.core") local plugin = require("apisix.plugin") local plugin_name = "my_plugin" local err local schema = { type = "object", properties = {} } local _M = { version = 0.1, priority = 1007, name = plugin_name, schema = schema } function _M.check_schema(conf, schema_type) local ok, err = core.schema.check(schema, conf) if not ok then return false, err end return true end function _M.init() -- call this function when plugin is loaded local attr = plugin.plugin_attr(plugin_name) if attr then core.log.notice(plugin_name,"Get plugin attr val: ", attr.val) end end local function retrieve_info(conf,ctx,etcd,plugin_name) -- check if the info has already been calculated local cached_api_endpoint_info = ctx.var.cb_api_endpoint_info if cached_api_endpoint_info then core.log.notice("re-using cached value: ", core.json.encode(cached_api_endpoint_info)) return cached_api_endpoint_info end local key = ctx.var.uri core.log.notice("Calculating value for ", key) -- set a context variable with the extracted info, which can be used again later core.ctx.register_var("cb_api_endpoint_info", function(ctx) return key end) return key end function _M.rewrite(conf, ctx) local data, err = retrieve_info(conf,ctx,etcd,plugin_name) core.log.notice(core.json.encode(data)) end return _M ``` 3. configure 2 routes, /test1 and /test2 which point to an upstream that answer with a success response (any response) 4. call the 2 created routes a lot of times subsequently (launching them with curl by hand is enough). E.g. `curl -v -X GET http://localhost:9080/test1` 5. verify that in some cases, for /test1 route is logged the line ".....re-using cached value: "\/test2"....", and in some cases for /test2 route is logged ".....re-using cached value: "\/test1"....". It should instead log the path of the current route. ### Environment - APISIX version (run `apisix version`): 3.1.0, 3.2.0, 3.3.0 (tried with all the 3) - Operating system (run `uname -a`): Red Hat Enterprise Linux release 8.6 - OpenResty / Nginx version (run `openresty -V` or `nginx -V`): openresty/1.21.4.1 - etcd version, if relevant (run `curl http://127.0.0.1:9090/v1/server_info`): 3.5.9 - APISIX Dashboard version, if relevant: - Plugin runner version, for issues related to plugin runners: - LuaRocks version, for installation issues (run `luarocks --version`): 3.9.2 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
