openresty / openresty/lua-resty-core

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

Open
#453 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lua
Stars
853
Forks
286
Avg merge
35m
Merged PRs (30d)
1

Description

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>

  • The exact version of the related software, including but not limited to the OpenResty version
    (if any), the NGINX core version, the ngx_lua module version(via openresty -V or nginx -V),
    and the lua-resty-core version(via resty -e 'print(require("resty.core").version)'),
    and your operating system version(via uname -a).
 $ 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
  • A minimal and standalone test case that others can easily run on their side and
    reproduce the issue you are seeing.
  • Do not simply say "something is broken" or "something does not work". Always provide
    as much details as possible. Always describe the symptoms and your expected results.

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ngx.shared.DICT:ttl entry and run the standalone ttl_0.sh reproduction repeatedly to observe the boundary behavior. Trace the ttl entry point and existing shared-dictionary tests, if available, to determine the expected result for an expired key. Done means the documented distinction between an expired key and a non-expiring key is preserved and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.