Repository navigation
Description
Describe the bug
When a test run is cancelled, Microsoft.Testing.Platform tears down the service provider without ever draining or disabling the message bus. An IDataConsumer can therefore still be executing ConsumeAsync while it is being disposed, and any messages still queued are silently dropped.
The sequence is:
CommonHost.ExecuteRequestAsyncwraps the session-end notification incatch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)and swallows it, soNotifyTestSessionEndAsyncis skipped on the cancellation path.
https://github.com/microsoft/testfx/blob/main/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.csNotifyTestSessionEndAsyncis the only place that callsbaseMessageBus.DrainDataAsync()followed bybaseMessageBus.DisableAsync().DisableAsyncis the only path that awaits consumer completion.CommonHost.RunAsync'sfinallythen callsDisposeServiceProviderAsync(..., isProcessShutdown: true), which disposes the bus and every consumer.AsyncConsumerDataProcessor.Dispose()only calls_channel.Writer.TryComplete()(_channel.Complete()on netstandard) — it does not await_consumeTask.
So on cancellation nothing waits for the in-flight consumer loop, and disposal races it.
Version used
main (reproduced by inspection at commit on main as of 2026-07-31). Not a regression — the behaviour predates the current OpenTelemetry work.
Steps To Reproduce
- Run a test application with a slow test and an
IDataConsumerextension whoseConsumeAsyncdoes non-trivial work. - Cancel the run (Ctrl+C, or cancel the
ITestApplicationCancellationTokenSource). - The consumer's
ConsumeAsynccan still be running whileDisposeServiceProviderAsyncdisposes it.
Expected behavior
On cancellation the platform should still complete the message-bus shutdown handshake — stop producers, then complete and await the consumers through a non-cancelable path — before disposing services. Consumers should never observe ConsumeAsync overlapping their own Dispose.
Actual behavior
- Consumers can be disposed while
ConsumeAsyncis still executing, so a consumer that mutates its own state during consumption can throw during shutdown (for example a "collection was modified" exception from a consumer that walks its own state while disposing). - Messages already queued at cancellation time are dropped rather than processed, so a cancelled run's partial results can be missing from reports.
This affects every IDataConsumer — TRX, coverage, the report extensions, terminal output — not one extension in particular.
Additional context
Found during review of the OpenTelemetry work in #. That PR hardens OpenTelemetryResultHandler locally (its in-flight bookkeeping is guarded, and disposal drains into a local list before closing spans) so that telemetry cannot be the thing that throws during shutdown. That is deliberately a local mitigation for one consumer; it does not address the platform-level sequencing, which is why this is filed separately.
Two adjacent clean-ups worth folding in:
CommonTestHost.NotifyTestSessionEndAsynccontains// TODO: Is this reachable? If so, are we missing await baseMessageBus.DisableAsync() here? Tracked by https://github.com/microsoft/testfx/issues/8086.— issue 8086 is actually "Repository Quality: Analyzer Catalog Hygiene Improvements", so that reference is stale and should point here instead.- Consider making
AsyncConsumerDataProcessor.Dispose()await (or at least observe)_consumeTask, so disposal is safe even if a caller skipsDisableAsync.
Loosely related, none covering this: #6099, #3695, #5345, #4588, #8904, #10191.
Activity