From: Peter van Dijk Date: Fri, 7 Nov 2025 16:01:03 +0000 (+0100) Subject: luawrapper: on luajit+arm64+tsan, retry allocs harder X-Git-Tag: rec-5.4.0-alpha1~118^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F16451%2Fhead;p=thirdparty%2Fpdns.git luawrapper: on luajit+arm64+tsan, retry allocs harder Co-authored-by: Miod Vallat Co-authored-by: Otto Moerbeek Signed-off-by: Peter van Dijk --- diff --git a/ext/luawrapper/include/LuaContext.hpp b/ext/luawrapper/include/LuaContext.hpp index 2284e44922..95192586d6 100644 --- a/ext/luawrapper/include/LuaContext.hpp +++ b/ext/luawrapper/include/LuaContext.hpp @@ -56,6 +56,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +// Set the older gcc define for tsan if clang or modern gcc tell us we have tsan. +#if defined(__has_feature) +#if __has_feature(thread_sanitizer) +#define __SANITIZE_THREAD__ 1 +#endif +#endif + #if defined(_MSC_VER) && _MSC_VER < 1900 # include "misc/exception.hpp" #endif @@ -98,7 +105,17 @@ public: explicit LuaContext(bool openDefaultLibs = true) { // luaL_newstate can return null if allocation failed - mState = luaL_newstate(); +#if defined(__SANITIZE_THREAD__) && defined(LUAJIT_VERSION) && defined(__aarch64__) + // on arm64 with luajit and TSAN, allocation may fail spuriously, so keep retrying a bit + int tries = 100; +#else + int tries = 1; +#endif + for (; tries > 0; tries--) { + mState = luaL_newstate(); + if (mState != nullptr) { break; } + } +#undef RETRIES if (mState == nullptr) throw std::bad_alloc();