Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.EventHandle
/// <summary>
/// The test run events handler.
/// </summary>
public class TestRunEventsHandler : IInternalTestRunEventsHandler
public class TestRunEventsHandler : IInternalTestRunEventsHandler, ITestCaseLifecycleNotifier
{
private readonly ITestRequestHandler _requestHandler;

Expand Down Expand Up @@ -104,4 +104,12 @@ public bool AttachDebuggerToProcess(AttachDebuggerInfo attachDebuggerInfo)
EqtTrace.Info("Sending AttachDebuggerToProcess on additional test process with pid: {0}", attachDebuggerInfo.ProcessId);
return _requestHandler.AttachDebuggerToProcess(attachDebuggerInfo);
}

/// <inheritdoc/>
void ITestCaseLifecycleNotifier.SendTestCaseStarting(TestCase testCase)
=> _requestHandler.SendTestCaseStarting(testCase);

/// <inheritdoc/>
void ITestCaseLifecycleNotifier.SendTestCaseFinished(TestCase testCase)
=> _requestHandler.SendTestCaseFinished(testCase);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// 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;
using System.Collections.Concurrent;
using System.Collections.Generic;

using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;

/// <summary>
/// Tracks a set of in-flight test executions that share the same <see cref="Guid"/>.
/// Uses inline slots to avoid queue allocation in the common case (unique Guid per test).
/// All mutation and enumeration is guarded by a lock because the message-receive thread
/// updates slots while the abort thread may read them concurrently via <see cref="GetAll"/>.
/// </summary>
internal sealed class InFlightTest
{
private readonly object _lock = new();
public TestCaseStartingPayload Slot0;
public DateTimeOffset StartTime0;
public TestCaseStartingPayload? Slot1;
public DateTimeOffset StartTime1;
public TestCaseStartingPayload? Slot2;
public DateTimeOffset StartTime2;
public TestCaseStartingPayload? Slot3;
public DateTimeOffset StartTime3;
public Queue<(TestCaseStartingPayload Payload, DateTimeOffset StartTime)>? Overflow;

public InFlightTest(TestCaseStartingPayload payload, DateTimeOffset startTime)
{
Slot0 = payload;
StartTime0 = startTime;
}

/// <summary>
/// Adds another in-flight execution for the same Guid.
/// </summary>
public void Add(TestCaseStartingPayload payload, DateTimeOffset startTime)
{
lock (_lock)
{
if (Slot1 is null)
{
Slot1 = payload;
StartTime1 = startTime;
}
else if (Slot2 is null)
{
Slot2 = payload;
StartTime2 = startTime;
}
else if (Slot3 is null)
{
Slot3 = payload;
StartTime3 = startTime;
}
else
{
Overflow ??= new Queue<(TestCaseStartingPayload, DateTimeOffset)>();
Overflow.Enqueue((payload, startTime));
}
}
}

/// <summary>
/// Removes the oldest in-flight execution (FIFO). Returns true if there are still entries remaining.
/// </summary>
public bool RemoveOldest()
{
lock (_lock)
{
// Shift slots down
if (Slot1 is not null)
{
Slot0 = Slot1;
StartTime0 = StartTime1;
Slot1 = Slot2;
StartTime1 = StartTime2;
Slot2 = Slot3;
StartTime2 = StartTime3;
Slot3 = null;
StartTime3 = default;

// Refill Slot3 from overflow if available
if (Overflow is { Count: > 0 })
{
var (payload, startTime) = Overflow.Dequeue();
Slot3 = payload;
StartTime3 = startTime;
}

return true;
}

// Slot0 was the only entry
return false;
}
Comment on lines +70 to +99
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InFlightTest mutates multiple fields (slot shifting + overflow queue) without any synchronization, while InFlightTestTracker is explicitly used in a multi-threaded context (message receive thread updating vs abort thread reading). This can lead to torn reads, inconsistent state, or even NullReferenceException in GetAll() (e.g., a slot becomes null after the is not null check but before dereferencing). Consider adding a lock inside InFlightTest that protects Add, RemoveOldest, and GetAll (or copying all slot references/start times to locals under lock before yielding), and similarly guarding overflow queue access.

Copilot uses AI. Check for mistakes.
}

/// <summary>
/// Returns a snapshot of all in-flight entries with their display name and start time.
/// </summary>
public IReadOnlyList<(string? DisplayName, DateTimeOffset StartTime)> GetAll()
{
lock (_lock)
{
var result = new List<(string?, DateTimeOffset)>();
result.Add((Slot0.DisplayName, StartTime0));

if (Slot1 is not null)
{
result.Add((Slot1.DisplayName, StartTime1));
}

if (Slot2 is not null)
{
result.Add((Slot2.DisplayName, StartTime2));
}

if (Slot3 is not null)
{
result.Add((Slot3.DisplayName, StartTime3));
}

if (Overflow is not null)
{
foreach (var (payload, startTime) in Overflow)
{
result.Add((payload.DisplayName, startTime));
}
}

return result;
}
}
}

