Skip to content

Commit a3ec156

Browse files
authored
bugfix: should reset the value_len to 0 when reuse the expired list type key in shared dict. (#1928)
1 parent 5095da4 commit a3ec156

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/ngx_http_lua_shdict.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ ngx_http_lua_shdict_push_helper(lua_State *L, int flags)
815815
"type matched, reusing it");
816816

817817
sd->expires = 0;
818-
818+
sd->value_len = 0;
819819
/* free list nodes */
820820

821821
queue = ngx_http_lua_shdict_get_list_head(sd, key.len);

t/145-shdict-list.t

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,3 +743,87 @@ GET /test
743743
done
744744
--- no_error_log
745745
[error]
746+
747+
748+
749+
=== TEST 20: push to an expired list
750+
--- http_config
751+
lua_shared_dict dogs 1m;
752+
--- config
753+
location = /test {
754+
content_by_lua_block {
755+
local dogs = ngx.shared.dogs
756+
local len, err = dogs:lpush("cc", "1") --add another list to avoid key"aa" be cleaned (run ngx_http_lua_shdict_expire(ctx, 1) may clean key ,ensure key'aa' not clean ,just expired))
757+
if not len then
758+
ngx.say("push cc err: ", err)
759+
end
760+
local len, err = dogs:lpush("aa", "1")
761+
if not len then
762+
ngx.say("push1 err: ", err)
763+
end
764+
local succ, err = dogs:expire("aa", 0.2)
765+
if not succ then
766+
ngx.say("expire err: ",err)
767+
end
768+
ngx.sleep(0.3) -- list aa expired
769+
local len, err = dogs:lpush("aa", "2") --push to an expired list may set as a new list
770+
if not len then
771+
ngx.say("push2 err: ", err)
772+
end
773+
local len, err = dogs:llen("aa") -- new list len is 1
774+
if not len then
775+
ngx.say("llen err: ", err)
776+
else
777+
ngx.say("aa:len :", dogs:llen("aa"))
778+
end
779+
}
780+
}
781+
782+
--- request
783+
GET /test
784+
--- response_body
785+
aa:len :1
786+
--- no_error_log
787+
[error]
788+
789+
790+
791+
=== TEST 21: push to an expired list then pop many time (more then list len )
792+
--- http_config
793+
lua_shared_dict dogs 1m;
794+
--- config
795+
location = /test {
796+
content_by_lua_block {
797+
local dogs = ngx.shared.dogs
798+
local len, err = dogs:lpush("cc", "1") --add another list to avoid key"aa" be cleaned (run ngx_http_lua_shdict_expire(ctx, 1) may clean key ,ensure key'aa' not clean ,just expired))
799+
if not len then
800+
ngx.say("push cc err: ", err)
801+
end
802+
local len, err = dogs:lpush("aa", "1")
803+
if not len then
804+
ngx.say("push1 err: ", err)
805+
end
806+
local succ, err = dogs:expire("aa", 0.2)
807+
if not succ then
808+
ngx.say("expire err: ",err)
809+
end
810+
ngx.sleep(0.3) -- list aa expired
811+
local len, err = dogs:lpush("aa", "2") --push to an expired list may set as a new list
812+
if not len then
813+
ngx.say("push2 err: ", err)
814+
end
815+
local val, err = dogs:lpop("aa")
816+
if not val then
817+
ngx.say("llen err: ", err)
818+
end
819+
local val, err = dogs:lpop("aa") -- val == nil
820+
ngx.say("aa list value: ", val)
821+
}
822+
}
823+
824+
--- request
825+
GET /test
826+
--- response_body
827+
aa list value: nil
828+
--- no_error_log
829+
[error]

0 commit comments

Comments
 (0)