From 442e73ea092905cef8f25bf0ac883e90f0666e89 Mon Sep 17 00:00:00 2001 From: "Meir Shpilraien (Spielrein)" Date: Tue, 10 May 2022 11:47:45 +0300 Subject: [PATCH] Fix #10705, avoid relinking the same library twice. (#10706) Set `old_li` to NULL to avoid linking it again on error. Before the fix, loading an already existing library will cause the existing library to be added again. This cause not harm other then wrong statistics. The statistics that are effected by the issue are: * `libraries_count` and `functions_count` returned by `function stats` command * `used_memory_functions` returned on `info memory` command * `functions.caches` returned on `memory stats` command --- src/functions.c | 1 + tests/unit/functions.tcl | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/functions.c b/src/functions.c index 19d0d0c11e..c40761865e 100644 --- a/src/functions.c +++ b/src/functions.c @@ -963,6 +963,7 @@ sds functionsCreateWithLibraryCtx(sds code, int replace, sds* err, functionsLibC old_li = dictFetchValue(lib_ctx->libraries, md.name); if (old_li && !replace) { + old_li = NULL; *err = sdscatfmt(sdsempty(), "Library '%S' already exists", md.name); goto error; } diff --git a/tests/unit/functions.tcl b/tests/unit/functions.tcl index 62c070f906..e9aeda16bf 100644 --- a/tests/unit/functions.tcl +++ b/tests/unit/functions.tcl @@ -1141,6 +1141,23 @@ start_server {tags {"scripting"}} { r function stats } {running_script {} engines {LUA {libraries_count 1 functions_count 1}}} + test {FUNCTION - test function stats on loading failure} { + r FUNCTION FLUSH + + r FUNCTION load {#!lua name=test1 + redis.register_function('f1', function() return 1 end) + redis.register_function('f2', function() return 1 end) + } + + catch {r FUNCTION load {#!lua name=test1 + redis.register_function('f3', function() return 1 end) + }} e + assert_match "*Library 'test1' already exists*" $e + + + r function stats + } {running_script {} engines {LUA {libraries_count 1 functions_count 2}}} + test {FUNCTION - function stats cleaned after flush} { r function flush r function stats