Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeviceClient.ReceiveAsync ignores token cancellation #1921

Closed
dkattan opened this issue Apr 28, 2021 · 3 comments
Closed

DeviceClient.ReceiveAsync ignores token cancellation #1921

dkattan opened this issue Apr 28, 2021 · 3 comments
Assignees
Labels
bug Something isn't working. IoTSDK Tracks all IoT SDK issues across the board underlying library issue

Comments

@dkattan
Copy link

dkattan commented Apr 28, 2021

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.

@dkattan dkattan added the bug Something isn't working. label Apr 28, 2021
@github-actions github-actions bot added the IoTSDK Tracks all IoT SDK issues across the board label Apr 28, 2021
@timtay-microsoft
Copy link
Member

timtay-microsoft commented Apr 28, 2021

Looks like the SDK uses the default receive timeout set in the transport settings rather than the cancellation token when using the receiveAsync API.

message = await _amqpUnit.ReceiveMessageAsync(_transportSettings.DefaultReceiveTimeout).ConfigureAwait(false);

That seems like a bug to me as well, so I'll fix this

@timtay-microsoft
Copy link
Member

After further investigation, the real issue here appears to be that the AMQP library we use simply doesn't accept cancellation tokens. Instead, it only accepts TimeSpans as you can see here. I'll discuss this a bit more with my team.

@timtay-microsoft timtay-microsoft self-assigned this Apr 29, 2021
@timtay-microsoft
Copy link
Member

Looks like there is already an ask out for adding support for cancellation tokens to the AMQP library in question here. Ideally they would add support and we could just pass along the cancellation token as expected.

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug Something isn't working. IoTSDK Tracks all IoT SDK issues across the board underlying library issue
Projects
None yet
Development

No branches or pull requests

4 participants