Skip to content

Commit 4a74481

Browse files
Darleletcapflam
authored andcommitted
DEBUG: hlua: distinguish burst timeout errors from exec timeout errors
hlua burst timeout was introduced in 58e36e5 ("MEDIUM: hlua: introduce tune.lua.burst-timeout"). It is a safety measure that allows to detect when too much time is spent on a single lua execution (between 2 interruptions/yields), meaning that the current thread is not able to perform other tasks. Such scenario should be avoided because it will cause thread contention which may have negative performance impact and could cause the watchdog to trigger. When the burst timeout is exceeded, the current Lua execution is aborted and a timeout error is reported to the user. Unfortunately, the same error is currently being reported for cumulative (AKA execution) timeout and for burst timeout, which may be confusing to the user. Indeed, "execution timeout" error historically results from the current hlua context exceeding the total (cumulative) time it's allowed to run. It is set per lua context using the dedicated tunables: - tune.lua.session-timeout - tune.lua.task-timeout - tune.lua.service-timeout We've already faced an user report where the user was able to trigger the burst timeout and got "Lua task: execution timeout." error while the user didn't set cumulative timeout. Thus the error was actually confusing because it was indeed the burst timeout which was causing it due to the use of cpu-intensive call from within the task without sufficient manual "yield" keypoints around the cpu-intensive call to ensure it runs on a dedicated scheduler cycle. In this patch we make it so burst timeout related errors are reported as "burst timeout" errors instead of "execution timeout" errors (which in fact became the generic timeout errors catchall with 58e36e5). To do this, hlua_timer_check() now returns a different value depending if the exeeded timeout is the burst one or the cumulative one, which allows us to return either HLUA_E_ETMOUT or HLUA_E_BTMOUT in hlua_ctx_resume(). It should improve the situation described in GH #2356 and may possibly be backported with 58e36e5 to improve error reporting if it applies without resistance. (cherry picked from commit 983513d) [cf: No reason to backport further] Signed-off-by: Christopher Faulet <[email protected]>
1 parent 8a11062 commit 4a74481

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

