api7 / lua-resty-etcd

Nonblocking Lua etcd driver library for OpenResty

Home Page:https://api7.ai/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"lua tcp socket read time out" keeps flushing and "worker is shutting down" after reloading

Ben0625 opened this issue · comments

I'm trying to write a simple timer function to watch a specified key in etcd.
It seems that this funtion can work: When I change the value of key in etcd, it will get the changes as expected. But when there is no changes, "lua tcp socket read time out" keeps flushing in error.log.

After I execute openresty -s reload, the previous worker process will not exit and keep the status "worker process is shutting down" for a very long time (I even do not know if it will keep this status forever)

Here is part of the code (Sorry that I cannot handle the style well...):
watch.lua:
local function watch(premature, self)
if premature then
return
end
local opts = {}
opts["protocol"] = config["etcd"][env]["protocol"]
opts["http_host"] = config["etcd"][env]["http_host"]

local cli, callback, err
cli, err = etcd.new(opts)
if err then
    ngx.log(ngx.ERR, "new etcd err: " .. err)
    ngx.timer.at(5, watch, self)
    return
end
local attr = {}
attr["timeout"] = 3
callback, err = cli:watchdir(config["etcd"][env]["watch_prefix"], attr)
if err then
    ngx.log(ngx.ERR, "get key error")
    ngx.timer.at(5, watch, self)
    return
end
local created_true = callback()
local watch_res = callback()
if watch_res then
    ngx.log(ngx.INFO, serializeTable(watch_res))
end
ngx.sleep(0.5)
ngx.timer.at(0, watch, self)

end

nginx.conf:
init_worker_by_lua_block{
local w = require("watch");
w:init();
}

you can make a try with this directive: http://nginx.org/en/docs/ngx_core_module.html#worker_shutdown_timeout

I add it in nginx.conf at the beginning, but it seems that it does not work......

worker_processes 1;
error_log logs/error.log info;
worker_shutdown_timeout 5s;

events {
worker_connections 1024;
}
http {
init_worker_by_lua_block{
local w = require("watch");
w:init();
}
}

By the way, I find that "lua tcp socket read time out" can be turned off, so this is solved.

you can make a try with this directive: http://nginx.org/en/docs/ngx_core_module.html#worker_shutdown_timeout

I also try to use watchcancel:
local res, err, http_cli = cli:watch('/path/to/key', {need_cancel = true})
res = cli:watchcancel(http_cli)

but http_cli is always nil which is very confusing......

https://groups.google.com/g/openresty/c/JmPf9HtdYwk/m/pipGQ67sEQAJ
I check this discuss and put the check in code:

if ngx.worker.exiting() then
return
end

before recursively creating a new timer, then there is no process in "worker is shutting down" status.
Thanks membphis for quickly responsing.