]> source.dussan.org Git - rspamd.git/commitdiff
[Fix] Fix an old issue with order of destruction race between redis pool and lua
authorVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 25 May 2023 13:41:30 +0000 (14:41 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Thu, 25 May 2023 13:41:30 +0000 (14:41 +0100)
src/libserver/redis_pool.cxx
src/lua/lua_common.c
src/lua/lua_common.h
src/lua/lua_redis.c

index 19acaa8283fa3c8dbaea0dd82cfb7958bcfc223a..0bbfa55de8aea9db129444351912b6fc983af2aa 100644 (file)
@@ -244,13 +244,13 @@ public:
                conns_by_ctx.emplace(ctx, conn);
        }
 
-       ~redis_pool() {
-               /*
-                * XXX: this will prevent hiredis to unregister connections that
-                * are already destroyed during redisAsyncFree...
-                */
+       /* Hack to prevent Redis callbacks to be executed */
+       auto prepare_to_die() -> void
+       {
                wanna_die = true;
        }
+
+       ~redis_pool() {}
 };
 
 
@@ -612,6 +612,7 @@ rspamd_redis_pool_destroy(void *p)
 {
        auto *pool = reinterpret_cast<class rspamd::redis_pool *>(p);
 
+       pool->prepare_to_die();
        delete pool;
 }
 
index db7e284eea09f11ddcbb5f5535e20ff7169f0c4b..7f4453c57d283ce1a3b42651e8f2abe73995d415 100644 (file)
@@ -918,6 +918,8 @@ rspamd_lua_wipe_realloc (void *ud,
 extern int luaopen_bit(lua_State *L);
 #endif
 
+static bool lua_initialized = false;
+
 lua_State *
 rspamd_lua_init (bool wipe_mem)
 {
@@ -1039,6 +1041,8 @@ rspamd_lua_init (bool wipe_mem)
        lua_setglobal (L, "get_traces");
 #endif
 
+       lua_initialized = true;
+
        return L;
 }
 
@@ -1063,6 +1067,14 @@ rspamd_lua_close (lua_State *L)
        DL_DELETE(rspamd_lua_global_ctx, ctx);
        kh_destroy(lua_class_set, ctx->classes);
        g_free(ctx);
+
+       lua_initialized = false;
+}
+
+bool
+rspamd_lua_is_initialised(void)
+{
+       return lua_initialized;
 }
 
 void
index b782b3d37af9a637f50f9dd14c40245b2a920545..2ea51249d4c5aa441040dc8bde291b6e6a3d17b9 100644 (file)
@@ -647,6 +647,12 @@ gchar *rspamd_lua_get_module_name (lua_State *L);
 bool rspamd_lua_universal_pcall (lua_State *L, gint cbref, const gchar* strloc,
                                                                 gint nret, const gchar *args, GError **err, ...);
 
+/**
+ * Returns true if lua is initialised
+ * @return
+ */
+bool rspamd_lua_is_initialised(void);
+
 /**
 * Wrapper for lua_geti from lua 5.3
 * @param L
index 8b72a52da8167a1468c1d15c91757227427c6459..99fc383b58e948f1d99b8e636018d3f6d79cd5dd 100644 (file)
@@ -460,7 +460,7 @@ lua_redis_callback (redisAsyncContext *c, gpointer r, gpointer priv)
        ctx = sp_ud->ctx;
        ud = sp_ud->c;
 
-       if (ud->terminated) {
+       if (ud->terminated || !rspamd_lua_is_initialised()) {
                /* We are already at the termination stage, just go out */
                return;
        }