From 5a838f4867753d99e90b11f6181feb4a3374280c Mon Sep 17 00:00:00 2001 From: Peter van Dijk Date: Fri, 7 Nov 2025 17:01:03 +0100 Subject: [PATCH] luawrapper: on luajit+arm64+tsan, retry allocs harder Co-authored-by: Miod Vallat Co-authored-by: Otto Moerbeek Signed-off-by: Peter van Dijk --- ext/luawrapper/include/LuaContext.hpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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(); -- 2.47.3