include/haproxy/hlua-t.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ enum hlua_exec {
9494
HLUA_E_AGAIN, /* LUA yield, must resume the stack execution later, when
9595
the associatedtask is waked. */
9696
HLUA_E_ETMOUT, /* Execution timeout */
97+
HLUA_E_BTMOUT, /* Burst timeout */
9798
HLUA_E_NOMEM, /* Out of memory error */
9899
HLUA_E_YIELD, /* LUA code try to yield, and this is not allowed */
99100
HLUA_E_ERRMSG, /* LUA stack execution failed with a string error message

src/hlua.c

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -564,15 +564,16 @@ static inline void hlua_timer_stop(struct hlua_timer *timer)
564564
hlua resume, ie: time between effective yields)
565565
* - then check for yield cumulative timeout
566566
*
567-
* Returns 1 if the check succeeded and 0 if it failed
568-
* (ie: timeout exceeded)
567+
* Returns 1 if the check succeeded, 0 if it failed because cumulative
568+
* timeout is exceeded, and -1 if it failed because burst timeout is
569+
* exceeded.
569570
*/
570571
static inline int hlua_timer_check(const struct hlua_timer *timer)
571572
{
572573
uint32_t pburst = _hlua_time_burst(timer); /* pending burst time in ms */
573574

574575
if (hlua_timeout_burst && (timer->burst + pburst) > hlua_timeout_burst)
575-
return 0; /* burst timeout exceeded */
576+
return -1; /* burst timeout exceeded */
576577
if (timer->max && (timer->cumulative + timer->burst + pburst) > timer->max)
577578
return 0; /* cumulative timeout exceeded */
578579
return 1; /* ok */
@@ -1883,6 +1884,7 @@ static struct hlua *hlua_stream_ctx_prepare(struct stream *s, int state_id)
18831884
void hlua_hook(lua_State *L, lua_Debug *ar)
18841885
{
18851886
struct hlua *hlua;
1887+
int timer_check;
18861888

18871889
/* Get hlua struct, or NULL if we execute from main lua state */
18881890
hlua = hlua_gethlua(L);
@@ -1937,8 +1939,12 @@ void hlua_hook(lua_State *L, lua_Debug *ar)
19371939

19381940
check_timeout:
19391941
/* If we cannot yield, check the timeout. */
1940-
if (!hlua_timer_check(&hlua->timer)) {
1941-
lua_pushfstring(L, "execution timeout");
1942+
timer_check = hlua_timer_check(&hlua->timer);
1943+
if (timer_check <= 0) {
1944+
if (!timer_check)
1945+
lua_pushfstring(L, "execution timeout");
1946+
else
1947+
lua_pushfstring(L, "burst timeout");
19421948
WILL_LJMP(lua_error(L));
19431949
}
19441950

@@ -2037,10 +2043,18 @@ static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
20372043
/* Check if the execution timeout is expired. If it is the case, we
20382044
* break the Lua execution.
20392045
*/
2040-
if (!hlua_timer_check(&lua->timer)) {
2041-
lua_settop(lua->T, 0); /* Empty the stack. */
2042-
ret = HLUA_E_ETMOUT;
2043-
break;
2046+
{
2047+
int timer_check;
2048+
2049+
timer_check = hlua_timer_check(&lua->timer);
2050+
if (timer_check <= 0) {
2051+
if (!timer_check)
2052+
ret = HLUA_E_ETMOUT;
2053+
else
2054+
ret = HLUA_E_BTMOUT;
2055+
lua_settop(lua->T, 0); /* Empty the stack. */
2056+
break;
2057+
}
20442058
}
20452059
/* Process the forced yield. if the general yield is not allowed or
20462060
* if no task were associated this the current Lua execution
@@ -2137,6 +2151,7 @@ static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
21372151
break;
21382152

21392153
case HLUA_E_ETMOUT:
2154+
case HLUA_E_BTMOUT:
21402155
case HLUA_E_NOMEM:
21412156
case HLUA_E_YIELD:
21422157
case HLUA_E_ERR:
@@ -9243,6 +9258,9 @@ struct task *hlua_process_task(struct task *task, void *context, unsigned int st
92439258
case HLUA_E_ETMOUT:
92449259
SEND_ERR(NULL, "Lua task: execution timeout.\n");
92459260
goto err_task_abort;
9261+
case HLUA_E_BTMOUT:
9262+
SEND_ERR(NULL, "Lua task: burst timeout.\n");
9263+
goto err_task_abort;
92469264
case HLUA_E_ERRMSG:
92479265
hlua_lock(hlua);
92489266
SEND_ERR(NULL, "Lua task: %s.\n", hlua_tostring_safe(hlua->T, -1));
@@ -9475,6 +9493,10 @@ static void hlua_event_handler(struct hlua *hlua)
94759493
SEND_ERR(NULL, "Lua event_hdl: execution timeout.\n");
94769494
break;
94779495

9496+
case HLUA_E_BTMOUT:
9497+
SEND_ERR(NULL, "Lua event_hdl: burst timeout.\n");
9498+
break;
9499+
94789500
case HLUA_E_ERRMSG:
94799501
hlua_lock(hlua);
94809502
SEND_ERR(NULL, "Lua event_hdl: %s.\n", hlua_tostring_safe(hlua->T, -1));
@@ -10201,6 +10223,10 @@ static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp,
1020110223
SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
1020210224
return 0;
1020310225

10226+
case HLUA_E_BTMOUT:
10227+
SEND_ERR(stream->be, "Lua converter '%s': burst timeout.\n", fcn->name);
10228+
return 0;
10229+
1020410230
case HLUA_E_NOMEM:
1020510231
SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
1020610232
return 0;
@@ -10336,6 +10362,10 @@ static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp
1033610362
SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
1033710363
return 0;
1033810364

10365+
case HLUA_E_BTMOUT:
10366+
SEND_ERR(smp->px, "Lua sample-fetch '%s': burst timeout.\n", fcn->name);
10367+
return 0;
10368+
1033910369
case HLUA_E_NOMEM:
1034010370
SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
1034110371
return 0;
@@ -10694,6 +10724,10 @@ static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
1069410724
SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
1069510725
goto end;
1069610726

10727+
case HLUA_E_BTMOUT:
10728+
SEND_ERR(px, "Lua function '%s': burst timeout.\n", rule->arg.hlua_rule->fcn->name);
10729+
goto end;
10730+
1069710731
case HLUA_E_NOMEM:
1069810732
SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
1069910733
goto end;
@@ -10874,6 +10908,11 @@ void hlua_applet_tcp_fct(struct appctx *ctx)
1087410908
rule->arg.hlua_rule->fcn->name);
1087510909
goto error;
1087610910

10911+
case HLUA_E_BTMOUT:
10912+
SEND_ERR(px, "Lua applet tcp '%s': burst timeout.\n",
10913+
rule->arg.hlua_rule->fcn->name);
10914+
goto error;
10915+
1087710916
case HLUA_E_NOMEM:
1087810917
SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
1087910918
rule->arg.hlua_rule->fcn->name);
@@ -11087,6 +11126,11 @@ void hlua_applet_http_fct(struct appctx *ctx)
1108711126
rule->arg.hlua_rule->fcn->name);
1108811127
goto error;
1108911128

11129+
case HLUA_E_BTMOUT:
11130+
SEND_ERR(px, "Lua applet http '%s': burst timeout.\n",
11131+
rule->arg.hlua_rule->fcn->name);
11132+
goto error;
11133+
1109011134
case HLUA_E_NOMEM:
1109111135
SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
1109211136
rule->arg.hlua_rule->fcn->name);
@@ -11715,6 +11759,11 @@ static int hlua_cli_io_handler_fct(struct appctx *appctx)
1171511759
fcn->name);
1171611760
return 1;
1171711761

11762+
case HLUA_E_BTMOUT:
11763+
SEND_ERR(NULL, "Lua converter '%s': burst timeout.\n",
11764+
fcn->name);
11765+
return 1;
11766+
1171811767
case HLUA_E_NOMEM:
1171911768
SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
1172011769
fcn->name);
@@ -12168,6 +12217,10 @@ static int hlua_filter_new(struct stream *s, struct filter *filter)
1216812217
SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name);
1216912218
ret = 0;
1217012219
goto end;
12220+
case HLUA_E_BTMOUT:
12221+
SEND_ERR(s->be, "Lua filter '%s' : 'new' burst timeout.\n", conf->reg->name);
12222+
ret = 0;
12223+
goto end;
1217112224
case HLUA_E_NOMEM:
1217212225
SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name);
1217312226
ret = 0;
@@ -12367,6 +12420,9 @@ static int hlua_filter_callback(struct stream *s, struct filter *filter, const c
1236712420
case HLUA_E_ETMOUT:
1236812421
SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun);
1236912422
goto end;
12423+
case HLUA_E_BTMOUT:
12424+
SEND_ERR(s->be, "Lua filter '%s' : '%s' callback burst timeout.\n", conf->reg->name, fun);
12425+
goto end;
1237012426
case HLUA_E_NOMEM:
1237112427
SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name);
1237212428
goto end;

0 commit comments

Comments
 (0)