Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sleep infinitely on secondary crashing threads #72618

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions src/coreclr/vm/eepolicy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,25 +325,31 @@ void LogInfoForFatalError(UINT exitCode, LPCWSTR pszMessage, LPCWSTR errorSource
{
WRAPPER_NO_CONTRACT;

static Thread *const FatalErrorNotSeenYet = nullptr;
static Thread *const FatalErrorLoggingFinished = reinterpret_cast<Thread *>(1);
static size_t s_pCrashingThreadID;

static Thread *volatile s_pCrashingThread = FatalErrorNotSeenYet;
size_t currentThreadID;
#ifndef TARGET_UNIX
currentThreadID = GetCurrentThreadId();
#else
currentThreadID = PAL_GetCurrentOSThreadId();
#endif

Thread *pThread = GetThreadNULLOk();
Thread *pPreviousThread = InterlockedCompareExchangeT<Thread *>(&s_pCrashingThread, pThread, FatalErrorNotSeenYet);
size_t previousThreadID = InterlockedCompareExchangeT<size_t>(&s_pCrashingThreadID, currentThreadID, 0);

if (pPreviousThread == pThread)
{
PrintToStdErrA("Fatal error while logging another fatal error.\n");
return;
}
else if (pPreviousThread != nullptr)
// Let the first crashing thread take care of the reporting.
if (previousThreadID != 0)
{
GCX_PREEMP(); // Avoid blocking other threads that may be trying to suspend the runtime
while (s_pCrashingThread != FatalErrorLoggingFinished)
if (previousThreadID == currentThreadID)
{
PrintToStdErrA("Fatal error while logging another fatal error.\n");
}
else
{
ClrSleepEx(50, /*bAlertable*/ FALSE);
// Switch to preemptive mode to avoid blocking the crashing thread. It may try to suspend the runtime
// for GC during the stacktrace reporting.
GCX_PREEMP();

ClrSleepEx(INFINITE, /*bAlertable*/ FALSE);
}
return;
}
Expand Down Expand Up @@ -379,9 +385,10 @@ void LogInfoForFatalError(UINT exitCode, LPCWSTR pszMessage, LPCWSTR errorSource

PrintToStdErrA("\n");

Thread* pThread = GetThreadNULLOk();
if (pThread && errorSource == NULL)
{
LogCallstackForLogWorker(GetThread());
LogCallstackForLogWorker(pThread);

if (argExceptionString != NULL) {
PrintToStdErrW(argExceptionString);
Expand All @@ -392,8 +399,6 @@ void LogInfoForFatalError(UINT exitCode, LPCWSTR pszMessage, LPCWSTR errorSource
{
}
EX_END_CATCH(SwallowAllExceptions)

InterlockedCompareExchangeT<Thread *>(&s_pCrashingThread, FatalErrorLoggingFinished, pThread);
}

//This starts FALSE and then converts to true if HandleFatalError has ever been called by a GC thread
Expand Down