From 5d19d8500aefff295d1dd184bce9579dc78c744f Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Sun, 12 Aug 2018 18:35:48 -0700 Subject: [PATCH] Fix an apartment state issue (partial port of #19384) Partial port of https://github.com/dotnet/coreclr/pull/19384 to 2.2 Fix for https://github.com/dotnet/coreclr/issues/17822 - The apartment state now defaults to MTA for the main thread along with a CoInitialize - Calling `Thread.SetApartmentState` with STA now fails as expected (different behavior from previous netcore, same behavior as netfx) Fix for https://github.com/dotnet/coreclr/issues/19225 --- src/vm/appdomain.cpp | 3 ++- src/vm/assembly.cpp | 6 +----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/vm/appdomain.cpp b/src/vm/appdomain.cpp index 67b79ec19d2c..cf5b2ce51dda 100644 --- a/src/vm/appdomain.cpp +++ b/src/vm/appdomain.cpp @@ -3078,8 +3078,9 @@ void SystemDomain::SetThreadAptState (Thread::ApartmentState state) Thread::ApartmentState pState = pThread->SetApartment(Thread::AS_InSTA, TRUE); _ASSERTE(pState == Thread::AS_InSTA); } - else if (state == Thread::AS_InMTA) + else { + // If an apartment state was not explicitly requested, default to MTA Thread::ApartmentState pState = pThread->SetApartment(Thread::AS_InMTA, TRUE); _ASSERTE(pState == Thread::AS_InMTA); } diff --git a/src/vm/assembly.cpp b/src/vm/assembly.cpp index e93d2c34669c..402b2b8c633f 100644 --- a/src/vm/assembly.cpp +++ b/src/vm/assembly.cpp @@ -1798,11 +1798,7 @@ INT32 Assembly::ExecuteMainMethod(PTRARRAYREF *stringArgs, BOOL waitForOtherThre Thread::ApartmentState state = Thread::AS_Unknown; state = SystemDomain::GetEntryPointThreadAptState(pMeth->GetMDImport(), pMeth->GetMemberDef()); - - // If the entry point has an explicit thread apartment state, set it - // before running the AppDomainManager initialization code. - if (state == Thread::AS_InSTA || state == Thread::AS_InMTA) - SystemDomain::SetThreadAptState(state); + SystemDomain::SetThreadAptState(state); #endif // FEATURE_COMINTEROP }