fix: ImHex processes getting stuck in the background

Fixes #2611
This commit is contained in:
WerWolv
2026-01-07 17:12:17 +01:00
parent 3411bc4577
commit 2064aea3b6
4 changed files with 15 additions and 11 deletions

View File

@@ -27,7 +27,7 @@ EXPORT_MODULE namespace hex {
namespace ContentRegistry::MCP { namespace ContentRegistry::MCP {
namespace impl { namespace impl {
mcp::Server& getMcpServerInstance(); std::unique_ptr<mcp::Server>& getMcpServerInstance();
void setEnabled(bool enabled); void setEnabled(bool enabled);
} }

View File

@@ -87,9 +87,9 @@ namespace hex {
private: private:
void reset() override { void reset() override {
if constexpr (requires { m_value.reset(); }) { if constexpr (requires(T t) { t.reset(); }) {
m_value.reset(); m_value.reset();
} else if constexpr (requires { m_value.clear(); }) { } else if constexpr (requires(T t) { t.clear(); }) {
m_value.clear(); m_value.clear();
} else if constexpr (std::is_pointer_v<T>) { } else if constexpr (std::is_pointer_v<T>) {
m_value = nullptr; // cppcheck-suppress nullPointer m_value = nullptr; // cppcheck-suppress nullPointer

View File

@@ -1421,13 +1421,13 @@ namespace hex {
namespace impl { namespace impl {
mcp::Server& getMcpServerInstance() { std::unique_ptr<mcp::Server>& getMcpServerInstance() {
static AutoReset<std::unique_ptr<mcp::Server>> server; static std::unique_ptr<mcp::Server> server;
if (*server == nullptr) if (server == nullptr)
server = std::make_unique<mcp::Server>(); server = std::make_unique<mcp::Server>();
return **server; return server;
} }
static bool s_mcpEnabled = false; static bool s_mcpEnabled = false;
@@ -1442,11 +1442,11 @@ namespace hex {
} }
bool isConnected() { bool isConnected() {
return impl::getMcpServerInstance().isConnected(); return impl::getMcpServerInstance()->isConnected();
} }
void registerTool(std::string_view capabilities, std::function<nlohmann::json(const nlohmann::json &params)> function) { void registerTool(std::string_view capabilities, std::function<nlohmann::json(const nlohmann::json &params)> function) {
impl::getMcpServerInstance().addPrimitive("tools", capabilities, function); impl::getMcpServerInstance()->addPrimitive("tools", capabilities, function);
} }
} }

View File

@@ -108,11 +108,11 @@ namespace hex::plugin::builtin {
void handleMCPServer() { void handleMCPServer() {
if (!ContentRegistry::MCP::isEnabled()) { if (!ContentRegistry::MCP::isEnabled()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
ContentRegistry::MCP::impl::getMcpServerInstance().disconnect(); ContentRegistry::MCP::impl::getMcpServerInstance()->disconnect();
return; return;
} }
ContentRegistry::MCP::impl::getMcpServerInstance().listen(); ContentRegistry::MCP::impl::getMcpServerInstance()->listen();
} }
} }
@@ -126,6 +126,10 @@ namespace hex::plugin::builtin {
ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.auto_backup", handleAutoBackup); ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.auto_backup", handleAutoBackup);
ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.mcp", handleMCPServer); ContentRegistry::BackgroundServices::registerService("hex.builtin.background_service.mcp", handleMCPServer);
EventImHexClosing::subscribe([] {
ContentRegistry::MCP::impl::getMcpServerInstance().reset();
});
EventProviderDirtied::subscribe([](prv::Provider *) { EventProviderDirtied::subscribe([](prv::Provider *) {
s_dataDirty = true; s_dataDirty = true;
}); });