Skip to content

[Testing Platform] Message bus is never drained or disabled when a run is cancelled, so consumers can run concurrently with their own disposal #10357

Closed

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:

  1. CommonHost.ExecuteRequestAsync wraps the session-end notification in catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) and swallows it, so NotifyTestSessionEndAsync is skipped on the cancellation path.
    https://github.com/microsoft/testfx/blob/main/src/Platform/Microsoft.Testing.Platform/Hosts/CommonTestHost.cs
  2. NotifyTestSessionEndAsync is the only place that calls baseMessageBus.DrainDataAsync() followed by baseMessageBus.DisableAsync(). DisableAsync is the only path that awaits consumer completion.
  3. CommonHost.RunAsync's finally then calls DisposeServiceProviderAsync(..., isProcessShutdown: true), which disposes the bus and every consumer.
  4. 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

  1. Run a test application with a slow test and an IDataConsumer extension whose ConsumeAsync does non-trivial work.
  2. Cancel the run (Ctrl+C, or cancel the ITestApplicationCancellationTokenSource).
  3. The consumer's ConsumeAsync can still be running while DisposeServiceProviderAsync disposes 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 ConsumeAsync is 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.NotifyTestSessionEndAsync contains // 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 skips DisableAsync.

Loosely related, none covering this: #6099, #3695, #5345, #4588, #8904, #10191.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions