From b55e71e0a6a6c6ac4cba337a6fde716c749bee0e Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 29 Dec 2025 13:35:44 +0100 Subject: [PATCH] fix: Make sure updater properly exists after launching update process (cherry picked from commit e28f3b75a42c9b9617316dcea5c67f3c2df0ee29) --- lib/libimhex/include/hex/helpers/utils.hpp | 1 + lib/libimhex/source/helpers/utils.cpp | 38 ++++++++++++++++++++++ main/updater/source/main.cpp | 5 ++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index b3baebeed..a62e621d6 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -99,6 +99,7 @@ namespace hex { void startProgram(const std::vector &command); int executeCommand(const std::string &command); std::optional executeCommandWithOutput(const std::string &command); + void executeCommandDetach(const std::string &command); void openWebpage(std::string url); extern "C" void registerFont(const char *fontName, const char *fontPath); diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index 181942a68..7c6d808d9 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -29,10 +29,12 @@ #elif defined(OS_LINUX) #include #include + #include #include #elif defined(OS_MACOS) #include #include + #include #include #include #elif defined(OS_WEB) @@ -353,6 +355,42 @@ namespace hex { return result; } + void executeCommandDetach(const std::string &command) { + #if defined(OS_WINDOWS) + STARTUPINFOA si = { }; + PROCESS_INFORMATION pi = { }; + si.cb = sizeof(si); + + DWORD flags = CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW; + std::string cmdCopy = command; + + BOOL result = ::CreateProcessA( + nullptr, + cmdCopy.data(), + nullptr, + nullptr, + false, + flags, + nullptr, + nullptr, + &si, + &pi + ); + + if (result) { + ::CloseHandle(pi.hProcess); + ::CloseHandle(pi.hThread); + } + #elif defined(OS_MACOS) || defined(OS_LINUX) + pid_t pid; + const char* argv[] = { "sh", "-c", command.c_str(), nullptr }; + + ::posix_spawnp(&pid, "sh", nullptr, nullptr, const_cast(argv), nullptr); + #elif defined(OS_WEB) + std::ignore = command; + #endif + } + void openWebpage(std::string url) { if (!url.contains("://")) url = "https://" + url; diff --git a/main/updater/source/main.cpp b/main/updater/source/main.cpp index 423464a3b..3a4f5f71e 100644 --- a/main/updater/source/main.cpp +++ b/main/updater/source/main.cpp @@ -151,7 +151,10 @@ auto updateCommand(std::string command) { const auto formattedCommand = fmt::format(fmt::runtime(command), updatePath.string()); hex::log::info("Starting update process with command: '{}'", formattedCommand); - return hex::executeCommand(formattedCommand) == 0; + + hex::executeCommandDetach(formattedCommand); + + return 0; }; }