[Tarantool-patches] [PATCH] func: clean the module cache on first load error

Cyrill Gorcunov gorcunov at gmail.com
Fri Nov 13 15:28:02 MSK 2020


In case if we're loading a fresh module we put it
into a module's cache first which allows us to not
reload same module twice (say there could be several
functions in same module).

But if the module is loaded for the first time and
symbol resolution failed we continue keeping this
module loaded even if there may be no more use of
it. Thus make a cleanup if needed.

There is no portable way to verify via test as far
as I know, just manually via "lsof -p `pidof tarantool`".

Fixes #5475

Reported-by: Vladislav Shpilevoy <v.shpilevoy at tarantool.org>
Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
 src/box/func.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/src/box/func.c b/src/box/func.c
index 8087c953f..9909cee45 100644
--- a/src/box/func.c
+++ b/src/box/func.c
@@ -528,10 +528,9 @@ func_c_load(struct func_c *func)
 	struct func_name name;
 	func_split_name(func->base.def->name, &name);
 
-	struct module *module = module_cache_find(name.package,
-						  name.package_end);
-	if (module == NULL) {
-		/* Try to find loaded module in the cache */
+	struct module *cached, *module;
+	cached = module_cache_find(name.package, name.package_end);
+	if (cached == NULL) {
 		module = module_load(name.package, name.package_end);
 		if (module == NULL)
 			return -1;
@@ -539,11 +538,27 @@ func_c_load(struct func_c *func)
 			module_delete(module);
 			return -1;
 		}
+	} else {
+		module = cached;
 	}
 
 	func->func = module_sym(module, name.sym);
-	if (func->func == NULL)
+	if (func->func == NULL) {
+		if (cached == NULL) {
+			/*
+			 * In case if it was a first load we should
+			 * clean the cache immediately otherwise
+			 * the module continue being referenced even
+			 * if there will be no use of it.
+			 *
+			 * Note the module_sym set an error thus be
+			 * careful to not wipe it.
+			 */
+			module_cache_del(name.package, name.package_end);
+			module_delete(module);
+		}
 		return -1;
+	}
 	func->module = module;
 	rlist_add(&module->funcs, &func->item);
 	return 0;
-- 
2.26.2



More information about the Tarantool-patches mailing list