Azure / azure-iot-sdk-csharp

A C# SDK for connecting devices to Microsoft Azure IoT services
Other
470 stars 492 forks source link

[Bug Report] DeviceClient.SendEventAsync takes over one minute after update from 1.42.0 to 1.42.2 #3426

Closed fabianmeyer closed 8 months ago

fabianmeyer commented 9 months ago

Context

Description of the issue

After updating from Microsoft.Azure.Devices.Client 1.42.0 to 1.42.2, calls to SendEventAsync take over one minute in comparison to ~700ms before.

Code sample exhibiting the issue

var deviceClient = DeviceClient.CreateFromConnectionString($"HostName={cloudGatewayHostname};DeviceId={deviceId};SharedAccessKey={sharedAccessKey}", TransportType.Mqtt);
var sw = Stopwatch.StartNew();
_logger.LogInformation("Sending event as device {DeviceId} to IoT Hub", deviceId);
await deviceClient.SendEventAsync(message, cancellationToken);
_logger.LogInformation("Sent event as device {DeviceId} to IoT Hub ({Duration}ms)", deviceId, sw.Elapsed.TotalMilliseconds);

Console log of the issue

1.42.0 Sent event as device 00000001-4505-009d-5741-ec8495e2bc03 to IoT Hub (755.3321ms) 1.42.2 Sent event as device 00000001-4505-009d-5741-ec8495e2bc03 to IoT Hub (71925.7335ms)

TheDon79 commented 9 months ago

It seems to be, that I am running into the same/a very similar problem. When using Version 1.42.2 deviceClient.SendEventAsync runs into a Timeout and throws an IotHubCommunicationException with Transient network error occurred, please retry. Returning to Version 1.4.0 resolves this issue.

timtay-microsoft commented 9 months ago

Do you see this same behavior on .NET 7 as well? And do you see it when using AMQPS as well?

fabianmeyer commented 9 months ago

Besides NET8, I only have the NET6 SDK installed, for which the issue does not occur (same duration for 1.42.0 and 1.42.2). The issue also does not occur when using AMQP with 1.42.2 on NET8.

TheDon79 commented 9 months ago

It seems that the client stops to communicate with the IoT Hub after the Server Hello Done. grafik

timtay-microsoft commented 9 months ago

We suspect this is related to a .NET 8 bug in DotNetty (the MQTT library we use). The fix for this bug in DotNetty has been merged and we are working on putting out a new DotNetty release. Once we have released DotNetty, we'll put out a release of this SDK with the new DotNetty version which should resolve this issue.

jenscski commented 8 months ago

Looks like DotNetty is out with new version, I have added direct refereces to DotNetty and it looks like it's working now

<PackageReference Include="DotNetty.Codecs.Mqtt" Version="0.7.6" />
<PackageReference Include="DotNetty.Handlers" Version="0.7.6" />
timtay-microsoft commented 8 months ago

This behavior should be fixed in the new release: https://github.com/Azure/azure-iot-sdk-csharp/releases/tag/2024-03-28