From 59a90f28c0545df849b6965639db84dd866f817b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20Neunh=C3=B6ffer?= Date: Fri, 13 Sep 2019 12:27:54 +0200 Subject: [PATCH] Fix a shutdown busy loop after main if two exceptions collide. (#9984) --- arangod/RestServer/arangod.cpp | 5 +++++ lib/Basics/files.cpp | 18 ++++++++++++++++++ lib/Basics/operating-system.h | 9 +++++++++ 3 files changed, 32 insertions(+) diff --git a/arangod/RestServer/arangod.cpp b/arangod/RestServer/arangod.cpp index caefa1a883..d8a986928d 100644 --- a/arangod/RestServer/arangod.cpp +++ b/arangod/RestServer/arangod.cpp @@ -299,6 +299,11 @@ static void WINAPI ServiceMain(DWORD dwArgc, LPSTR* lpszArgv) { #endif int main(int argc, char* argv[]) { +#ifdef __linux__ + // Do not delete this! See lib/Basics/operating-system.h for details. + ThrowSomeException(); +#endif + #ifdef __linux__ #if USE_ENTERPRISE arangodb::checkLicenseKey(); diff --git a/lib/Basics/files.cpp b/lib/Basics/files.cpp index 93e1d44506..4422c5a49e 100644 --- a/lib/Basics/files.cpp +++ b/lib/Basics/files.cpp @@ -2451,3 +2451,21 @@ bool TRI_GETENV(char const* which, std::string& value) { return true; #endif } + +////////////////////////////////////////////////////////////////////////////// +/// @brief bug fix for some race on libmusl and static linking +////////////////////////////////////////////////////////////////////////////// + +// The following function just throws an exception and catches it. This is +// used on Linux for the case that we link statically and the underlying +// C-library is libmusl. This configuration has a bug in libgcc which +// triggers a shutdown busy loop (after main), provided the very first +// exception being thrown in the life of the process happens in two threads +// at the same time. By throwing right at the beginning of main() when the +// process is still single-threaded, we circumvent this problem. +#ifdef __linux__ +void ThrowSomeException() { + try { throw 42; } catch(int const&) {}; +} +#endif + diff --git a/lib/Basics/operating-system.h b/lib/Basics/operating-system.h index 6bb70b2a5a..124adc6452 100644 --- a/lib/Basics/operating-system.h +++ b/lib/Basics/operating-system.h @@ -675,6 +675,15 @@ #define TRI_uid_t uid_t #define TRI_gid_t gid_t +// The following function just throws an exception and catches it. This is +// used on Linux for the case that we link statically and the underlying +// C-library is libmusl. This configuration has a bug in libgcc which +// triggers a shutdown busy loop (after main), provided the very first +// exception being thrown in the life of the process happens in two threads +// at the same time. By throwing right at the beginning of main() when the +// process is still single-threaded, we circumvent this problem. +void ThrowSomeException(); + #endif // -----------------------------------------------------------------------------