Skip to content

Commit df4c367

Browse files
authored
[lldb] Stop the protocol servers when terminating the plugin (#156101)
Currently, the server keeps running until we call Stop from its dtor in the static destruction chain. This is too late: the server should stop when the plugin gets terminated.
1 parent 6d43551 commit df4c367

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

lldb/include/lldb/Core/ProtocolServer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class ProtocolServer : public PluginInterface {
2222

2323
static ProtocolServer *GetOrCreate(llvm::StringRef name);
2424

25+
static llvm::Error Terminate();
26+
2527
static std::vector<llvm::StringRef> GetSupportedProtocols();
2628

2729
struct Connection {

lldb/source/Core/ProtocolServer.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,29 @@
88

99
#include "lldb/Core/ProtocolServer.h"
1010
#include "lldb/Core/PluginManager.h"
11+
#include "llvm/Support/Error.h"
1112

1213
using namespace lldb_private;
1314
using namespace lldb;
1415

15-
ProtocolServer *ProtocolServer::GetOrCreate(llvm::StringRef name) {
16-
static std::mutex g_mutex;
16+
static std::pair<llvm::StringMap<ProtocolServerUP> &, std::mutex &> Servers() {
1717
static llvm::StringMap<ProtocolServerUP> g_protocol_server_instances;
18+
static std::mutex g_mutex;
19+
return {g_protocol_server_instances, g_mutex};
20+
}
21+
22+
ProtocolServer *ProtocolServer::GetOrCreate(llvm::StringRef name) {
23+
auto [protocol_server_instances, mutex] = Servers();
1824

19-
std::lock_guard<std::mutex> guard(g_mutex);
25+
std::lock_guard<std::mutex> guard(mutex);
2026

21-
auto it = g_protocol_server_instances.find(name);
22-
if (it != g_protocol_server_instances.end())
27+
auto it = protocol_server_instances.find(name);
28+
if (it != protocol_server_instances.end())
2329
return it->second.get();
2430

2531
if (ProtocolServerCreateInstance create_callback =
2632
PluginManager::GetProtocolCreateCallbackForPluginName(name)) {
27-
auto pair =
28-
g_protocol_server_instances.try_emplace(name, create_callback());
33+
auto pair = protocol_server_instances.try_emplace(name, create_callback());
2934
return pair.first->second.get();
3035
}
3136

@@ -45,3 +50,18 @@ std::vector<llvm::StringRef> ProtocolServer::GetSupportedProtocols() {
4550

4651
return supported_protocols;
4752
}
53+
54+
llvm::Error ProtocolServer::Terminate() {
55+
llvm::Error error = llvm::Error::success();
56+
57+
auto [protocol_server_instances, mutex] = Servers();
58+
std::lock_guard<std::mutex> guard(mutex);
59+
for (auto &instance : protocol_server_instances) {
60+
if (llvm::Error instance_error = instance.second->Stop())
61+
error = llvm::joinErrors(std::move(error), std::move(instance_error));
62+
}
63+
64+
protocol_server_instances.clear();
65+
66+
return error;
67+
}

lldb/source/Plugins/Protocol/MCP/ProtocolServerMCP.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ void ProtocolServerMCP::Initialize() {
3939
}
4040

4141
void ProtocolServerMCP::Terminate() {
42+
if (llvm::Error error = ProtocolServer::Terminate())
43+
LLDB_LOG_ERROR(GetLog(LLDBLog::Host), std::move(error), "{0}");
4244
PluginManager::UnregisterPlugin(CreateInstance);
4345
}
4446

0 commit comments

Comments
 (0)