You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2020/12/11 01:57:57 UTC

[GitHub] [apisix] griffenliu opened a new issue #1667: bug: global-rule error when enable_debug=true

griffenliu opened a new issue #1667:
URL: https://github.com/apache/apisix/issues/1667


   ### Issue description
   I customized a plug-in for global rules,When I set the configuration enable_debug=true, i got the follow error:
   ```
   2020/06/08 11:40:56 [error] 15825#0: *330 failed to run body_filter_by_lua*: /usr/local/share/lua/5.1/apisix/plugin.lua:260: headers have already been sent
   stack traceback:
           [C]: in function 'error'
           /usr/local/share/lua/5.1/apisix/core/response.lua:90: in function 'set_header'
           /usr/local/share/lua/5.1/apisix/plugin.lua:260: in function 'filter'
           /usr/local/share/lua/5.1/apisix/init.lua:456: in function 'common_phase'
           /usr/local/share/lua/5.1/apisix/init.lua:470: in function 'http_body_filter_phase'
           body_filter_by_lua:2: in main chunk, client: ::1, server: , request: "GET /config HTTP/1.1", host: "localhost:9080"
   
   ```
   
   ### Environment
   
   * apisix version (cmd: `apisix version`): 1.2
   * OS: Ubuntu 18.04
   
   ### Minimal test code / Steps to reproduce the issue
   1. config.yaml
   ```
   apisix:
        enable_debug: true
   
   plugins:
       - my_custom_plugin
   ```
   2. my_custom_plugin.lua
   ```
   local core = require("apisix.core")
   
   local plugin_name = "my_custom_plugin"
   local schema = {}
   
   local _M = {
       version = 0.1,
       priority = 0,
       name = plugin_name,
       schema = schema,
   }
   
   function _M.access(conf, ctx)
   --    core.response.exit(200, "测试!!!")
   --    return 200, "测试!!!"
   end
   return _M
   ```
   3. set global rule:
   ```
   curl -X PUT \
     http://localhost:9080/apisix/admin/global_rules/1 \
     -H 'Content-Type: application/json' \
     -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
     -d '{
           "plugins": {
               "my_custom_plugin": {}
           }
       }'
   
   ```
   
   ### What's the actual result? (including assertion message & call stack if applicable)
   ```
   ➜  ~ curl http://localhost:9080/config
   curl: (52) Empty reply from server
   ```
   
   ### What's the expected result?
   ➜  ~ curl http://localhost:9080/config
   {"error_msg":"failed to match any routes"}
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [apisix] membphis commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis commented on issue #1667:
URL: https://github.com/apache/apisix/issues/1667#issuecomment-742957939


   it seems that this issue was fixed by PR: https://github.com/apache/apisix/pull/1782/files


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [apisix] membphis commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis commented on issue #1667:
URL: https://github.com/apache/apisix/issues/1667#issuecomment-742914726


   comment: https://github.com/apache/apisix/pull/2029#discussion_r540632723
   
   we should use this way to fix this issue, so I reopen this issue and welcome PR to fix it


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [apisix] spacewander closed issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
spacewander closed issue #1667:
URL: https://github.com/apache/apisix/issues/1667


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #1667:
URL: https://github.com/apache/apisix/issues/1667#issuecomment-743101721


   @membphis 
   😢 I already wrote one right now.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [apisix] membphis commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis commented on issue #1667:
URL: https://github.com/apache/apisix/issues/1667#issuecomment-671012353


   @nizq @griffenliu I submitted a PR to fix this issue, you can make a try with it


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [apisix] griffenliu closed issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
griffenliu closed issue #1667:
URL: https://github.com/apache/apisix/issues/1667


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #1667:
URL: https://github.com/apache/apisix/issues/1667#issuecomment-742958242


   The PR doesn't get merged. Not sure why the author closed it.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [apisix] membphis commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis commented on issue #1667:
URL: https://github.com/apache/apisix/issues/1667#issuecomment-743091879


   I reopen it ^_^


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] griffenliu commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
griffenliu commented on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-647389361


   ok, tks, i will try.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [apisix] membphis commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis commented on issue #1667:
