Skip to content
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

bug: timer leak in request-id plugin when reloaded #8520

Closed
kingluo opened this issue Dec 14, 2022 · 0 comments · Fixed by #8556
Closed

bug: timer leak in request-id plugin when reloaded #8520

kingluo opened this issue Dec 14, 2022 · 0 comments · Fixed by #8556

Comments

@kingluo
Copy link
Contributor

kingluo commented Dec 14, 2022

Current Behavior

The timer created by this plugin is only destroyed when snowflake_inited is true.
But on some conditions, i.e. etcd_cli:keepalive(lease_id), snowflake_inited would be set to nil but keep the timer running, then plugin reload would cause a timer leak.

function _M.init()
...
    if attr.snowflake.enable then
        if process.type() == "worker" then
            ngx.timer.at(0, snowflake_init)
        end
    end
end

function _M.destroy()
    if snowflake_inited then
        timers.unregister_timer("plugin#request-id")
    end
end

local function snowflake_init()
    if snowflake_inited == nil then
...
        data_machine = gen_data_machine(max_number)
        if data_machine == nil then
            return ""
        end

...
        snowflake_inited = true
    end
end

local function gen_data_machine(max_number)
    if data_machine == nil then
...
                local handler = function()
...
                    local _, err4 = etcd_cli:keepalive(lease_id)
                    if err4 then
                        snowflake_inited = nil
                        data_machine = nil
                        core.log.error("snowflake data_machine: " .. id .." lease failed.")
                    end
                    start_at = now
                    core.log.info("snowflake data_machine: " .. id .." lease success.")
                end

                timers.register_timer("plugin#request-id", handler)
...
                break
            end
        end

        if data_machine == nil then
            core.log.error("No data_machine is not available")
            return nil
        end
    end
    return data_machine
end

Expected Behavior

No response

Error Logs

No response

Steps to Reproduce

curl http://127.0.0.1:9180/apisix/admin/plugins/reload -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT

Environment

  • APISIX version (run apisix version): master branch
  • Operating system (run uname -a):
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info):
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant