Skip to content

Commit 3516014

Browse files
Ensure LoaderAllocator can't be collected while we clean handles on collectible LoaderAllocators (#102872)
(cherry picked from commit eb84218)
1 parent 32400f6 commit 3516014

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/coreclr/vm/threadstatics.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,22 @@ void ThreadLocalBlock::FreeTLM(SIZE_T i, BOOL isThreadShuttingdown)
5252
ThreadLocalModule::CollectibleDynamicEntry *entry = (ThreadLocalModule::CollectibleDynamicEntry*)pThreadLocalModule->m_pDynamicClassTable[k].m_pDynamicEntry;
5353
PTR_LoaderAllocator pLoaderAllocator = entry->m_pLoaderAllocator;
5454

55-
if (entry->m_hGCStatics != NULL)
56-
{
57-
pLoaderAllocator->FreeHandle(entry->m_hGCStatics);
58-
}
59-
if (entry->m_hNonGCStatics != NULL)
55+
// LoaderAllocator may be collected when the thread is shutting down.
56+
// We enter coop mode to ensure that we get a valid value of the exposed object and
57+
// can safely clean up handles if it is not yet collected.
58+
GCX_COOP();
59+
60+
LOADERALLOCATORREF loaderAllocator = pLoaderAllocator->GetExposedObject();
61+
if (loaderAllocator != NULL)
6062
{
61-
pLoaderAllocator->FreeHandle(entry->m_hNonGCStatics);
63+
if (entry->m_hGCStatics != NULL)
64+
{
65+
pLoaderAllocator->FreeHandle(entry->m_hGCStatics);
66+
}
67+
if (entry->m_hNonGCStatics != NULL)
68+
{
69+
pLoaderAllocator->FreeHandle(entry->m_hNonGCStatics);
70+
}
6271
}
6372
}
6473
delete pThreadLocalModule->m_pDynamicClassTable[k].m_pDynamicEntry;

0 commit comments

Comments
 (0)