Skip to content

Commit

Permalink
Fix LRU crash when getting too many random lua scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Seungmin Lee <[email protected]>
  • Loading branch information
Seungmin Lee committed Nov 15, 2024
1 parent 86f33ea commit 81e4cd1
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ void scriptingInit(int setup) {
* sha with the dictionary, so free fn is not set. */
lctx.lua_scripts = dictCreate(&shaScriptObjectDictType);
lctx.lua_scripts_lru_list = listCreate();
listSetFreeMethod(lctx.lua_scripts_lru_list, (void (*)(void *))sdsfree);
lctx.lua_scripts_mem = 0;

luaRegisterServerAPI(lua);
Expand Down Expand Up @@ -517,10 +518,7 @@ void luaDeleteFunction(client *c, sds sha) {
/* Delete the script from server. */
dictEntry *de = dictUnlink(lctx.lua_scripts, sha);
serverAssertWithInfo(c ? c : lctx.lua_client, NULL, de);
luaScript *l = dictGetVal(de);
/* We only delete `EVAL` scripts, which must exist in the LRU list. */
serverAssert(l->node);
listDelNode(lctx.lua_scripts_lru_list, l->node);
luaScript *l = dictGetVal(de);
lctx.lua_scripts_mem -= sdsAllocSize(sha) + getStringObjectSdsUsedMemory(l->body);
dictFreeUnlinkedEntry(lctx.lua_scripts, de);
}
Expand Down Expand Up @@ -549,11 +547,12 @@ listNode *luaScriptsLRUAdd(client *c, sds sha, int evalsha) {
listNode *ln = listFirst(lctx.lua_scripts_lru_list);
sds oldest = listNodeValue(ln);
luaDeleteFunction(c, oldest);
listDelNode(lctx.lua_scripts_lru_list, ln);
server.stat_evictedscripts++;
}

/* Add current. */
listAddNodeTail(lctx.lua_scripts_lru_list, sha);
listAddNodeTail(lctx.lua_scripts_lru_list, sdsdup(sha));
return listLast(lctx.lua_scripts_lru_list);
}

Expand Down

0 comments on commit 81e4cd1

Please sign in to comment.