Skip to content

Commit 739a474

Browse files
mot256lukebakken
authored andcommitted
Fix connection leaks on auto recovery
1 parent 4e50ada commit 739a474

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,24 @@ private bool TryPerformAutomaticRecovery()
451451
catch (Exception e)
452452
{
453453
ESLog.Error("Exception when recovering connection. Will try again after retry interval.", e);
454+
455+
try
456+
{
457+
/*
458+
* To prevent connection leaks on the next recovery loop,
459+
* we abort the delegated connection if it is still open.
460+
* We do not want to block the abort forever (potentially deadlocking recovery),
461+
* so we specify the same configured timeout used for connection.
462+
*/
463+
if (_delegate?.IsOpen == true)
464+
{
465+
_delegate.Abort(Constants.InternalError, "FailedAutoRecovery", ShutdownInitiator.Library, _factory.RequestedConnectionTimeout);
466+
}
467+
}
468+
catch (Exception e2)
469+
{
470+
ESLog.Warn("Exception when aborting previous auto recovery connection.", e2);
471+
}
454472
}
455473

456474
return false;
@@ -672,7 +690,6 @@ private void Init(IFrameHandler fh)
672690
lock (_eventLock)
673691
{
674692
ConnectionShutdown += recoveryListener;
675-
_recordedShutdownEventHandlers += recoveryListener;
676693
}
677694
}
678695

projects/RabbitMQ.Client/client/impl/Connection.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,15 @@ public Connection(IConnectionFactory factory, bool insist, IFrameHandler frameHa
116116
_model0 = (ModelBase)Protocol.CreateModel(_session0);
117117

118118
StartMainLoop(factory.UseBackgroundThreadsForIO);
119-
Open(insist);
119+
try
120+
{
121+
Open(insist);
122+
}
123+
catch
124+
{
125+
TerminateMainloop();
126+
throw;
127+
}
120128
}
121129

122130
public Guid Id { get { return _id; } }

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,18 @@ public void Close()
193193
try
194194
{
195195
_channelWriter.Complete();
196-
_writerTask.GetAwaiter().GetResult();
196+
_writerTask?.GetAwaiter().GetResult();
197197
}
198-
catch(Exception)
198+
catch
199199
{
200+
// ignore, we are closing anyway
200201
}
201202

202203
try
203204
{
204205
_socket.Close();
205206
}
206-
catch (Exception)
207+
catch
207208
{
208209
// ignore, we are closing anyway
209210
}

0 commit comments

Comments
 (0)