Skip to content

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

Description

@kingluo

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):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions