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

Prepare the COM apartment and context at a safe time #52745

Merged
1 commit merged into from
May 14, 2021
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
18 changes: 11 additions & 7 deletions src/coreclr/vm/threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ Thread* SetupThread()

SetupTLSForThread();
pThread->InitThread();
pThread->PrepareApartmentAndContext();

// reset any unstarted bits on the thread object
FastInterlockAnd((ULONG *) &pThread->m_State, ~Thread::TS_Unstarted);
Expand Down Expand Up @@ -798,9 +799,6 @@ Thread* SetupThread()
FastInterlockOr((ULONG *) &pThread->m_State, Thread::TS_TPWorkerThread);
}

// Initialize the thread for the platform as the final step.
pThread->FinishInitialization();

#ifdef FEATURE_EVENT_TRACE
ETW::ThreadLog::FireThreadCreated(pThread);
#endif // FEATURE_EVENT_TRACE
Expand Down Expand Up @@ -1821,12 +1819,18 @@ BOOL Thread::HasStarted()

InitThread();

fCanCleanupCOMState = TRUE;
// Preparing the COM apartment and context may attempt
// to transition to Preemptive mode. At this point in
// the thread's lifetime this can be a bad thing if a GC
// is triggered (e.g. GCStress). Do the preparation prior
// to the thread being set so the Preemptive mode transition
// is a no-op.
PrepareApartmentAndContext();

SetThread(this);
SetAppDomain(m_pDomain);

fCanCleanupCOMState = TRUE;
FinishInitialization();

ThreadStore::TransferStartedThread(this);

#ifdef FEATURE_EVENT_TRACE
Expand Down Expand Up @@ -4709,7 +4713,7 @@ class ApartmentSpyImpl : public IUnknownCommon<IInitializeSpy, IID_IInitializeSp
};
#endif // FEATURE_COMINTEROP

void Thread::FinishInitialization()
void Thread::PrepareApartmentAndContext()
{
CONTRACTL {
THROWS;
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ class Thread

#endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT

void FinishInitialization();
void PrepareApartmentAndContext();

#ifdef FEATURE_COMINTEROP
bool IsDisableComObjectEagerCleanup()
Expand Down