From 61b324043a19cb024e4b43d3b82dad102da66f7f Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Thu, 26 Jul 2018 20:02:11 +0200 Subject: [PATCH] Refactoring --- Build/MQTTnet.nuspec | 2 ++ Source/MQTTnet/Server/MqttClientSession.cs | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Build/MQTTnet.nuspec b/Build/MQTTnet.nuspec index 10da9e58..1433105e 100644 --- a/Build/MQTTnet.nuspec +++ b/Build/MQTTnet.nuspec @@ -13,9 +13,11 @@ * [Core] Performance optimizations. * [Client] Added support for proxies when using web socket connections (thanks to PitySOFT). * [Client] Refactored TLS parameter usage and added more parameters (thanks to PitySOFT). +* [Client] Fixed a deadlock when clients are disconnecting (thanks to @JanEggers). * [Server] Fixed an issue in client keep alive checks (thanks to @jenscski). * [Server] Changed the order of _ClientConnected_ and _ClientDisconnected_ events so that _ClientConnected_ is fired at first (thanks to @jenscski). * [Server] Fixed an iussue which lets the server stop processing messages when using the application message interceptor (thanks to @alamsor). +* [Server] Fixed a deadlock when clients are disconnecting (thanks to @JanEggers). Copyright Christian Kratky 2016-2018 MQTT Message Queue Telemetry Transport MQTTClient MQTTServer Server MQTTBroker Broker NETStandard IoT InternetOfThings Messaging Hardware Arduino Sensor Actuator M2M ESP Smart Home Cities Automation Xamarin diff --git a/Source/MQTTnet/Server/MqttClientSession.cs b/Source/MQTTnet/Server/MqttClientSession.cs index fe586636..de3a7a48 100644 --- a/Source/MQTTnet/Server/MqttClientSession.cs +++ b/Source/MQTTnet/Server/MqttClientSession.cs @@ -81,13 +81,16 @@ namespace MQTTnet.Server try { _adapter = adapter; + adapter.ReadingPacketStarted += OnAdapterReadingPacketStarted; adapter.ReadingPacketCompleted += OnAdapterReadingPacketCompleted; _cancellationTokenSource = new CancellationTokenSource(); //woraround for https://github.com/dotnet/corefx/issues/24430 - _cleanupHandle = _cancellationTokenSource.Token.Register(() => Cleanup()); +#pragma warning disable 4014 + _cleanupHandle = _cancellationTokenSource.Token.Register(() => CleanupAsync()); +#pragma warning restore 4014 //endworkaround _wasCleanDisconnect = false; @@ -131,7 +134,7 @@ namespace MQTTnet.Server } finally { - await Cleanup().ConfigureAwait(false); + await CleanupAsync().ConfigureAwait(false); _cleanupHandle?.Dispose(); _cleanupHandle = null; @@ -141,11 +144,11 @@ namespace MQTTnet.Server } } - private async Task Cleanup() + private async Task CleanupAsync() { + var adapter = _adapter; try { - var adapter = _adapter; if (adapter == null) { return; @@ -155,13 +158,17 @@ namespace MQTTnet.Server adapter.ReadingPacketStarted -= OnAdapterReadingPacketStarted; adapter.ReadingPacketCompleted -= OnAdapterReadingPacketCompleted; + await adapter.DisconnectAsync(_options.DefaultCommunicationTimeout, CancellationToken.None).ConfigureAwait(false); - adapter.Dispose(); } catch (Exception exception) { _logger.Error(exception, exception.Message); } + finally + { + adapter?.Dispose(); + } } public void Stop(MqttClientDisconnectType type)