diff --git a/ApplicationExeCode/RiaMain.cpp b/ApplicationExeCode/RiaMain.cpp index 7b59b38da5..9a24d78b4b 100644 --- a/ApplicationExeCode/RiaMain.cpp +++ b/ApplicationExeCode/RiaMain.cpp @@ -75,6 +75,8 @@ void riaFilteredMessageHandler( QtMsgType type, const QMessageLogContext& contex return; } + RiaMainTools::recordQtMessage( type, msg ); + if ( g_previousMessageHandler ) { g_previousMessageHandler( type, context, msg ); diff --git a/ApplicationExeCode/RiaMainTools.cpp b/ApplicationExeCode/RiaMainTools.cpp index dc463c6e0d..eeca777a1f 100644 --- a/ApplicationExeCode/RiaMainTools.cpp +++ b/ApplicationExeCode/RiaMainTools.cpp @@ -21,6 +21,7 @@ #include "RiaFileLogger.h" #include "RiaLogging.h" #include "RiaOpenTelemetryManager.h" +#include "RiaPreferencesOpenTelemetry.h" #include "RiaRegressionTestRunner.h" #include "RiaSocketCommand.h" #include "RiaVersionInfo.h" @@ -31,12 +32,18 @@ #include "cafPdmUiFieldEditorHandle.h" #include +#include +#include #include +#include #include +#include +#include #include #include #include +#include #include #include @@ -57,6 +64,7 @@ #ifndef WIN32 #include +#include // macOS marks as deprecated and the mcontext layout // (gregs[REG_RIP]) is Linux-only — skip the program-counter capture // path on Apple platforms. See #14045. @@ -160,7 +168,59 @@ static std::string signalCodeDescription( int signo, int siCode ) #endif } // namespace internal -static std::atomic s_crashHandlingInProgress( false ); +static std::atomic s_crashHandlingInProgress( false ); +static std::atomic s_crashHandlingThread; + +static constexpr size_t s_maxRecordedQtMessages = 5; +static constexpr int s_maxQtMessageLength = 300; +static std::atomic_flag s_qtMessageLock = ATOMIC_FLAG_INIT; +static std::deque s_recentQtMessages; + +//-------------------------------------------------------------------------------------------------- +/// Crash logging is not async-signal-safe and may deadlock. Arm a watchdog so the process is +/// killed by SIGALRM (default action) instead of lingering as a hung process using CPU. +/// The timeout covers the telemetry flush (connection timeout) plus a margin for symbol resolution. +//-------------------------------------------------------------------------------------------------- +static void startCrashWatchdog() +{ +#ifndef WIN32 + unsigned int timeoutSeconds = 15; + if ( auto* prefs = RiaPreferencesOpenTelemetry::current() ) + { + timeoutSeconds = std::max( timeoutSeconds, static_cast( prefs->connectionTimeoutMs() / 1000 + 10 ) ); + } + + signal( SIGALRM, SIG_DFL ); + alarm( timeoutSeconds ); +#endif +} + +//-------------------------------------------------------------------------------------------------- +/// Must be the first thing done in every crash handler, before any allocation or formatting, so that +/// the watchdog is armed and nested crashes are caught before they can recurse. +/// +/// A nested crash on the handling thread (e.g. a paint event during the telemetry flush) exits +/// immediately. A crash on another thread is parked so the first crash report can complete; the +/// watchdog still bounds how long that takes. See #15035. +//-------------------------------------------------------------------------------------------------- +static void enterCrashHandling() +{ + if ( s_crashHandlingInProgress.exchange( true ) ) + { + if ( s_crashHandlingThread.load() != std::this_thread::get_id() ) + { + for ( ;; ) + { + std::this_thread::sleep_for( std::chrono::hours( 1 ) ); + } + } + + std::_Exit( 1 ); + } + + s_crashHandlingThread = std::this_thread::get_id(); + startCrashWatchdog(); +} //-------------------------------------------------------------------------------------------------- /// Shared crash logging: writes to file logger and OpenTelemetry. @@ -170,12 +230,30 @@ static std::atomic s_crashHandlingInProgress( false ); /// expected on Windows. Behavior on Linux is undefined, but will work in most cases. /// https://github.com/gabime/spdlog/issues/1607 //-------------------------------------------------------------------------------------------------- -static void performCrashLogging( int signalCode, const std::map& extraAttrs ) +static void performCrashLogging( int signalCode, const std::map& crashAttrs ) { // Block timer-driven application logic (e.g. gRPC idle processing) for the rest of the process // lifetime: the telemetry flush below pumps the event loop, and a timer slot firing there could // close the project and destroy objects the crashed code still uses, causing a nested crash. - s_crashHandlingInProgress = true; + // The flag is set by enterCrashHandling(), which every crash handler calls first. + + auto extraAttrs = crashAttrs; + { + // Spin lock with try-semantics: the crash may have happened while this thread was recording a message + if ( !s_qtMessageLock.test_and_set( std::memory_order_acquire ) ) + { + if ( !s_recentQtMessages.empty() ) + { + std::string joined; + for ( const auto& message : s_recentQtMessages ) + { + joined += message + "\n"; + } + extraAttrs["crash.last_qt_messages"] = joined; + } + s_qtMessageLock.clear( std::memory_order_release ); + } + } #if RIA_HAS_STD_STACKTRACE auto st = std::stacktrace::current(); @@ -226,6 +304,8 @@ static void performCrashLogging( int signalCode, const std::map extraAttrs; if ( auto ex = std::current_exception() ) @@ -249,7 +329,10 @@ void manageTerminate() // prior unwind) that trace still includes the original throw site. performCrashLogging( SIGABRT, extraAttrs ); - std::abort(); // preserve default terminate behavior (core dump, exit status) + // Restore the default action so abort() produces a core dump and a SIGABRT exit status instead of + // being caught by the crash handler, which would exit silently as a nested crash. + signal( SIGABRT, SIG_DFL ); + std::abort(); } //-------------------------------------------------------------------------------------------------- @@ -257,8 +340,10 @@ void manageTerminate() //-------------------------------------------------------------------------------------------------- void manageSegFailure( int signalCode ) { + enterCrashHandling(); performCrashLogging( signalCode, {} ); - exit( 1 ); + // _Exit skips atexit handlers and static destructors, which can crash again after a fault (#15035) + std::_Exit( 1 ); } #ifndef WIN32 @@ -268,6 +353,8 @@ void manageSegFailure( int signalCode ) //-------------------------------------------------------------------------------------------------- void manageSegFailureSA( int signalCode, siginfo_t* info, void* ucontext ) { + enterCrashHandling(); + std::map extraAttrs; if ( info ) @@ -311,7 +398,8 @@ void manageSegFailureSA( int signalCode, siginfo_t* info, void* ucontext ) #endif performCrashLogging( signalCode, extraAttrs ); - exit( 1 ); + // _Exit skips atexit handlers and static destructors, which can crash again after a fault (#15035) + std::_Exit( 1 ); } #endif @@ -325,6 +413,28 @@ bool isCrashHandlingInProgress() return s_crashHandlingInProgress; } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void recordQtMessage( QtMsgType type, const QString& msg ) +{ + if ( type == QtDebugMsg || type == QtInfoMsg ) return; + + // Spin lock with try-semantics: this may be called from any thread, and must never block the message handler + if ( s_qtMessageLock.test_and_set( std::memory_order_acquire ) ) return; + + auto text = msg.left( s_maxQtMessageLength ).toStdString(); + + // Skip repeats of the previous message, so a warning in a hot loop cannot flush out the earlier context + if ( s_recentQtMessages.empty() || s_recentQtMessages.back() != text ) + { + s_recentQtMessages.push_back( std::move( text ) ); + if ( s_recentQtMessages.size() > s_maxRecordedQtMessages ) s_recentQtMessages.pop_front(); + } + + s_qtMessageLock.clear( std::memory_order_release ); +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationExeCode/RiaMainTools.h b/ApplicationExeCode/RiaMainTools.h index 164e203d04..9c95d1f985 100644 --- a/ApplicationExeCode/RiaMainTools.h +++ b/ApplicationExeCode/RiaMainTools.h @@ -17,6 +17,10 @@ ///////////////////////////////////////////////////////////////////////////////// #pragma once +#include + +class QString; + namespace RiaMainTools { void initializeSingletons(); @@ -27,4 +31,8 @@ void deleteStaleSettingsLockFiles(); // telemetry, so timer-driven application logic must check this flag and stay idle to avoid // re-entering or tearing down objects the crashed code still uses. bool isCrashHandlingInProgress(); + +// Remember the latest Qt warning/critical/fatal messages. They are included in crash reports, as +// Qt prints the reason for an abort (e.g. lost GL context) to stderr right before aborting. +void recordQtMessage( QtMsgType type, const QString& msg ); }; // namespace RiaMainTools diff --git a/ApplicationLibCode/Application/Tools/Telemetry/RiaOpenTelemetryManager.cpp b/ApplicationLibCode/Application/Tools/Telemetry/RiaOpenTelemetryManager.cpp index cba53b16f2..85662f6bb7 100644 --- a/ApplicationLibCode/Application/Tools/Telemetry/RiaOpenTelemetryManager.cpp +++ b/ApplicationLibCode/Application/Tools/Telemetry/RiaOpenTelemetryManager.cpp @@ -24,6 +24,7 @@ #include "RifJsonEncodeDecode.h" #include +#include #include #include #include @@ -432,14 +433,16 @@ void RiaOpenTelemetryManager::reportCrash( int signalCode, const std::stacktrace auto* prefs = RiaPreferencesOpenTelemetry::current(); int timeoutMs = prefs ? prefs->connectionTimeoutMs() : 5000; - int elapsedMs = 0; int pollIntervalMs = 50; - while ( m_pendingReplies.load() > 0 && elapsedMs < timeoutMs ) + + // Measure real elapsed time: each loop.exec() can take longer than the poll interval + QElapsedTimer timer; + timer.start(); + while ( m_pendingReplies.load() > 0 && timer.elapsed() < timeoutMs ) { QEventLoop loop; QTimer::singleShot( pollIntervalMs, &loop, &QEventLoop::quit ); loop.exec( QEventLoop::ExcludeUserInputEvents ); - elapsedMs += pollIntervalMs; } }