/// <summary>
/// Thread-safe tracker for tests currently executing in a testhost.
/// Used by vstest.console to report which tests were running when a testhost crashes.
/// </summary>
internal sealed class InFlightTestTracker
{
private readonly ConcurrentDictionary<Guid, InFlightTest> _tests = new();

/// <summary>
/// Records that a test has started executing.
/// </summary>
public void TestStarting(TestCaseStartingPayload payload, DateTimeOffset startTime)
{
_tests.AddOrUpdate(
payload.Id,
_ => new InFlightTest(payload, startTime),
(_, existing) =>
{
existing.Add(payload, startTime);
return existing;
});
}

/// <summary>
/// Records that a test has finished executing. Removes the oldest execution for the given Guid.
/// </summary>
public void TestFinished(Guid testId)
{
if (_tests.TryGetValue(testId, out var inFlight))
{
if (!inFlight.RemoveOldest())
{
_tests.TryRemove(testId, out _);
}
}
}

/// <summary>
/// Gets all tests that are currently in-flight. Used when testhost crashes to report what was running.
/// </summary>
public IReadOnlyList<(string? DisplayName, DateTimeOffset StartTime)> GetInFlightTests()
{
var result = new List<(string?, DateTimeOffset)>();
foreach (var kvp in _tests)
{
foreach (var entry in kvp.Value.GetAll())
{
result.Add(entry);
}
}

return result;
}

/// <summary>
/// Returns true if there are any in-flight tests being tracked.
/// </summary>
public bool HasInFlightTests => !_tests.IsEmpty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.VisualStudio.TestPlatform.ObjectModel;

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;

/// <summary>
/// Provides lightweight test case lifecycle notifications for real-time in-flight tracking.
/// Implemented by event handlers that can relay start/finish signals to the console.
/// </summary>
internal interface ITestCaseLifecycleNotifier
{
void SendTestCaseStarting(TestCase testCase);

void SendTestCaseFinished(TestCase testCase);
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,18 @@ public interface ITestRequestHandler : IDisposable
/// <param name="attachDebuggerInfo">Process ID and tfm of the process to which the debugger should be attached.</param>
/// <returns><see langword="true"/> if the debugger was successfully attached to the requested process, <see langword="false"/> otherwise.</returns>
bool AttachDebuggerToProcess(AttachDebuggerInfo attachDebuggerInfo);

/// <summary>
/// Sends a lightweight notification that a test case has started executing.
/// Used for real-time in-flight test tracking on the console side.
/// </summary>
/// <param name="testCase">The test case that started.</param>
void SendTestCaseStarting(TestCase testCase);

/// <summary>
/// Sends a lightweight notification that a test case has finished executing.
/// Used for real-time in-flight test tracking on the console side.
/// </summary>
/// <param name="testCase">The test case that finished.</param>
void SendTestCaseFinished(TestCase testCase);
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ public static class MessageType
/// </summary>
public const string TestRunStatsChange = "TestExecution.StatsChange";

/// <summary>
/// Lightweight notification that a test case has started executing.
/// Sent unbatched from testhost to console for real-time in-flight test tracking.
/// </summary>
public const string TestCaseStarting = "TestExecution.TestCaseStarting";

/// <summary>
/// Lightweight notification that a test case has finished executing.
/// Sent unbatched from testhost to console for real-time in-flight test tracking.
/// </summary>
public const string TestCaseFinished = "TestExecution.TestCaseFinished";

/// <summary>
/// The execution complete.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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;
using System.Runtime.Serialization;

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;

/// <summary>
/// Payload for the TestCaseFinished message, sent from testhost to console when a test completes execution.
/// </summary>
[DataContract]
public class TestCaseFinishedPayload
{
/// <summary>
/// Gets or sets the test case ID.
/// </summary>
[DataMember]
public Guid Id { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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;
using System.Runtime.Serialization;

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;

/// <summary>
/// Payload for the TestCaseStarting message, sent from testhost to console when a test begins execution.
/// </summary>
[DataContract]
public class TestCaseStartingPayload
{
/// <summary>
/// Gets or sets the test case ID.
/// </summary>
[DataMember]
public Guid Id { get; set; }

/// <summary>
/// Gets or sets the display name of the test case.
/// </summary>
[DataMember]
public string? DisplayName { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#nullable enable
const Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.MessageType.TelemetryEventMessage = "TestPlatform.TelemetryEvent" -> string!
const Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.MessageType.TestCaseFinished = "TestExecution.TestCaseFinished" -> string!
const Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.MessageType.TestCaseStarting = "TestExecution.TestCaseStarting" -> string!
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces.ITestRequestHandler.SendTestCaseFinished(Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase! testCase) -> void
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces.ITestRequestHandler.SendTestCaseStarting(Microsoft.VisualStudio.TestPlatform.ObjectModel.TestCase! testCase) -> void
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseFinishedPayload
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseFinishedPayload.Id.get -> System.Guid
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseFinishedPayload.Id.set -> void
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseFinishedPayload.TestCaseFinishedPayload() -> void
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseStartingPayload
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseStartingPayload.DisplayName.get -> string?
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseStartingPayload.DisplayName.set -> void
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseStartingPayload.Id.get -> System.Guid
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseStartingPayload.Id.set -> void
Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel.TestCaseStartingPayload.TestCaseStartingPayload() -> void
~static Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources.Resources.ConnectionTimeoutProcessDidNotStartErrorMessage.get -> string
~static Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources.Resources.ConnectionTimeoutProcessExitedErrorMessage.get -> string
~static Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Resources.Resources.ConnectionTimeoutWithDetailsErrorMessage.get -> string
Loading