Skip to content

[BUG] OpenTelemetry traces contain System.Threading.Tasks.TaskCanceledException #49032

Closed
@ibruynin

Description

@ibruynin

Library name and version

Azure.Messaging.ServiceBus 7.18.4

Describe the bug

Use case

A worker continuously starts and stops processing on the ServiceBusProcessor.
The async MessageHandler creates activities to trace the processing of the messages received.
This creates spans in aspire dashboard / Jaeger which is wanted behaviour.

Implementation

Program.cs

// enable experimental tracing
AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true);
// enable OTEL
// this adds tracing.AddSource("Azure.Messaging.ServiceBus.*");
builder.Services.AddMyOpenTelemetry(builder.Configuration)

Worker.cs

protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
  while (true)
  {
  // start processing
  await processor.StartProcessingAsync(stoppingToken);
  // make sure no more messages are processed in this run
  for (int i = 0; i < 10; ++i)
  {
      if (MessagesProcessed >= MAXMSGS)
      {
           break;
      }
      await Task.Delay(100);
  }
  // stop processing (give room to other threads)
  await processor.StopProcessingAsync(stoppingToken);
  }
}

async Task MessageHandler(ProcessMessageEventArgs args)
{
    using (var act = _metricsClient.StartMessageReceivingActivity()) {
        // processing logic here
        await args.CompleteMessageAsync(args.Message);
    }
}

Actual behaviour

The activity (ServiceBusReceiver.Receive) for effectively received messages are linked to the activity span created in the MessageHandler. (👍)

The activity (ServiceBusReceiver.Receive) also appears with error: true and error.type: System.Threading.Tasks.TaskCanceledException due to the fact that the processor is stopped in the loop in ExecuteAsync. (👎)

Expected behavior

Since StartProcessingAsync and StopProcessingAsync operations are normal operations, I'd expect no errors/exceptions in the traces.

Actual behavior

cfr. "Actual behaviour" in description

Reproduction Steps

cfr. "Implementation" in description

Environment

.net 9

Microsoft Visual Studio Enterprise 2022
Version 17.12.1
VisualStudio.17.Release/17.12.1+35514.174
Installed Version: Enterprise

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.Service Buscustomer-reportedIssues that are reported by GitHub users external to the Azure organization.issue-addressedWorkflow: The Azure SDK team believes it to be addressed and ready to close.questionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions