From 2064aea3b6d5bd2c1ef9dc8a2aed7f22bc783f8c Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 7 Jan 2026 17:12:17 +0100 Subject: [PATCH] fix: ImHex processes getting stuck in the background Fixes #2611 --- .../api/content_registry/communication_interface.hpp | 2 +- lib/libimhex/include/hex/helpers/auto_reset.hpp | 4 ++-- lib/libimhex/source/api/content_registry.cpp | 12 ++++++------ .../builtin/source/content/background_services.cpp | 8 ++++++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/libimhex/include/hex/api/content_registry/communication_interface.hpp b/lib/libimhex/include/hex/api/content_registry/communication_interface.hpp index dbd709249..3b95b9462 100644 --- a/lib/libimhex/include/hex/api/content_registry/communication_interface.hpp +++ b/lib/libimhex/include/hex/api/content_registry/communication_interface.hpp @@ -27,7 +27,7 @@ EXPORT_MODULE namespace hex { namespace ContentRegistry::MCP { namespace impl { - mcp::Server& getMcpServerInstance(); + std::unique_ptr& getMcpServerInstance(); void setEnabled(bool enabled); } diff --git a/lib/libimhex/include/hex/helpers/auto_reset.hpp b/lib/libimhex/include/hex/helpers/auto_reset.hpp index cd193f8d3..88416b211 100644 --- a/lib/libimhex/include/hex/helpers/auto_reset.hpp +++ b/lib/libimhex/include/hex/helpers/auto_reset.hpp @@ -87,9 +87,9 @@ namespace hex { private: void reset() override { - if constexpr (requires { m_value.reset(); }) { + if constexpr (requires(T t) { t.reset(); }) { m_value.reset(); - } else if constexpr (requires { m_value.clear(); }) { + } else if constexpr (requires(T t) { t.clear(); }) { m_value.clear(); } else if constexpr (std::is_pointer_v) { m_value = nullptr; // cppcheck-suppress nullPointer diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 82e566ce8..106966555 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -1421,13 +1421,13 @@ namespace hex { namespace impl { - mcp::Server& getMcpServerInstance() { - static AutoReset> server; + std::unique_ptr& getMcpServerInstance() { + static std::unique_ptr server; - if (*server == nullptr) + if (server == nullptr) server = std::make_unique(); - return **server; + return server; } static bool s_mcpEnabled = false; @@ -1442,11 +1442,11 @@ namespace hex { } bool isConnected() { - return impl::getMcpServerInstance().isConnected(); + return impl::getMcpServerInstance()->isConnected(); } void registerTool(std::string_view capabilities, std::function function) { - impl::getMcpServerInstance().addPrimitive("tools", capabilities, function); + impl::getMcpServerInstance()->addPrimitive("tools", capabilities, function); } } diff --git a/plugins/builtin/source/content/background_services.cpp b/plugins/builtin/source/content/background_services.cpp index ea5108d51..c9363d2b7 100644 --- a/plugins/builtin/source/content/background_services.cpp +++ b/plugins/builtin/source/content/background_services.cpp @@ -108,11 +108,11 @@ namespace hex::plugin::builtin { void handleMCPServer() { if (!ContentRegistry::MCP::isEnabled()) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); - ContentRegistry::MCP::impl::getMcpServerInstance().disconnect(); + ContentRegistry::MCP::impl::getMcpServerInstance()->disconnect(); 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.mcp", handleMCPServer); + EventImHexClosing::subscribe([] { + ContentRegistry::MCP::impl::getMcpServerInstance().reset(); + }); + EventProviderDirtied::subscribe([](prv::Provider *) { s_dataDirty = true; });