Repository navigation
Stabilize distributed trace pipeline tests - #5958
Conversation
Wait for the distributed tracing event handler to be ready before waking the tracee, and wait for activity delivery before stopping the pipeline. Co-authored-by: Copilot App <[email protected]> Copilot-Session: ee7094e3-aa37-4474-b595-d1ed5a1d2764
Co-authored-by: Copilot App <[email protected]> Copilot-Session: ee7094e3-aa37-4474-b595-d1ed5a1d2764
There was a problem hiding this comment.
Pull request overview
This PR aims to reduce flakiness in distributed trace pipeline unit tests by synchronizing “pipeline ready” and “tracee begins emitting” so the ActivitySource listener is installed before activities are produced and the pipeline remains open until the expected activity is consumed.
Changes:
- Delay
PipelineStartedsignaling inDistributedTracesPipelineuntil after the consumer handler is registered. - Extend
PipelineTestUtilities.ExecutePipelineWithTraceeto optionally wait for a “pipeline started” task before waking the tracee. - Update the EventPipe tracee to optionally wait until
ActivitySource.HasListeners()before starting the test body, and update trace tests to coordinate start/stop via TCS signals.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/PipelineTestUtilities.cs | Adds optional pipelineStartedTask gating to reduce readiness race windows before waking the tracee. |
| src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/DistributedTracesPipelineUnitTests.cs | Adds pipeline/activity synchronization via TCS and updates tracee args to wait for ActivitySource listener installation. |
| src/tests/CommonTestRunner/Debuggees/EventPipeTracee/Program.cs | Adds WaitForActivitySourceListener mode to delay activity emission until the runtime listener is installed. |
| src/Microsoft.Diagnostics.Monitoring.EventPipe/DistributedTraces/DistributedTracesPipeline.cs | Registers Dynamic.All handler before reporting pipeline startup to loggers. |
Suppressed comments (4)
src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/DistributedTracesPipelineUnitTests.cs:92
- Same invariance issue as above:
TaskCompletionSource<object?>cannot be passed toExecutePipelineWithTraceewhereTaskCompletionSource<object>is expected. UseTaskCompletionSource<object>consistently within this test file.
TaskCompletionSource<object?> pipelineStartedSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
TaskCompletionSource<object?> activityLoggedSource = new(TaskCreationOptions.RunContinuationsAsynchronously);
TestActivityLogger logger = new(pipelineStartedSource, activityLoggedSource);
src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/DistributedTracesPipelineUnitTests.cs:149
- After switching the test TCS types to
TaskCompletionSource<object>, the logger should also use the same generic type. Consider using a single static non-null object as the signal value to avoid allocating a new object each timeTrySetResultis called.
private readonly TaskCompletionSource<object?> _pipelineStartedSource;
private readonly TaskCompletionSource<object?> _activityLoggedSource;
public TestActivityLogger(
TaskCompletionSource<object?> pipelineStartedSource,
TaskCompletionSource<object?> activityLoggedSource)
{
_pipelineStartedSource = pipelineStartedSource;
_activityLoggedSource = activityLoggedSource;
}
src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/DistributedTracesPipelineUnitTests.cs:159
- If the logger's completion sources are
TaskCompletionSource<object>, passingnulltoTrySetResultwill produce nullable warnings (and can be treated as errors). Use a non-null signal value (e.g., thes_signalobject) when completing the TCS.
LoggedActivities.Add((activity, tags.ToArray()));
_activityLoggedSource.TrySetResult(null);
}
src/tests/Microsoft.Diagnostics.Monitoring.EventPipe/DistributedTracesPipelineUnitTests.cs:165
- Same as in
Log: if_pipelineStartedSourceisTaskCompletionSource<object>, complete it with a non-null value (e.g.,s_signal) to avoid nullable warnings/errors under#nullable enable.
public Task PipelineStarted(CancellationToken token)
{
_pipelineStartedSource.TrySetResult(null);
return Task.CompletedTask;
}
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot App <[email protected]> Copilot-Session: ee7094e3-aa37-4474-b595-d1ed5a1d2764
Fix a race in
DistributedTracesPipelinewhere EventPipe processing could begin whileOnEventSourceAvailablewas suspended in an asynchronousPipelineStartedcallback, before the activity event handler was registered. Register the handler before the first await so activities emitted during logger startup are captured instead of dropped.The tests now also wait for the target runtime to install its
ActivitySourcelistener and keep the pipeline open until the expected activity is consumed.No API breaking changes. Observable lifecycle behavior changes:
Logmay overlap an asynchronousPipelineStartedcallback, and startup events are no longer dropped.