提交 958ca00a 编写于 作者: J Jared Parsons

Guard against EventListener dispose race

The underlying issue here is EventListener.DisposeOnShutdown has a race
condition if a new EventSource is added during an AppDomain or Process
exit.  When this occurs there is an unhandled exception during shutdown
due to a modified collection during enumeration that causes xunit to
falsely fail the run.

The type CDSCollectionETWBCLProvider triggers this bug in our tests.  It
is an EventSource for concurrent collections that is loaded on many
concurrent collection operations.  These operations are not triggered
directly in some of our tests and hence lead to the race.

This test guards against them force loading, albeit indirectly, the EventSource
instance of CDSCollectionETWBCLProvider.  Hence uses during shutdown
are just re-using this instance and don't trigger the race.

closes #8936
上级 df01b581
......@@ -74,6 +74,9 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\Test\Shared\EventListenerGuard.cs">
<Link>EventListenerGuard.cs</Link>
</Compile>
<Compile Include="InteractiveWindowEditorsFactoryService.cs" />
<Compile Include="InteractiveWindowTestHost.cs" />
<Compile Include="HistoryTests.cs" />
......@@ -98,4 +101,4 @@
<Import Project="..\..\..\build\Targets\VSL.Imports.targets" />
<Import Project="..\..\..\build\Targets\Roslyn.Toolsets.Xunit.targets" />
</ImportGroup>
</Project>
</Project>
\ No newline at end of file
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Roslyn.Test.Utilities;
using System;
using System.Collections.Concurrent;
using Xunit;
namespace Microsoft.CodeAnalysis.UnitTests
{
public class EventListenerGuard
{
/// <summary>
/// A unit test that guards against the EventListener race condition:
///
/// - https://github.com/dotnet/roslyn/issues/8936
/// - https://github.com/dotnet/corefx/issues/3793
///
/// The underlying issue here is EventListener.DisposeOnShutdown has a race
/// condition if a new EventSource is added during an AppDomain or Process
/// exit. When this occurs there is an unhandled exception during shutdown
/// due to a modified collection during enumeration that causes xunit to
/// falsely fail the run.
///
/// The type CDSCollectionETWBCLProvider triggers this bug in our tests. It
/// is an EventSource for concurrent collections that is loaded on many
/// concurrent collection operations. These operations are not triggered
/// directly in some of our tests and hence lead to the race.
///
/// This test guards against them force loading, albeit indirectly, the EventSource
/// instance of CDSCollectionETWBCLProvider. Hence uses during shutdown
/// are just re-using this instance and don't trigger the race.
/// </summary>
[WorkItem(8936, "https://github.com/dotnet/roslyn/issues/8936")]
[Fact]
public void GuardAgainstRace()
{
// This code will trigger the load of CDSCollectionETWBCLProvider
var dictionary = new ConcurrentDictionary<int, int>();
dictionary.Clear();
var log = typeof(ConcurrentDictionary<int, int>)
.Assembly
.GetType("System.Collections.Concurrent.CDSCollectionETWBCLProvider")
.GetField("Log", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
.GetValue(null);
Assert.NotNull(log);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册