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

Avoid clearing thread local handles for already unloaded loader #99998

Merged
Merged
Show file tree
Hide file tree
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
4 changes: 1 addition & 3 deletions src/coreclr/vm/loaderallocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,13 +964,11 @@ void LoaderAllocator::SetHandleValue(LOADERHANDLE handle, OBJECTREF value)
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
MODE_COOPERATIVE;
PRECONDITION(handle != NULL);
}
CONTRACTL_END;

GCX_COOP();

GCPROTECT_BEGIN(value);

// If the slot value does have the low bit set, then it is a simple pointer to the value
Expand Down
21 changes: 15 additions & 6 deletions src/coreclr/vm/threadstatics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,22 @@ void ThreadLocalBlock::FreeTLM(SIZE_T i, BOOL isThreadShuttingdown)
ThreadLocalModule::CollectibleDynamicEntry *entry = (ThreadLocalModule::CollectibleDynamicEntry*)pThreadLocalModule->m_pDynamicClassTable[k].m_pDynamicEntry;
PTR_LoaderAllocator pLoaderAllocator = entry->m_pLoaderAllocator;

if (entry->m_hGCStatics != 0)
{
pLoaderAllocator->FreeHandle(entry->m_hGCStatics);
}
if (entry->m_hNonGCStatics != 0)
// LoaderAllocator may be collected when the thread is shutting down.
// We enter coop mode to ensure that we get a valid value of the exposed object and
// can safely clean up handles if it is not yet collected.
GCX_COOP();

LOADERALLOCATORREF loaderAllocator = pLoaderAllocator->GetExposedObject();
if (loaderAllocator != NULL)
{
pLoaderAllocator->FreeHandle(entry->m_hNonGCStatics);
if (entry->m_hGCStatics != 0)
{
pLoaderAllocator->FreeHandle(entry->m_hGCStatics);
}
if (entry->m_hNonGCStatics != 0)
{
pLoaderAllocator->FreeHandle(entry->m_hNonGCStatics);
}
}
}
delete pThreadLocalModule->m_pDynamicClassTable[k].m_pDynamicEntry;
Expand Down
Loading