openresty / lua-resty-core

New FFI-based API for lua-nginx-module
792 stars 270 forks source link

ngx.shared.DICT:ttl() returns 0 for the exipred key #453

Open chobits opened 6 months ago

chobits commented 6 months ago

Hi guys,

I found in some scenario, the ngx.shared.DICT:ttl(key) will return 0 (key will never expire) for the expired key. Is this an intentional design or a bug?


The return value 0 of ngx.shared.DICT:ttl(key) in lua-nginx-module doc means this key would never expire.

A value of 0 means that the item will never expire.

But sometimes if the key expires, it still returns 0 value

Steps to reproduce

  1. source
$ cat ttl_0.sh
resty --shdict "hello 10m" -e '
local dict = ngx.shared.hello

dict:set("foo", "bar", 0.002)

for i = 1, 2 do
  ngx.sleep(0.001)
  local ttl = dict:ttl("foo")
  print(ttl)
  assert(ttl ~= 0)
end
'
  1. run it mutiple times, you'll get the assertion failure
$ sh ttl_0.sh
0.001
0
ERROR: (command line -e):10: assertion failed!
stack traceback:
        (command line -e):10: in function 'inline_gen'
        init_worker_by_lua(nginx.conf:142):44: in function <init_worker_by_lua(nginx.conf:142):43>
        [C]: in function 'xpcall'
        init_worker_by_lua(nginx.conf:142):52: in function <init_worker_by_lua(nginx.conf:142):50>

 $ resty -v
resty 0.29
nginx version: openresty/1.21.4.3 (no pool)
built with OpenSSL 3.1.4 24 Oct 2023
TLS SNI support enabled
configure arguments: --prefix=/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/bazel-out/darwin_arm64-fastbuild/bin/external/openresty/openresty.build_tmpdir/openresty/nginx --with-debug --with-cc-opt='-DNGX_LUA_USE_ASSERT -DNGX_LUA_ABORT_AT_PANIC -O2 -I/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/bazel-out/darwin_arm64-fastbuild/bin/external/openresty/openresty.ext_build_deps/pcre/include -I/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/bazel-out/darwin_arm64-fastbuild/bin/external/openresty/openresty.ext_build_deps/openssl/include -I/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/bazel-out/darwin_arm64-fastbuild/bin/external/openresty/openresty.ext_build_deps/luajit/include -DNGX_LUA_USE_ASSERT -DNGX_LUA_ABORT_AT_PANIC -g -O0 -DNGX_WASM_HOST_PROPERTY_NAMESPACE=kong' --add-module=../ngx_devel_kit-0.3.2 --add-module=../echo-nginx-module-0.63 --add-module=../ngx_lua-0.10.25 --add-module=../headers-more-nginx-module-0.34 --add-module=../ngx_stream_lua-0.0.13 --with-ld-opt='-Wl,-rpath,/Users/xc/work/dev/kong/bazel-bin/build/kong-dev/openresty/luajit/lib -L/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/bazel-out/darwin_arm64-fastbuild/bin/external/openresty/openresty.ext_build_deps/pcre/lib -L/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/bazel-out/darwin_arm64-fastbuild/bin/external/openresty/openresty.ext_build_deps/openssl/lib -L/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/bazel-out/darwin_arm64-fastbuild/bin/external/openresty/openresty.ext_build_deps/luajit/lib -Wl,-rpath,/Users/xc/work/dev/kong/bazel-bin/build/kong-dev/kong/lib -Wl,-rpath,/Users/xc/work/dev/kong/bazel-bin/build/kong-dev/openresty/lualib' --with-pcre-jit --with-http_ssl_module --with-http_sub_module --with-http_realip_module --with-http_stub_status_module --with-http_v2_module --with-stream_realip_module --with-stream_ssl_preread_module --add-module=/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/external/lua-kong-nginx-module --add-module=/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/external/lua-kong-nginx-module/stream --add-module=/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/external/lua-resty-lmdb --add-module=/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/external/lua-resty-events --with-compat --add-dynamic-module=/private/var/tmp/_bazel_xc/e6d31d9d1855f25ff93b432735e02c28/execroot/kong/external/ngx_wasm_module --with-stream --with-stream_ssl_module

You can (temporarily) enable the nginx debugging logs to see the internal workings of NGINX in your nginx''s error.log file. See http://nginx.org/en/docs/debugging_log.html The same instructions apply equally well to OpenResty.

If you are seeing crashes, please provide the full backtrace for the crash. See https://www.nginx.com/resources/wiki/start/topics/tutorials/debugging/#core-dump for more details.

Thanks for your cooperation.