From: Aurelien DARRAGON Date: Mon, 16 Nov 2026 11:08:09 +0000 (+0100) Subject: MINOR: hlua: add our own safe coroutine.wrap() wrapper X-Git-Url: https://git.kaiwu.me/http/%7B@url%7D?a=commitdiff_plain;h=d3dd7127e8da0b7197e4a5052479ae8ec91012c1;p=haproxy.git MINOR: hlua: add our own safe coroutine.wrap() wrapper Similar to our coroutine.create() wrapper but for coroutine.wrap() which returns the new function instead of the coroutine itself. --- diff --git a/src/hlua.c b/src/hlua.c index 861986b26..9ff1cbf57 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -9093,6 +9093,24 @@ __LJMP static int hlua_coroutine_create(lua_State *L) return 1; } +/* safe lua coroutine.wrap() function: + * + * This is a simple wrapper for coroutine.wrap() that + * ensures the coroutine cannot be created from sub ctx, + * because then the coroutine could outlive its parent + * ctx and Lua doesn't provide a way for the parent ctx + * to destroy child threads. + */ +__LJMP static int hlua_coroutine_wrap(lua_State *L) +{ + MAY_LJMP(hlua_coroutine_create(L)); + /* pop thread from the stack so that is at the top + * of the stack + */ + lua_pop(L, 1); + return 1; +} + /* This function is used as a callback of a task. It is called by the * HAProxy task subsystem when the task is awaked. The LUA runtime can * return an E_AGAIN signal, the emmiter of this signal must set a @@ -14007,10 +14025,12 @@ lua_State *hlua_init_state(int thread_num) * */ - /* push our "safe" coroutine.create() function */ + /* push our "safe" coroutine.create() and .wrap() functions */ lua_getglobal(L, "coroutine"); lua_pushcclosure(L, hlua_coroutine_create, 0); lua_setfield(L, -2, "create"); + lua_pushcclosure(L, hlua_coroutine_wrap, 0); + lua_setfield(L, -2, "wrap"); /* *