BUG/MEDIUM: debug/lua: Use internal hlua function to dump the lua traceback
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 24 Mar 2021 13:52:24 +0000 (14:52 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 24 Mar 2021 16:08:49 +0000 (17:08 +0100)
The commit reverts following commits:
  * 83926a04 BUG/MEDIUM: debug/lua: Don't dump the lua stack if not dumpable
  * a61789a1 MEDIUM: lua: Use a per-thread counter to track some non-reentrant parts of lua

Instead of relying on a Lua function to print the lua traceback into the
debugger, we are now using our own internal function (hlua_traceback()).
This one does not allocate memory and use a chunk instead. This avoids any
issue with a possible deadlock in the memory allocator because the thread
processing was interrupted during a memory allocation.

This patch relies on the commit "BUG/MEDIUM: debug/lua: Use internal hlua
function to dump the lua traceback". Both must be backported wherever the
patches above are backported, thus as far as 2.0

(cherry picked from commit cc2c4f8f4c1d8613b481d1b346e083a9d2462811)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

include/haproxy/hlua.h
src/debug.c
src/hlua.c

index 20b0b45..b4e56d4 100644 (file)
@@ -51,7 +51,6 @@ void hlua_applet_tcp_fct(struct appctx *ctx);
 void hlua_applet_http_fct(struct appctx *ctx);
 struct task *hlua_process_task(struct task *task, void *context, unsigned short state);
 
-extern THREAD_LOCAL unsigned int hlua_not_dumpable;
 #else /* USE_LUA */
 
 /************************ For use when Lua is disabled ********************/
index a47fe10..36c822c 100644 (file)
@@ -223,13 +223,9 @@ void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx)
        }
 
        if (hlua && hlua->T) {
-               if (hlua_not_dumpable == 0) {
-                       luaL_traceback(hlua->T, hlua->T, NULL, 0);
-                       if (!append_prefixed_str(buf, lua_tostring(hlua->T, -1), pfx, '\n', 1))
-                               b_putchr(buf, '\n');
-               }
-               else
-                       chunk_appendf(buf, "Inside non-rentrant part, Stack traceback not available\n");
+               chunk_appendf(buf, "stack traceback:\n    ");
+               append_prefixed_str(buf, hlua_traceback(hlua->T, "\n    "), pfx, '\n', 0);
+               b_putchr(buf, '\n');
        }
        else
                b_putchr(buf, '\n');
index bd0c911..228bd67 100644 (file)
@@ -237,9 +237,6 @@ struct hlua_mem_allocator {
 
 static struct hlua_mem_allocator hlua_global_allocator;
 
- /* > 0 if lua is in a non-rentrant part, thus with a non-dumpable stack */
-THREAD_LOCAL unsigned int hlua_not_dumpable = 0;
-
 /* These functions converts types between HAProxy internal args or
  * sample and LUA types. Another function permits to check if the
  * LUA stack contains arguments according with an required ARG_T
@@ -8242,9 +8239,7 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
                /* it's a free */
                if (ptr)
                        zone->allocated -= osize;
-               hlua_not_dumpable++;
                free(ptr);
-               hlua_not_dumpable--;
                return NULL;
        }
 
@@ -8253,9 +8248,7 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
                if (zone->limit && zone->allocated + nsize > zone->limit)
                        return NULL;
 
-               hlua_not_dumpable++;
                ptr = malloc(nsize);
-               hlua_not_dumpable--;
                if (ptr)
                        zone->allocated += nsize;
                return ptr;
@@ -8265,9 +8258,7 @@ static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
        if (zone->limit && zone->allocated + nsize - osize > zone->limit)
                return NULL;
 
-       hlua_not_dumpable++;
        ptr = realloc(ptr, nsize);
-       hlua_not_dumpable--;
        if (ptr)
                zone->allocated += nsize - osize;
        return ptr;