diff --git a/MQTTnet.Core/Client/MqttPacketDispatcher.cs b/MQTTnet.Core/Client/MqttPacketDispatcher.cs index 0784e8a..aba050d 100644 --- a/MQTTnet.Core/Client/MqttPacketDispatcher.cs +++ b/MQTTnet.Core/Client/MqttPacketDispatcher.cs @@ -10,7 +10,7 @@ namespace MQTTnet.Core.Client public class MqttPacketDispatcher { private readonly object _syncRoot = new object(); - private readonly List _receivedPackets = new List(); + private readonly HashSet _receivedPackets = new HashSet(); private readonly List _packetAwaiters = new List(); public async Task WaitForPacketAsync(Func selector, TimeSpan timeout) @@ -41,7 +41,7 @@ namespace MQTTnet.Core.Client { if (packetAwaiter.PacketSelector(packet)) { - packetAwaiter.SetResult(packet); + packetAwaiter.TrySetResult(packet); packetDispatched = true; break; } @@ -71,7 +71,7 @@ namespace MQTTnet.Core.Client private List GetPacketAwaiters() { - lock (_packetAwaiters) + lock (_syncRoot) { return new List(_packetAwaiters); }