diff --git a/llvm/examples/OrcV2Examples/LLJITWithRemoteDebugging/RemoteJITUtils.cpp b/llvm/examples/OrcV2Examples/LLJITWithRemoteDebugging/RemoteJITUtils.cpp --- a/llvm/examples/OrcV2Examples/LLJITWithRemoteDebugging/RemoteJITUtils.cpp +++ b/llvm/examples/OrcV2Examples/LLJITWithRemoteDebugging/RemoteJITUtils.cpp @@ -50,6 +50,7 @@ RemoteTargetProcessControl(ExecutionSession &ES, std::unique_ptr Channel, std::unique_ptr Endpoint); + ~RemoteTargetProcessControl(); void initializeMemoryManagement(); Error disconnect() override; @@ -60,6 +61,7 @@ std::unique_ptr OwnedMemAccess; std::unique_ptr OwnedMemMgr; std::atomic Finished{false}; + std::atomic AttemptDisconnect{0}; std::thread ListenerThread; }; @@ -80,6 +82,12 @@ }); } +RemoteTargetProcessControl::~RemoteTargetProcessControl() { + // Avoid reportError() from the base class, because the ExecutionSession + // might have been deleted already. + consumeError(disconnect()); +} + void RemoteTargetProcessControl::initializeMemoryManagement() { OwnedMemAccess = std::make_unique(*this); OwnedMemMgr = std::make_unique(*this); @@ -90,6 +98,10 @@ } Error RemoteTargetProcessControl::disconnect() { + // Make sure we don't disconnect more than once. + if (AttemptDisconnect.fetch_or(1)) + return Error::success(); + std::promise P; auto F = P.get_future(); auto Err = closeConnection([&](Error Err) -> Error {