From bd9e9a7dcfcd5b393f3d761a877e298bec2db5d0 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 27 Aug 2025 23:53:46 +0200 Subject: [PATCH] impr: Make crash handling from non-main threads more resilient --- main/gui/source/crash_handlers.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/main/gui/source/crash_handlers.cpp b/main/gui/source/crash_handlers.cpp index 3414e34eb..3d1537f91 100644 --- a/main/gui/source/crash_handlers.cpp +++ b/main/gui/source/crash_handlers.cpp @@ -77,7 +77,18 @@ namespace hex::crash { } } - extern "C" void triggerSafeShutdown(int signalNumber = 0) { + extern "C" [[noreturn]] void triggerSafeShutdown(int signalNumber = 0) { + if (!TaskManager::isMainThread()) { + log::error("Terminating from non-main thread, scheduling termination on main thread"); + TaskManager::doLater([signalNumber] { + triggerSafeShutdown(signalNumber); + }); + + while (true) { + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + } + // Trigger an event so that plugins can handle crashes EventAbnormalTermination::post(signalNumber); @@ -168,7 +179,10 @@ namespace hex::crash { // Print the current exception info try { - std::rethrow_exception(std::current_exception()); + if (std::uncaught_exceptions() > 0) + std::rethrow_exception(std::current_exception()); + else + log::fatal("std::terminate() called without an active exception"); } catch (std::exception &ex) { std::string exceptionStr = fmt::format("{}()::what() -> {}", trace::demangle(typeid(ex).name()), ex.what());