-
Notifications
You must be signed in to change notification settings - Fork 492
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
Unobserved task exception when using MQTT websocket when connection cannot be established #3318
Comments
This issue has been fixed as of this release, so I'll close this thread |
I guess that this fix is now causing an MQTT exception on the code below: private static async Task<DeviceRegistrationResult> ProvisionDeviceAsync(Parameters parameters)
{
Logger.LogInformation("Provisioning the device");
using var symmetricKeyProvider =
new SecurityProviderSymmetricKey(parameters.DeviceId, parameters.DeviceSymmetricKey, null);
using var mqttTransportHandler = new ProvisioningTransportHandlerMqtt();
var provisioningDeviceClient = ProvisioningDeviceClient.Create(
parameters.DpsEndpoint,
parameters.DpsScopeId,
symmetricKeyProvider,
mqttTransportHandler);
var pnpPayload = new ProvisioningRegistrationAdditionalData
{
JsonData = PnpConvention.CreateDpsPayload(ModelId),
};
while (true)
{
try
{
return await provisioningDeviceClient.RegisterAsync(pnpPayload);
}
catch (Exception ex)
{
Logger.LogError("Failed to provision the device. Exception: {ExceptionMessage}", ex.Message);
while (ex.InnerException is not null)
{
ex = ex.InnerException;
Logger.LogError("Inner Exception: {ExceptionMessage}", ex.Message);
}
}
Logger.LogInformation("Retrying to provision the device");
}
} There's no exception if I use the versions below: <PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.42.0" /> <!-- Version 1.42.1 has MQQT Connectivity issues -->
<PackageReference Include="Microsoft.Azure.Devices.Provisioning.Client" Version="1.19.3" /> <!-- Version 1.19.4 has MQQT Connectivity issues -->
<PackageReference Include="Microsoft.Azure.Devices.Provisioning.Transport.Mqtt" Version="1.17.3" /> <!-- Version 1.17.4 has MQQT Connectivity issues --> |
@nelsonmorais can you open a new thread for the issue you are seeing? And can you include the exception that you are seeing after upgrading? |
@timtay-microsoft Just started Christmas Holidays, will do that once I'm back at work. |
Context
Description of the issue
When using
Mqtt_WebSocket_Only
transport and a host that cannot be reached (i.e. due to DNS issues, because the device is offline) then aWebSocketException
is thrown that doesn't seem to be catched, because it is picked up byTaskScheduler.UnobservedTaskException
.Code sample exhibiting the issue
Console log of the issue
The following information is dumped to the console:
Probable cause
This is probably caused by the
MqttTransportHandler.OnError
method. When an error is encountered, then it sets the exception to both_connectCompletion
and_subscribeCompletionSource
. Because the tasks are never awaited, because of the exception the task leaks with an un-observed exception.It would probably best to add the following code to
MqttTransportHandler.Dispose(bool)
:This will briefly touch the exception and this marks the exception as handled inside the
TaskExceptionHolder
and prevents it from being reported. An even better solution would be to not create the task completion sources, until they are actually needed. This would also release some strain on the GC.The text was updated successfully, but these errors were encountered: