griffenliu opened a new issue #2194:
URL: https://github.com/apache/apisix/issues/2194
### Issue description
when route not found and `return core.response.exit(404, {error_msg =
"failed to match any routes"})`, the apisix occur error.
### Environment
* apisix version (cmd: `apisix version`): 1.5
* OS: Ubuntu 18.04
### Minimal test code / Steps to reproduce the issue
1. add global plugin cors
```
curl -X PUT \
https://{apisix_listen_address}/apisix/admin/global_rules/1 \
-H 'Content-Type: application/json' \
-H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
-d '{
"plugins": {
"cors": { }
}
}'
```
2. Access a nonexistent Route, for example: http://172.0.0.1:9080/test
3. the code snippet
```
-- ### init.lua
function _M.http_access_phase()
.....
-- load and run global rule
if router.global_rules and router.global_rules.values
and #router.global_rules.values > 0 then
local plugins = core.tablepool.fetch("plugins", 32, 0)
local values = router.global_rules.values
for _, global_rule in config_util.iterate_values(values) do
api_ctx.conf_type = "global_rule"
api_ctx.conf_version = global_rule.modifiedIndex
api_ctx.conf_id = global_rule.value.id
core.table.clear(plugins)
api_ctx.plugins = plugin.filter(global_rule, plugins)
run_plugin("rewrite", plugins, api_ctx)
run_plugin("access", plugins, api_ctx)
end
core.tablepool.release("plugins", plugins)
-- Note: api_ctx is cleared here
api_ctx.plugins = nil
api_ctx.conf_type = nil
api_ctx.conf_version = nil
api_ctx.conf_id = nil
api_ctx.global_rules = router.global_rules
end
.....
-- Note: here no route matched, so exit 404
local route = api_ctx.matched_route
if not route then
return core.response.exit(404,
{error_msg = "failed to match any routes"})
end
...
end
-- Continue with the following code
function _M.http_header_filter_phase()
common_phase("header_filter")
end
local function common_phase(phase_name)
...
if api_ctx.global_rules then
local plugins = core.tablepool.fetch("plugins", 32, 0)
local values = api_ctx.global_rules.values
for _, global_rule in config_util.iterate_values(values) do
core.table.clear(plugins)
plugins = plugin.filter(global_rule, plugins)
-- Note: here run global plugin
run_plugin(phase_name, plugins, api_ctx)
end
core.tablepool.release("plugins", plugins)
end
...
end
local function run_plugin(phase, plugins, api_ctx)
.....
for i = 1, #plugins, 2 do
local phase_fun = plugins[i][phase]
if phase_fun then
phase_fun(plugins[i + 1], api_ctx)
end
end
.....
end
-- ### cors.lua
function _M.header_filter(conf, ctx)
.....
-- Note: here the ctx is api_ctx
local multiple_origin, err = core.lrucache.plugin_ctx(plugin_name, ctx,
create_mutiple_origin_cache,
conf)
.....
end
-- ### lrucache.lua
function _M.plugin_ctx(plugin_name, api_ctx, create_obj_fun, ...)
-- Note: here used api_ctx.conf_id is nil
local key = api_ctx.conf_type .. "#" .. api_ctx.conf_id
return _plugin(plugin_name, key, api_ctx.conf_version, create_obj_fun,
...)
end
```
### What's the actual result? (including assertion message & call stack if
applicable)
2020/09/09 11:37:14 [error] 9701#9701: *373 failed to run
header_filter_by_lua*:
...user-center//deps/share/lua/5.1/apisix/core/lrucache.lua:202: attempt to
concaten ate field 'conf_id' (a nil value)
2297217 stack traceback:
2297218 ...user-center//deps/share/lua/5.1/apisix/core/lrucache.lua:202:
in function 'plugin_ctx'
2297219 ...-user-center//deps/share/lua/5.1/apisix/plugins/cors.lua:144:
in function 'phase_fun'
2297220 ...odes/dfy-user-center//deps/share/lua/5.1/apisix/init.lua:142:
in function 'run_plugin'
2297221 ...odes/dfy-user-center//deps/share/lua/5.1/apisix/init.lua:562:
in function 'common_phase'
2297222 ...odes/dfy-user-center//deps/share/lua/5.1/apisix/init.lua:573:
in function 'http_header_filter_phase'
2297223 header_filter_by_lua:2: in main chunk, client: 172.30.212.206,
server: , request: "GET /dfy-h5-user HTTP/1.0", host: "m.dafangya.com.cn"
### What's the expected result?
404 page.
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]