Skip to content

Commit dff9c90

Browse files
[release/7.0] [NativeAOT] do not do shutdown for the main thread on Windows (#74712)
* do not do shutdown for the main thread * specialcase the thread that called RhpShutdown instead Co-authored-by: vsadov <[email protected]>
1 parent bdb78f4 commit dff9c90

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/coreclr/nativeaot/Runtime/startup.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ static void UninitDLL()
401401
#endif // PROFILE_STARTUP
402402
}
403403

404-
volatile bool g_processShutdownHasStarted = false;
404+
volatile Thread* g_threadPerformingShutdown = NULL;
405405

406406
static void DllThreadDetach()
407407
{
@@ -413,7 +413,7 @@ static void DllThreadDetach()
413413
{
414414
// Once shutdown starts, RuntimeThreadShutdown callbacks are ignored, implying that
415415
// it is no longer guaranteed that exiting threads will be detached.
416-
if (!g_processShutdownHasStarted)
416+
if (g_threadPerformingShutdown != NULL)
417417
{
418418
ASSERT_UNCONDITIONALLY("Detaching thread whose home fiber has not been detached");
419419
RhFailFast();
@@ -439,9 +439,17 @@ void RuntimeThreadShutdown(void* thread)
439439
}
440440
#else
441441
ASSERT((Thread*)thread == ThreadStore::GetCurrentThread());
442+
443+
// Do not do shutdown for the thread that performs the shutdown.
444+
// other threads could be terminated before it and could leave TLS locked
445+
if ((Thread*)thread == g_threadPerformingShutdown)
446+
{
447+
return;
448+
}
449+
442450
#endif
443451

444-
ThreadStore::DetachCurrentThread(g_processShutdownHasStarted);
452+
ThreadStore::DetachCurrentThread(g_threadPerformingShutdown != NULL);
445453
}
446454

447455
extern "C" bool RhInitialize()
@@ -474,11 +482,11 @@ COOP_PINVOKE_HELPER(void, RhpEnableConservativeStackReporting, ())
474482
COOP_PINVOKE_HELPER(void, RhpShutdown, ())
475483
{
476484
// Indicate that runtime shutdown is complete and that the caller is about to start shutting down the entire process.
477-
g_processShutdownHasStarted = true;
485+
g_threadPerformingShutdown = ThreadStore::RawGetCurrentThread();
478486
}
479487

480488
#ifdef _WIN32
481-
EXTERN_C UInt32_BOOL WINAPI RtuDllMain(HANDLE hPalInstance, uint32_t dwReason, void* /*pvReserved*/)
489+
EXTERN_C UInt32_BOOL WINAPI RtuDllMain(HANDLE hPalInstance, uint32_t dwReason, void* pvReserved)
482490
{
483491
switch (dwReason)
484492
{

0 commit comments

Comments
 (0)