forked from dotnet/coreclr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a couple of apartment state issues
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/17787 - `WaitHandle.WaitAll` for multiple handles is not supported on an STA thread due to issues described in https://github.com/dotnet/coreclr/issues/17787#issuecomment-385117537 - It now throws `NotSupportedException` as expected (different behavior from previous netcore, same behavior as netfx) Fix for https://github.com/dotnet/coreclr/issues/19225
- Loading branch information
Showing
13 changed files
with
309 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
tests/src/baseservices/threading/ApartmentState/SetApartmentStateDefaultMta.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
public static class SetApartmentStateDefaultMtaTests | ||
{ | ||
public static int Main() | ||
{ | ||
if (CannotChangeApartmentStateOfMainThreadTest()) | ||
{ | ||
return 100; // pass | ||
} | ||
return 101; // fail | ||
} | ||
|
||
private static bool CannotChangeApartmentStateOfMainThreadTest() | ||
{ | ||
var thread = Thread.CurrentThread; | ||
try | ||
{ | ||
thread.SetApartmentState(ApartmentState.STA); | ||
|
||
Console.WriteLine("CannotChangeApartmentStateOfMainThreadTest: Unexpected success (no exception): 'SetApartmentState(ApartmentState.STA)'"); | ||
return false; | ||
} | ||
catch (InvalidOperationException) | ||
{ | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"CannotChangeApartmentStateOfMainThreadTest: Unexpected exception during 'SetApartmentState(ApartmentState.STA)': {ex}"); | ||
return false; | ||
} | ||
|
||
try | ||
{ | ||
thread.SetApartmentState(ApartmentState.MTA); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"CannotChangeApartmentStateOfMainThreadTest: Unexpected exception during 'SetApartmentState(ApartmentState.MTA)': {ex}"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/src/baseservices/threading/ApartmentState/SetApartmentStateDefaultMta.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{0F4DD32F-EC7D-4AEF-9C6F-0C40E7CC7F2C}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
<!-- Default configurations to help VS understand the configurations --> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="SetApartmentStateDefaultMta.cs" /> | ||
</ItemGroup> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> | ||
</Project> |
51 changes: 51 additions & 0 deletions
51
tests/src/baseservices/threading/ApartmentState/SetApartmentStateMta.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
public static class SetApartmentStateMtaTests | ||
{ | ||
[MTAThread] | ||
public static int Main() | ||
{ | ||
if (CannotChangeApartmentStateOfMainThreadTest()) | ||
{ | ||
return 100; // pass | ||
} | ||
return 101; // fail | ||
} | ||
|
||
private static bool CannotChangeApartmentStateOfMainThreadTest() | ||
{ | ||
var thread = Thread.CurrentThread; | ||
try | ||
{ | ||
thread.SetApartmentState(ApartmentState.STA); | ||
|
||
Console.WriteLine("CannotChangeApartmentStateOfMainThreadTest: Unexpected success (no exception): 'SetApartmentState(ApartmentState.STA)'"); | ||
return false; | ||
} | ||
catch (InvalidOperationException) | ||
{ | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"CannotChangeApartmentStateOfMainThreadTest: Unexpected exception during 'SetApartmentState(ApartmentState.STA)': {ex}"); | ||
return false; | ||
} | ||
|
||
try | ||
{ | ||
thread.SetApartmentState(ApartmentState.MTA); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"CannotChangeApartmentStateOfMainThreadTest: Unexpected exception during 'SetApartmentState(ApartmentState.MTA)': {ex}"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/src/baseservices/threading/ApartmentState/SetApartmentStateMta.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{D5F2231F-6DEA-4DB2-AF79-120CABAED49B}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
<!-- Default configurations to help VS understand the configurations --> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="SetApartmentStateMta.cs" /> | ||
</ItemGroup> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> | ||
</Project> |
51 changes: 51 additions & 0 deletions
51
tests/src/baseservices/threading/ApartmentState/SetApartmentStateSta.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
public static class SetApartmentStateStaTests | ||
{ | ||
[STAThread] | ||
public static int Main() | ||
{ | ||
if (CannotChangeApartmentStateOfMainThreadTest()) | ||
{ | ||
return 100; // pass | ||
} | ||
return 101; // fail | ||
} | ||
|
||
private static bool CannotChangeApartmentStateOfMainThreadTest() | ||
{ | ||
var thread = Thread.CurrentThread; | ||
try | ||
{ | ||
thread.SetApartmentState(ApartmentState.MTA); | ||
|
||
Console.WriteLine("CannotChangeApartmentStateOfMainThreadTest: Unexpected success (no exception): 'SetApartmentState(ApartmentState.MTA)'"); | ||
return false; | ||
} | ||
catch (InvalidOperationException) | ||
{ | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"CannotChangeApartmentStateOfMainThreadTest: Unexpected exception during 'SetApartmentState(ApartmentState.MTA)': {ex}"); | ||
return false; | ||
} | ||
|
||
try | ||
{ | ||
thread.SetApartmentState(ApartmentState.STA); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"CannotChangeApartmentStateOfMainThreadTest: Unexpected exception during 'SetApartmentState(ApartmentState.STA)': {ex}"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/src/baseservices/threading/ApartmentState/SetApartmentStateSta.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{6EE5C24D-CB8C-42AD-9D44-B49D584FB173}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
<!-- Default configurations to help VS understand the configurations --> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="SetApartmentStateSta.cs" /> | ||
</ItemGroup> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> | ||
</Project> |
66 changes: 66 additions & 0 deletions
66
tests/src/baseservices/threading/ApartmentState/WaitAllSta.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Threading; | ||
|
||
public static class WaitAllStaTests | ||
{ | ||
[STAThread] | ||
public static int Main() | ||
{ | ||
if (WaitAllNotSupportedOnSta_Test0() && | ||
WaitAllNotSupportedOnSta_Test1()) | ||
{ | ||
return 100; // pass | ||
} | ||
return 101; // fail | ||
} | ||
|
||
private static bool WaitAllNotSupportedOnSta_Test0() | ||
{ | ||
var wh = new ManualResetEvent[2]; | ||
wh[0] = new ManualResetEvent(true); | ||
wh[1] = new ManualResetEvent(true); | ||
try | ||
{ | ||
bool result = WaitHandle.WaitAll(wh, 0); | ||
|
||
Console.WriteLine($"WaitAllNotSupportedOnSta_Test0: WaitAll did not throw but returned {result}"); | ||
} | ||
catch (NotSupportedException) | ||
{ | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"WaitAllNotSupportedOnSta_Test0: WaitAll threw unexpected exception: {ex}"); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static bool WaitAllNotSupportedOnSta_Test1() | ||
{ | ||
var wh = new ManualResetEvent[2]; | ||
wh[0] = new ManualResetEvent(true); | ||
wh[1] = wh[0]; | ||
try | ||
{ | ||
bool result = WaitHandle.WaitAll(wh, 0); | ||
|
||
Console.WriteLine($"WaitAllNotSupportedOnSta_Test1: WaitAll did not throw but returned {result}"); | ||
} | ||
catch (NotSupportedException) | ||
{ | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"WaitAllNotSupportedOnSta_Test1: WaitAll threw unexpected exception: {ex}"); | ||
} | ||
|
||
return false; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/src/baseservices/threading/ApartmentState/WaitAllSta.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{CEC647A3-9A72-42C6-8023-22670F63E7B2}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
<!-- Default configurations to help VS understand the configurations --> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="WaitAllSta.cs" /> | ||
</ItemGroup> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters