Dispose socket when TCP connect failed.

This commit is contained in:
Christian Kratky
2020-04-30 23:02:05 +02:00
parent 856861e1b1
commit f85bc9b956
3 changed files with 52 additions and 44 deletions
+1
View File
@@ -17,6 +17,7 @@
* [Client] Added method to trigger PING/PONG manually (connection check etc.). * [Client] Added method to trigger PING/PONG manually (connection check etc.).
* [Client] Added support for certificate validation callback when using Web Sockets (requires netstandard2.1+). * [Client] Added support for certificate validation callback when using Web Sockets (requires netstandard2.1+).
* [Client] Fixed a memory leak when web socket based connections trying to reconnect with an offline server. * [Client] Fixed a memory leak when web socket based connections trying to reconnect with an offline server.
* [Client] Fixed a memory leak when TCP based connections trying to reconnect with an offline server.
* [Client] Fixed an issue when connecting to an invalid host (format). * [Client] Fixed an issue when connecting to an invalid host (format).
* [ManagedClient] Added method to trigger PING/PONG manually (connection check etc.). * [ManagedClient] Added method to trigger PING/PONG manually (connection check etc.).
* [MQTTnet.AspNetCore] improved compatibility with AspNetCore 3.1. * [MQTTnet.AspNetCore] improved compatibility with AspNetCore 3.1.
@@ -143,7 +143,7 @@ namespace MQTTnet.Implementations
{ {
_networkStream?.Dispose(); _networkStream?.Dispose();
// Workaround for: workaround for https://github.com/dotnet/corefx/issues/24430 // Workaround for: https://github.com/dotnet/corefx/issues/24430
using (cancellationToken.Register(() => _socket.Dispose())) using (cancellationToken.Register(() => _socket.Dispose()))
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
@@ -46,8 +46,9 @@ namespace MQTTnet.Implementations
public async Task ConnectAsync(CancellationToken cancellationToken) public async Task ConnectAsync(CancellationToken cancellationToken)
{ {
CrossPlatformSocket socket; CrossPlatformSocket socket = null;
try
{
if (_options.AddressFamily == AddressFamily.Unspecified) if (_options.AddressFamily == AddressFamily.Unspecified)
{ {
socket = new CrossPlatformSocket(); socket = new CrossPlatformSocket();
@@ -97,6 +98,12 @@ namespace MQTTnet.Implementations
Endpoint = socket.RemoteEndPoint?.ToString(); Endpoint = socket.RemoteEndPoint?.ToString();
} }
catch (Exception)
{
socket?.Dispose();
throw;
}
}
public Task DisconnectAsync(CancellationToken cancellationToken) public Task DisconnectAsync(CancellationToken cancellationToken)
{ {