From: Aurelien DARRAGON Date: Mon, 11 Mar 2024 23:22:44 +0000 (+0100) Subject: DEBUG: lua: precisely identify if stream is stuck inside lua or not X-Git-Tag: v3.0-dev6~98 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1a2cdf64c9e33495b993771da8759aac9d3dd0ba;p=thirdparty%2Fhaproxy.git DEBUG: lua: precisely identify if stream is stuck inside lua or not When ha_panic() is called by the watchdog, we try to guess from ha_task_dump() and ha_thread_dump_one() if the thread was stuck while executing lua from the stream context. However we consider this is the case by simply checking if the stream hlua context was set, but this is not very precise because if the hlua context is set, then it simply means that at least one lua instruction was executed at the stream level, not that the stuck was currently executing lua when the panic occured. This is especially true with filters, one could simply register a lua filter that does nothing but this will still end up initializing the stream hlua context for each stream. If the thread end up being stuck during the stream handling, then debug dumping functions will report that the stream was stuck while handling lua, which is not necessarilly true, and could in fact confuse us even more. So here we take another approach, we add the BUSY flag to hlua context: this flag is set by hlua_ctx_resume() around lua_resume() call, this way we can precisely tell if the thread was handling lua when it was interrupted, and we rely on this flag in debug functions to check if the thread was effectively stuck inside lua or not while processing the stream No backport needed unless a commit depends on it. --- diff --git a/include/haproxy/hlua-t.h b/include/haproxy/hlua-t.h index 2672ffdcf4..af54d86943 100644 --- a/include/haproxy/hlua-t.h +++ b/include/haproxy/hlua-t.h @@ -67,6 +67,7 @@ struct stream; #define HLUA_WAKEREQWR 0x00000008 #define HLUA_EXIT 0x00000010 #define HLUA_NOYIELD 0x00000020 +#define HLUA_BUSY 0x00000040 #define HLUA_F_AS_STRING 0x01 #define HLUA_F_MAY_USE_HTTP 0x02 diff --git a/include/haproxy/hlua.h b/include/haproxy/hlua.h index 3c67ccea84..18fad9ff2c 100644 --- a/include/haproxy/hlua.h +++ b/include/haproxy/hlua.h @@ -30,6 +30,9 @@ #define HLUA_SET_RUN(__hlua) do {(__hlua)->flags |= HLUA_RUN;} while(0) #define HLUA_CLR_RUN(__hlua) do {(__hlua)->flags &= ~HLUA_RUN;} while(0) #define HLUA_IS_RUNNING(__hlua) ((__hlua)->flags & HLUA_RUN) +#define HLUA_SET_BUSY(__hlua) do {(__hlua)->flags |= HLUA_BUSY;} while(0) +#define HLUA_CLR_BUSY(__hlua) do {(__hlua)->flags &= ~HLUA_BUSY;} while(0) +#define HLUA_IS_BUSY(__hlua) ((__hlua)->flags & HLUA_BUSY) #define HLUA_SET_CTRLYIELD(__hlua) do {(__hlua)->flags |= HLUA_CTRLYIELD;} while(0) #define HLUA_CLR_CTRLYIELD(__hlua) do {(__hlua)->flags &= ~HLUA_CTRLYIELD;} while(0) #define HLUA_IS_CTRLYIELDING(__hlua) ((__hlua)->flags & HLUA_CTRLYIELD) diff --git a/src/debug.c b/src/debug.c index d1526e1d44..ff54803737 100644 --- a/src/debug.c +++ b/src/debug.c @@ -302,7 +302,7 @@ void ha_thread_dump_one(int thr, int from_signal) const struct stream *s = (const struct stream *)th_ctx->current->context; struct hlua *hlua = s ? s->hlua : NULL; - if (hlua && hlua->T) { + if (hlua && HLUA_IS_BUSY(hlua)) { mark_tainted(TAINTED_LUA_STUCK); if (hlua->state_id == 0) mark_tainted(TAINTED_LUA_STUCK_SHARED); @@ -417,7 +417,8 @@ void ha_task_dump(struct buffer *buf, const struct task *task, const char *pfx) #ifdef USE_LUA hlua = NULL; - if (s && (hlua = s->hlua)) { + if (s && s->hlua && HLUA_IS_BUSY(s->hlua)) { + hlua = s->hlua; chunk_appendf(buf, "%sCurrent executing Lua from a stream analyser -- ", pfx); } else if (task->process == hlua_process_task && (hlua = task->context)) { diff --git a/src/hlua.c b/src/hlua.c index 7f1dfed6da..e0a5bfdff9 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -1803,6 +1803,8 @@ resume_execution: /* start the timer as we're about to start lua processing */ hlua_timer_start(&lua->timer); + HLUA_SET_BUSY(lua); + /* Call the function. */ #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres); @@ -1810,6 +1812,8 @@ resume_execution: ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); #endif + HLUA_CLR_BUSY(lua); + /* out of lua processing, stop the timer */ hlua_timer_stop(&lua->timer);