Skip to content

Commit

Permalink
Fix concurrency issue with registering callback on TestRunCancellatio…
Browse files Browse the repository at this point in the history
…nToken (#3958)
  • Loading branch information
Evangelink committed Oct 21, 2024
1 parent ac7fffb commit 53c3c4e
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Concurrent;

namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;

/// <summary>
Expand All @@ -10,8 +12,9 @@ public class TestRunCancellationToken
{
/// <summary>
/// Callbacks to be invoked when canceled.
/// Needs to be a concurrent collection, see https://github.com/microsoft/testfx/issues/3953.
/// </summary>
private readonly List<Action> _registeredCallbacks = new();
private readonly ConcurrentBag<Action> _registeredCallbacks = new();

/// <summary>
/// Stores whether the test run is canceled or not.
Expand Down Expand Up @@ -63,7 +66,17 @@ private set
/// <summary>
/// Unregister the callback method.
/// </summary>
public void Unregister() => _registeredCallbacks.Clear();
public void Unregister()
#if NETCOREAPP || WINDOWS_UWP
=> _registeredCallbacks.Clear();
#else
{
while (!_registeredCallbacks.IsEmpty)
{
_ = _registeredCallbacks.TryTake(out _);
}
}
#endif

internal void ThrowIfCancellationRequested()
{
Expand Down

0 comments on commit 53c3c4e

Please sign in to comment.