Skip to content
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

[Bug Report] selecting MQTT as DeviceClient transport type causes WPF app to hangs on close? #2163

Closed
ZmorzynskiK opened this issue Sep 3, 2021 · 7 comments
Assignees
Labels
bug Something isn't working. fix-checked-in Fix checked into main or preview, but not yet released. IoTSDK Tracks all IoT SDK issues across the board

Comments

@ZmorzynskiK
Copy link

Context

  • OS, version, SKU and CPU architecture used: Windows 10 Desktop x64
  • Application's .NET Target Framework : net5 + WPF
  • Device: Laptop
  • SDK version used: 1.38.0

Description of the issue

I've stumbled upon a strange issue when creating and using DeviceClient.Create in WPF app with Transport.Mqtt.
I'm sending a message to IoT Hub and when closing the main window, the application just hangs and never terminates.
When I select TransportType.Amqp this hang does not occur and the application terminates correctly.

Code sample exhibiting the issue

Create a simple WPF application, add two buttons in XAML:

<Window x:Class="AzIoTDev.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AzIoTDev"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
        <Button Content="Send data MQTT" Click="Button_Click"/>
        <Button Content="Send data AMQP" Click="Button_Click_1" />
    </StackPanel>
</Window>

then in codebehind set this code (set your own credentials for device):

public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}

		private const string DeviceId = "IoT hub device ID";
		private const string AuthKey = "IoT hub auth key for device";
		private const string ServerUrl = "IoT hub URL";
		private const int OperationTimeoutMs = 1000;

		private DeviceClient _azureClient;

		private async Task sendHello(TransportType tt)
		{
			var auth = new DeviceAuthenticationWithRegistrySymmetricKey(DeviceId, AuthKey);
			_azureClient = DeviceClient.Create(ServerUrl, auth, tt);
			_azureClient.OperationTimeoutInMilliseconds = OperationTimeoutMs;
			
			byte[] bytes = Encoding.UTF8.GetBytes("HelloPayload");
			Message eventMessage = new Message(bytes);
			eventMessage.Properties.Add("MessageType", "Hello");
			await _azureClient.SendEventAsync(eventMessage).ConfigureAwait(false);

			await _azureClient.CloseAsync().ConfigureAwait(false);
			_azureClient.Dispose();
		}

		private async void Button_Click(object sender, RoutedEventArgs e)
		{
			await sendHello(TransportType.Mqtt);
		}

		private async void Button_Click_1(object sender, RoutedEventArgs e)
		{
			await sendHello(TransportType.Amqp);
		}
	}

You will see that clicking "Send with MQTT" will hang the app when closing and "Send with AQMP" will not hang when closing.
Is there an issue or am I doing something wrong?

all the best
Krzysztof

@ZmorzynskiK ZmorzynskiK added the bug Something isn't working. label Sep 3, 2021
@github-actions github-actions bot added the IoTSDK Tracks all IoT SDK issues across the board label Sep 3, 2021
@jamdavi jamdavi self-assigned this Sep 3, 2021
@jamdavi
Copy link
Member

jamdavi commented Sep 3, 2021

Thanks for the repro code I will take a look.

@jamdavi
Copy link
Member

jamdavi commented Sep 21, 2021

@ZmorzynskiK Just wanted to keep an update here. The PR above does fix the issue, I just need to make sure that this implementation passes all our our tests which have had some issues the past few weeks. Once all is passing I'll update the thread.

@ZmorzynskiK
Copy link
Author

@jamdavi great, thanks for the update.

@BillArmstrong
Copy link

@jamdavi I see that the PR is still being worked on. The original bug report indicates that the issue occurs in a WPF app. We're experiencing the same hanging issue with a simple Console application that just opens a connection and closes it again. It would be great if this could be resolved soon.

@jamdavi
Copy link
Member

jamdavi commented Dec 8, 2021

Just an update for all who might be looking at this. We tracked the issue down to a change with DotNetty. The following PR has been merged and we will take a new version of DotNetty to fix.

Azure/DotNetty#570

@jamdavi
Copy link
Member

jamdavi commented Jan 10, 2022

We had been waiting on a release from the Netty team and discussed internally the best way to handle the update. The Netty team created a new version of the Common library and we are now taking a dependency on this new version of the library.

This is not required once we release the new SDK but if you want to test this out you can also take a dependency on the latest version of DotNetty.Common and it should replace the assembly at build time.

Note, if you're targeting .NET Full framework such as net472 you'll have to use a binding redirect in your app.config. Visual Studio has an option to generate one automatically.

@jamdavi jamdavi added the fix-checked-in Fix checked into main or preview, but not yet released. label Jan 11, 2022
@jamdavi
Copy link
Member

jamdavi commented Jan 31, 2022

This is now fixed with version 1.40.0 [https://github.com/Azure/azure-iot-sdk-csharp/releases/tag/2022_01_26].

@jamdavi jamdavi closed this as completed Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. fix-checked-in Fix checked into main or preview, but not yet released. IoTSDK Tracks all IoT SDK issues across the board
Projects
None yet
Development

No branches or pull requests

3 participants