URL: https://github.com/apache/apisix/issues/1667#issuecomment-742958008


   @spacewander do you have time to look at this issue?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] moonming commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
moonming commented on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-654667156


   @nizq @griffenliu Is this patch works for you?


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] membphis commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis commented on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-647066033


   I made a try with your steps, but I got `{"error_msg":"failed to match any routes"}` finally, it seems right.
   
   I think your ENV is dirty. you can clean the data in etcd and call `make init` to initialize your etcd.
   
   Then you can make a try with your steps.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] membphis commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis commented on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-650519165


   @nizq I made a try with your example, it works fine. if you still have a problem, you can create a new issue with more detailed information, eg: your apisix version, fully steps etc.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] griffenliu edited a comment on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
griffenliu edited a comment on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-647389361


   ok, tks, i will try again.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] membphis commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis commented on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-652140810


   many thanks to your PR, and I confirmed it should be a bug.
   
   I write a patch for this bug. You can take a look at this, and we need some test cases to confirm we fixed this bug:
   
   ```diff
   diff --git a/apisix/init.lua b/apisix/init.lua
   index 41295f0..3ecb999 100644
   --- a/apisix/init.lua
   +++ b/apisix/init.lua
   @@ -252,7 +252,7 @@ function _M.http_access_phase()
                api_ctx.conf_id = global_rule.value.id
   
                core.table.clear(plugins)
   -            api_ctx.plugins = plugin.filter(global_rule, plugins)
   +            api_ctx.plugins = plugin.filter(global_rule, plugins, true)
                run_plugin("rewrite", plugins, api_ctx)
                run_plugin("access", plugins, api_ctx)
            end
   @@ -445,8 +445,7 @@ local function common_phase(phase_name)
            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)
   +            plugins = plugin.filter(global_rule, plugins, true)
                run_plugin(phase_name, plugins, api_ctx)
            end
            core.tablepool.release("plugins", plugins)
   diff --git a/apisix/plugin.lua b/apisix/plugin.lua
   index 075d058..8267ae1 100644
   --- a/apisix/plugin.lua
   +++ b/apisix/plugin.lua
   @@ -232,14 +232,35 @@ function _M.api_routes()
    end
   
   
   -function _M.filter(user_route, plugins)
   +local function set_response_header_by_debug_flag(plugins, dry_run)
   +    if dry_run then
   +        return
   +    end
   +
   +    if not local_conf or not local_conf.apisix.enable_debug then
   +        return
   +    end
   +
   +    if #plugins == 0 then
   +        core.response.set_header("Apisix-Plugins", "no plugin")
   +    end
   +
   +    local t = {}
   +    for i = 1, #plugins, 2 do
   +        core.table.insert(t, plugins[i].name)
   +    end
   +    core.response.set_header("Apisix-Plugins", core.table.concat(t, ", "))
   +end
   +
   +
   +function _M.filter(user_route, plugins, dry_run)
        plugins = plugins or core.table.new(#local_plugins * 2, 0)
   +    core.table.clear(plugins)
   +
        local user_plugin_conf = user_route.value.plugins
        if user_plugin_conf == nil or
           core.table.nkeys(user_plugin_conf) == 0 then
   -        if local_conf and local_conf.apisix.enable_debug then
   -            core.response.set_header("Apisix-Plugins", "no plugin")
   -        end
   +        set_response_header_by_debug_flag(plugins, dry_run)
            return plugins
        end
   
   @@ -253,14 +274,7 @@ function _M.filter(user_route, plugins)
            end
        end
   
   -    if local_conf.apisix.enable_debug then
   -        local t = {}
   -        for i = 1, #plugins, 2 do
   -            core.table.insert(t, plugins[i].name)
   -        end
   -        core.response.set_header("Apisix-Plugins", core.table.concat(t, ", "))
   -    end
   -
   +    set_response_header_by_debug_flag(plugins, dry_run)
        return plugins
    end
   ```


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] griffenliu commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
griffenliu commented on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-651482750


   I think i know the reason of this error(guess). Because no routes are set. So the request ends prematurely, causing the header information to fail to be set later. the code:
   ```
   # init.lua
   # _M.http_access_phase()
   if not route then
          return core.response.exit(404,
                     {error_msg = "failed to match any routes"})
   end
   
   # plugin.lua
   # _M.http_body_filter_phase()
   if local_conf.apisix.enable_debug then
           local t = {}
           for i = 1, #plugins, 2 do
               core.table.insert(t, plugins[i].name)
           end
           core.response.set_header("Apisix-Plugins", core.table.concat(t, ", "))
       end
   
   ```
   I don't know if that's a problem.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] membphis edited a comment on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis edited a comment on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-650519165


   @nizq I made a try with your example, it works fine. if you still have a problem, you can create a new issue with more detailed information, eg: your apisix version, full steps, etc. 


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] membphis closed issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
membphis closed issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] griffenliu edited a comment on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
griffenliu edited a comment on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-651482750


   I think i know the reason of this error(guess). 
   The reason is that no routes are set. Here is the code:
   ```
   # init.lua
   # _M.http_access_phase()
   if not route then
          return core.response.exit(404,
                     {error_msg = "failed to match any routes"})
   end
   
   # plugin.lua
   # _M.http_body_filter_phase()
   if local_conf.apisix.enable_debug then
           local t = {}
           for i = 1, #plugins, 2 do
               core.table.insert(t, plugins[i].name)
           end
           core.response.set_header("Apisix-Plugins", core.table.concat(t, ", "))
       end
   
   ```
   I don't know if that's a problem.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [incubator-apisix] nizq commented on issue #1667: bug: global-rule error when enable_debug=true

