Skip to content

Commit 85356b3

Browse files
authored
Merge pull request #16451 from Habbie/luajit-thread-aargh64itizer
luawrapper: on luajit+arm64+tsan, retry allocs harder
2 parents 18fb734 + 5a838f4 commit 85356b3

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

ext/luawrapper/include/LuaContext.hpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5656
#include <boost/type_traits.hpp>
5757
#include <lua.hpp>
5858

59+
// Set the older gcc define for tsan if clang or modern gcc tell us we have tsan.
60+
#if defined(__has_feature)
61+
#if __has_feature(thread_sanitizer)
62+
#define __SANITIZE_THREAD__ 1
63+
#endif
64+
#endif
65+
5966
#if defined(_MSC_VER) && _MSC_VER < 1900
6067
# include "misc/exception.hpp"
6168
#endif
@@ -98,7 +105,17 @@ class LuaContext {
98105
explicit LuaContext(bool openDefaultLibs = true)
99106
{
100107
// luaL_newstate can return null if allocation failed
101-
mState = luaL_newstate();
108+
#if defined(__SANITIZE_THREAD__) && defined(LUAJIT_VERSION) && defined(__aarch64__)
109+
// on arm64 with luajit and TSAN, allocation may fail spuriously, so keep retrying a bit
110+
int tries = 100;
111+
#else
112+
int tries = 1;
113+
#endif
114+
for (; tries > 0; tries--) {
115+
mState = luaL_newstate();
116+
if (mState != nullptr) { break; }
117+
}
118+
#undef RETRIES
102119
if (mState == nullptr)
103120
throw std::bad_alloc();
104121

0 commit comments

Comments
 (0)