-
Notifications
You must be signed in to change notification settings - Fork 351
Report in-flight tests and suggest --blame-crash when testhost crashes #15680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nohwnd
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
nohwnd:improve-crash-diagnostics-2952
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
198 changes: 198 additions & 0 deletions
198
src/Microsoft.TestPlatform.CommunicationUtilities/InFlightTestTracker.cs
This file contains hidden or 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,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; | ||
| } | ||
| } | ||
|
|
||
| /// <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; | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
src/Microsoft.TestPlatform.CommunicationUtilities/Interfaces/ITestCaseLifecycleNotifier.cs
This file contains hidden or 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,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); | ||
| } |
This file contains hidden or 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 hidden or 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
20 changes: 20 additions & 0 deletions
20
src/Microsoft.TestPlatform.CommunicationUtilities/Messages/TestCaseFinishedPayload.cs
This file contains hidden or 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,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; } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/Microsoft.TestPlatform.CommunicationUtilities/Messages/TestCaseStartingPayload.cs
This file contains hidden or 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,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; } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/Microsoft.TestPlatform.CommunicationUtilities/PublicAPI/PublicAPI.Unshipped.txt
This file contains hidden or 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 |
|---|---|---|
| @@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
InFlightTestmutates multiple fields (slot shifting + overflow queue) without any synchronization, whileInFlightTestTrackeris explicitly used in a multi-threaded context (message receive thread updating vs abort thread reading). This can lead to torn reads, inconsistent state, or evenNullReferenceExceptioninGetAll()(e.g., a slot becomes null after theis not nullcheck but before dereferencing). Consider adding a lock insideInFlightTestthat protectsAdd,RemoveOldest, andGetAll(or copying all slot references/start times to locals under lock before yielding), and similarly guarding overflow queue access.