Posted by GitBox <gi...@apache.org>.
nizq commented on issue #1667:
URL: https://github.com/apache/incubator-apisix/issues/1667#issuecomment-650481752


   
   apisix version: master
   
   commond:
   
   ```sh
   curl http://localhost:9080/apisix/admin/global_rules/2 \
            -H "X-API-KEY: ${KEY}" \
            -X PUT \
            -d '
   {
     "plugins": {
       "skywalking": {
          "endpoint": "http://xx.xx.xx.xx:12800",
          "sample_ratio": 1,
          "service_name": "APISIX_SERVER"
       }
     }
   }'
   ```
   
   -------------
   ```
   2020/06/27 10:57:14 [error] 35#35: *971 failed to run body_filter_by_lua*: /usr/local/apisix/apisix/plugin.lua:260: headers have already been sent
   stack traceback:
   	[C]: in function 'error'
   	/usr/local/apisix/apisix/core/response.lua:90: in function 'set_header'
   	/usr/local/apisix/apisix/plugin.lua:260: in function 'filter'
   	/usr/local/apisix/apisix/init.lua:449: in function 'common_phase'
   	/usr/local/apisix/apisix/init.lua:465: in function 'http_body_filter_phase'
   	body_filter_by_lua:2: in main chunk while sending to client, client: 192.168.1.108, server: , request: "GET / HTTP/1.1", upstream: "http://172.16.123.91:80/", host: "192.168.1.108:9080"
   2020/06/27 10:57:14 [error] 35#35: *971 failed to run log_by_lua*: /usr/local/apisix/apisix/plugin.lua:260: headers have already been sent
   stack traceback:
   	[C]: in function 'error'
   	/usr/local/apisix/apisix/core/response.lua:90: in function 'set_header'
   	/usr/local/apisix/apisix/plugin.lua:260: in function 'filter'
   	/usr/local/apisix/apisix/init.lua:449: in function 'common_phase'
   	/usr/local/apisix/apisix/init.lua:471: in function 'http_log_phase'
   	log_by_lua(nginx.conf:284):2: in main chunk while logging request, client: 192.168.1.108, server: , request: "GET / HTTP/1.1", upstream: "http://172.16.123.91:80/", host: "192.168.1.108:9080"
   ```


----------------------------------------------------------------
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:
users@infra.apache.org