Skip to content

Commit 2e0183b

Browse files
authored
Do not register signal handlers for externally managed interpreters (#905)
1 parent 5aad26e commit 2e0183b

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

lib/CppInterOp/CppInterOp.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ struct InterpreterInfo {
188188

189189
static void DefaultProcessCrashHandler(void*);
190190
// Function-static storage for interpreters
191-
static std::deque<InterpreterInfo>& GetInterpreters() {
191+
static std::deque<InterpreterInfo>&
192+
GetInterpreters(bool SetCrashHandler = true) {
192193
// static int FakeArgc = 1;
193194
// static const std::string VersionStr = GetVersion();
194195
// static const char* ArgvBuffer[] = {VersionStr.c_str(), nullptr};
@@ -200,8 +201,9 @@ static std::deque<InterpreterInfo>& GetInterpreters() {
200201
// FIXME: Currently we never call llvm::llvm_shutdown and sInterpreters leaks.
201202
static llvm::ManagedStatic<std::deque<InterpreterInfo>> sInterpreters;
202203
static std::once_flag ProcessInitialized;
203-
std::call_once(ProcessInitialized, []() {
204-
llvm::sys::PrintStackTraceOnErrorSignal("CppInterOp");
204+
std::call_once(ProcessInitialized, [SetCrashHandler]() {
205+
if (SetCrashHandler)
206+
llvm::sys::PrintStackTraceOnErrorSignal("CppInterOp");
205207

206208
if (getenv("CPPINTEROP_LOG") != nullptr)
207209
CppInterOp::Tracing::InitTracing();
@@ -213,7 +215,9 @@ static std::deque<InterpreterInfo>& GetInterpreters() {
213215
llvm::InitializeAllAsmParsers();
214216
llvm::InitializeAllAsmPrinters();
215217

216-
llvm::sys::AddSignalHandler(DefaultProcessCrashHandler, /*Cookie=*/nullptr);
218+
if (SetCrashHandler)
219+
llvm::sys::AddSignalHandler(DefaultProcessCrashHandler,
220+
/*Cookie=*/nullptr);
217221

218222
// std::atexit(llvm::llvm_shutdown);
219223
});
@@ -262,7 +266,7 @@ static void DefaultProcessCrashHandler(void*) {
262266
}
263267

264268
static void RegisterInterpreter(compat::Interpreter* I, bool Owned) {
265-
std::deque<InterpreterInfo>& Interps = GetInterpreters();
269+
std::deque<InterpreterInfo>& Interps = GetInterpreters(Owned);
266270
Interps.emplace_back(I, Owned);
267271
}
268272

@@ -294,7 +298,7 @@ TInterp_t GetInterpreter() {
294298

295299
void UseExternalInterpreter(TInterp_t I) {
296300
INTEROP_TRACE(I);
297-
assert(GetInterpreters().empty() && "sInterpreter already in use!");
301+
assert(GetInterpreters(false).empty() && "sInterpreter already in use!");
298302
RegisterInterpreter(static_cast<compat::Interpreter*>(I), /*Owned=*/false);
299303
return INTEROP_VOID_RETURN();
300304
}

0 commit comments

Comments
 (0)