Skip to content

DeviceClient.ReceiveAsync ignores token cancellation #1921

@dkattan

Description

@dkattan

Context

  • .NET 5
  • DeviceClient SDK 1.36
  • AMQP

Description of the issue

DeviceClient.ReceiveAsync(CancellationToken cancellationToken) method does not exit when cancellationToken is cancelled.

The current tests simply verify that the task returns immediately when passed a cancellationToken that is already cancelled.

Code sample exhibiting the issue

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
      stoppingToken.Register(() => { _logger.LogInformation("Stopping Token Called"); });
      while (!stoppingToken.IsCancellationRequested)
      {
        using Message receivedMessage = await DeviceClient.ReceiveAsync(stoppingToken);
        if (receivedMessage == null) continue;
      }
     _logger.LogInformation(“Exiting Application”);
}

Console log of the issue

Stopping Token Called

Expected Result

Stopping Token Called
Exiting Application

Workaround

To workaround this I have changed my code to use the ReceiveAsync(TimeSpan) overload, but it feels hacky.

Metadata

Metadata

Assignees

Labels

IoTSDKTracks all IoT SDK issues across the boardbugSomething isn't working.underlying library issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions