@@ -401,7 +401,7 @@ static void UninitDLL()
401
401
#endif // PROFILE_STARTUP
402
402
}
403
403
404
- volatile bool g_processShutdownHasStarted = false ;
404
+ volatile Thread* g_threadPerformingShutdown = NULL ;
405
405
406
406
static void DllThreadDetach ()
407
407
{
@@ -413,7 +413,7 @@ static void DllThreadDetach()
413
413
{
414
414
// Once shutdown starts, RuntimeThreadShutdown callbacks are ignored, implying that
415
415
// it is no longer guaranteed that exiting threads will be detached.
416
- if (!g_processShutdownHasStarted )
416
+ if (g_threadPerformingShutdown != NULL )
417
417
{
418
418
ASSERT_UNCONDITIONALLY (" Detaching thread whose home fiber has not been detached" );
419
419
RhFailFast ();
@@ -439,9 +439,17 @@ void RuntimeThreadShutdown(void* thread)
439
439
}
440
440
#else
441
441
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
+
442
450
#endif
443
451
444
- ThreadStore::DetachCurrentThread (g_processShutdownHasStarted );
452
+ ThreadStore::DetachCurrentThread (g_threadPerformingShutdown != NULL );
445
453
}
446
454
447
455
extern " C" bool RhInitialize ()
@@ -474,11 +482,11 @@ COOP_PINVOKE_HELPER(void, RhpEnableConservativeStackReporting, ())
474
482
COOP_PINVOKE_HELPER (void , RhpShutdown, ())
475
483
{
476
484
// 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 () ;
478
486
}
479
487
480
488
#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)
482
490
{
483
491
switch (dwReason)
484
492
{
0 commit comments