[fix] Fix datacollector crash visibility: replace Assert with throwable exceptions#16048
Open
nohwnd wants to merge 1 commit into
Open
[fix] Fix datacollector crash visibility: replace Assert with throwable exceptions#16048nohwnd wants to merge 1 commit into
nohwnd wants to merge 1 commit into
Conversation
…eptions When TestElement is null in TestCaseStarted/TestCaseEnded, TPDebug.Assert calls Debug.Assert which in debug builds terminates the process via Environment.FailFast, bypassing the try-catch in DataCollectionTestCaseEventHandler.ProcessRequests. This makes errors very hard to see. Replace the TPDebug.Assert null checks with explicit if/throw so the InvalidOperationException is caught by ProcessRequests, reported via _messageSink.SendMessage as a visible error, and the test run continues cleanly instead of crashing. Fixes #15622 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates DataCollectionManager to avoid TPDebug.Assert-triggered process termination when TestElement is missing, so failures become catchable/reportable errors instead of a silent datacollector crash.
Changes:
- Replace
TPDebug.Assert(...TestElement...)with explicitif/throw InvalidOperationExceptioninTestCaseStarted. - Replace
TPDebug.Assert(...TestElement...)with explicitif/throw InvalidOperationExceptioninTestCaseEnded.
| @@ -317,7 +317,11 @@ public void TestCaseStarted(TestCaseStartEventArgs testCaseStartEventArgs) | |||
| } | |||
|
|
|||
| TPDebug.Assert(_dataCollectionEnvironmentContext is not null, "_dataCollectionEnvironmentContext is null"); | |||
| @@ -333,7 +337,11 @@ public Collection<AttachmentSet> TestCaseEnded(TestCaseEndEventArgs testCaseEndE | |||
| } | |||
|
|
|||
| TPDebug.Assert(_dataCollectionEnvironmentContext is not null, "_dataCollectionEnvironmentContext is null"); | |||
Comment on lines
+320
to
+323
| if (testCaseStartEventArgs.TestElement is null) | ||
| { | ||
| throw new InvalidOperationException("TestCaseStartEventArgs.TestElement is null. The data collector cannot start a test case without a test element."); | ||
| } |
nohwnd
commented
May 20, 2026
Member
Author
nohwnd
left a comment
There was a problem hiding this comment.
Review Summary
Dimensions activated: Backward Compatibility & Rollback Safety · Error Reporting & Diagnostic Clarity · Null Safety & Boundary Validation
The fix is correct and the PR description accurately describes the change.
Key verification points:
TPDebug.Assertis[Conditional("DEBUG")]— a no-op in release builds. In debug builds it delegates toDebug.Assert, which can callFailFastand bypass thetry/catchinProcessRequests. The newif/throw InvalidOperationExceptionenforces the null check in all build configurations.- Both new exceptions are caught by the existing
try/catchblocks inDataCollectionTestCaseEventHandler.ProcessRequests(TestCaseStarted handler at ~line 88, TestCaseEnded handler at ~line 112) and correctly surfaced via_messageSink.SendMessage(new DataCollectionMessageEventArgs(TestMessageLevel.Error, ...)). TestCaseEndedstill sendsDataCollectionTestEndResultwith an emptyAttachmentSeton error, which prevents the vstest.console caller from deadlocking while waiting for the result.- No public API surface changes;
IDataCollectionManagerinterface is unchanged.
No issues found. ✅
🧠 Reviewed by Expert Code Reviewer
🧠 Reviewed by Expert Code Reviewer 🧠
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🤖 This is an automated fix generated by the Issue Repro Triage & Auto-Fix workflow.
Fixes #15622
Root Cause
When
TestElementis null inDataCollectionManager.TestCaseStartedorTestCaseEnded, the code calledTPDebug.Assert(testCaseStartEventArgs.TestElement is not null, "...").TPDebug.Assertdelegates toDebug.Assert, which in debug builds (and when no debugger is attached) callsEnvironment.FailFastthrough the default trace listener. This terminates the datacollector process immediately, bypassing the try-catch inDataCollectionTestCaseEventHandler.ProcessRequests. As a result, the error surfaces only as a cryptic crash in stderr (Process terminated. Assertion failed.) that is easy to miss in test output.Fix
Replace the
TPDebug.Assertnull checks with explicitif/throwstatements that throwInvalidOperationException. Since these are inside thetry-catchinProcessRequests, the exception is:_messageSink.SendMessage(new DataCollectionMessageEventArgs(TestMessageLevel.Error, ...))— visible in test outputEqtTrace.ErrorTests
All 390
Microsoft.TestPlatform.Common.UnitTestspass with the change applied.