-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5798: Implement test to track if any UnobservedTaskExceptions were raised while test run #1831
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
Conversation
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.
Pull request overview
This PR implements tracking for unobserved task exceptions during test execution to help identify tasks that complete with exceptions without proper handling. The implementation uses xUnit's extensibility to register a global handler that captures unobserved exceptions and verifies none occurred after all tests complete.
Key Changes
- Added custom xUnit test case and discoverer infrastructure to track
UnobservedTaskExceptionevents globally - Implemented a test that runs after all other tests to verify no unobserved exceptions were raised
- Fixed a race condition in
CSharp3302Testswhere an unhandled task was causing unobserved exceptions
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| UnobservedExceptionTrackingTestCase.cs | New test case class that registers/unregisters the UnobservedTaskException handler and stores exceptions in a static list |
| UnobservedExceptionTestDiscoverer.cs | New xUnit test discoverer and fact attribute to enable unobserved exception tracking |
| TimeoutEnforcingXunitTestAssemblyRunner.cs | Modified to run the unobserved exception tracking test after all other tests complete |
| UnobservedTaskExceptionTracking.cs | New test class that verifies no unobserved exceptions were captured during test execution |
| CSharp3302Tests.cs | Fixed to properly handle task cancellation to prevent unobserved exceptions |
| Cluster.cs | Added disposal check to prevent ObjectDisposedException from timer callback |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
.../MongoDB.TestHelpers/XunitExtensions/TimeoutEnforcing/UnobservedExceptionTrackingTestCase.cs
Outdated
Show resolved
Hide resolved
.../MongoDB.TestHelpers/XunitExtensions/TimeoutEnforcing/UnobservedExceptionTrackingTestCase.cs
Outdated
Show resolved
Hide resolved
.../MongoDB.TestHelpers/XunitExtensions/TimeoutEnforcing/UnobservedExceptionTrackingTestCase.cs
Outdated
Show resolved
Hide resolved
| { | ||
| return new TimeoutEnforcingXunitTestCollectionRunner(testCollection, testCases, DiagnosticMessageSink, messageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), cancellationTokenSource).RunAsync(); | ||
| } |
Copilot
AI
Nov 27, 2025
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.
[nitpick] The formatting change converts this to a multi-line method but the actual logic is still a single return statement. Consider keeping the expression-bodied member syntax => for consistency with the codebase style, or add meaningful logic to justify the block body.
| { | |
| return new TimeoutEnforcingXunitTestCollectionRunner(testCollection, testCases, DiagnosticMessageSink, messageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), cancellationTokenSource).RunAsync(); | |
| } | |
| => new TimeoutEnforcingXunitTestCollectionRunner(testCollection, testCases, DiagnosticMessageSink, messageBus, TestCaseOrderer, new ExceptionAggregator(Aggregator), cancellationTokenSource).RunAsync(); |
.../MongoDB.TestHelpers/XunitExtensions/TimeoutEnforcing/UnobservedExceptionTrackingTestCase.cs
Outdated
Show resolved
Hide resolved
…were raised while test run
f0ea7f0 to
0620e0c
Compare
tests/MongoDB.TestHelpers/XunitExtensions/TimeoutEnforcing/TimeoutEnforcingTestInvoker.cs
Outdated
Show resolved
Hide resolved
| public class UnobservedTaskExceptionTracking | ||
| { | ||
| [UnobservedExceptionTrackingFact] | ||
| public void EnsureNoUnobservedTaskException() |
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.
So why don't we add this to BSON a well?
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.
Added.
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.
No issues with BSON?
| public class UnobservedTaskExceptionTracking | ||
| { | ||
| [UnobservedExceptionTrackingFact] | ||
| public void EnsureNoUnobservedTaskException() |
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.
No issues with BSON?
| return result; | ||
| } | ||
|
|
||
| private class UnobservedExceptionDebugger : IDisposable |
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.
Nice
BorisDog
left a comment
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.
LGTM
No description provided.