BUG/MEDIUM: lua-thread: some parts must be initialized once
Lua dedicated TCP, HTTP and SSL socket and proxies must be initialized
once. Right now, they are initialized from the Lua init state, but since
commit 59f11be43 ("MEDIUM: lua-thread: Add the lua-load-per-thread
directive") this function is called one time per lua context. This
caused some fields to be cleared and overwritten, and pre-allocated
object to be lost. This is why the address sanitizer detected memory
leaks from the socket_ssl server initialization.
Let's move all the state-independent part of the function to the
hlua_init() function to avoid this.
No backport is needed, this is only 2.4-dev.
diff --git a/src/hlua.c b/src/hlua.c
index 32ea6c9..66d0b7b 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -8675,17 +8675,6 @@
void **context;
lua_State *L;
struct prepend_path *pp;
-#ifdef USE_OPENSSL
- struct srv_kw *kw;
- int tmp_error;
- char *error;
- char *args[] = { /* SSL client configuration. */
- "ssl",
- "verify",
- "none",
- NULL
- };
-#endif
/* Init main lua stack. */
L = lua_newstate(hlua_alloc, &hlua_global_allocator);
@@ -9136,6 +9125,40 @@
/* Register previous table in the registry with reference and named entry. */
class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
+ lua_atpanic(L, hlua_panic_safe);
+
+ return L;
+}
+
+void hlua_init(void) {
+ int i;
+ int idx;
+#ifdef USE_OPENSSL
+ struct srv_kw *kw;
+ int tmp_error;
+ char *error;
+ char *args[] = { /* SSL client configuration. */
+ "ssl",
+ "verify",
+ "none",
+ NULL
+ };
+#endif
+
+ /* Init post init function list head */
+ for (i = 0; i < MAX_THREADS + 1; i++)
+ LIST_INIT(&hlua_init_functions[i]);
+
+ /* Init state for common/shared lua parts */
+ hlua_state_id = 0;
+ ha_set_tid(0);
+ hlua_states[0] = hlua_init_state(0);
+
+ /* Init state 1 for thread 0. We have at least one thread. */
+ hlua_state_id = 1;
+ ha_set_tid(0);
+ hlua_states[1] = hlua_init_state(1);
+
/* Proxy and server configuration initialisation. */
memset(&socket_proxy, 0, sizeof(socket_proxy));
init_new_proxy(&socket_proxy);
@@ -9260,27 +9283,6 @@
}
#endif
- lua_atpanic(L, hlua_panic_safe);
-
- return L;
-}
-
-void hlua_init(void) {
- int i;
-
- /* Init post init function list head */
- for (i = 0; i < MAX_THREADS + 1; i++)
- LIST_INIT(&hlua_init_functions[i]);
-
- /* Init state for common/shared lua parts */
- hlua_state_id = 0;
- ha_set_tid(0);
- hlua_states[0] = hlua_init_state(0);
-
- /* Init state 1 for thread 0. We have at least one thread. */
- hlua_state_id = 1;
- ha_set_tid(0);
- hlua_states[1] = hlua_init_state(1);
}
static void hlua_deinit()