Skip to content

Commit 1555ba1

Browse files
Dispose socket on ConnectFailureException
Fixes #337.
1 parent f67be34 commit 1555ba1

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

projects/client/RabbitMQ.Client/RabbitMQ.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<PackageReference Include="System.Net.Sockets" Version="4.3.0" />
6262
<PackageReference Include="System.Reflection.Extensions" Version="4.3.0" />
6363
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.3.0" />
64+
<PackageReference Include="System.Runtime" Version="4.3.0" />
6465
<PackageReference Include="System.Runtime.Extensions" Version="4.3.0" />
6566
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.0" />
6667
<PackageReference Include="System.Threading" Version="4.3.0" />

projects/client/RabbitMQ.Client/src/client/api/ITcpClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace RabbitMQ.Client
1010
/// Wrapper interface for standard TCP-client. Provides socket for socket frame handler class.
1111
/// </summary>
1212
/// <remarks>Contains all methods that are currenty in use in rabbitmq client.</remarks>
13-
public interface ITcpClient
13+
public interface ITcpClient : IDisposable
1414
{
1515
bool Connected { get; }
1616

projects/client/RabbitMQ.Client/src/client/impl/SocketFrameHandler.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public void Flush()
257257

258258
private bool ShouldTryIPV6(AmqpTcpEndpoint endpoint)
259259
{
260-
return (Socket.OSSupportsIPv6 && endpoint.AddressFamily != AddressFamily.InterNetwork)
260+
return (Socket.OSSupportsIPv6 && endpoint.AddressFamily != AddressFamily.InterNetwork);
261261
}
262262

263263
private ITcpClient ConnectUsingIPv6(AmqpTcpEndpoint endpoint,
@@ -278,9 +278,14 @@ private ITcpClient ConnectUsingAddressFamily(AmqpTcpEndpoint endpoint,
278278
Func<AddressFamily, ITcpClient> socketFactory,
279279
int timeout, AddressFamily family)
280280
{
281-
var socket = socketFactory(family);
282-
ConnectOrFail(socket, endpoint, timeout);
283-
return socket;
281+
ITcpClient socket = socketFactory(family);
282+
try {
283+
ConnectOrFail(socket, endpoint, timeout);
284+
return socket;
285+
} catch (ConnectFailureException e) {
286+
socket.Dispose();
287+
throw e;
288+
}
284289
}
285290

286291
private void ConnectOrFail(ITcpClient socket, AmqpTcpEndpoint endpoint, int timeout)

projects/client/RabbitMQ.Client/src/client/impl/TcpClientAdapter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,16 @@ public virtual async Task ConnectAsync(string host, int port)
4141

4242
public virtual void Close()
4343
{
44-
if(sock != null)
44+
// TODO: Socket.Close() would be a better option here. MK.
45+
this.Dispose();
46+
}
47+
48+
public virtual void Dispose()
49+
{
50+
if (sock != null)
51+
{
4552
sock.Dispose();
53+
}
4654
sock = null;
4755
}
4856

0 commit comments

Comments
 (0)