Skip to content

Commit 6a5d229

Browse files
Merge pull request #1161 from rabbitmq/rabbitmq-dotnet-client-1145-main
Port PR #1145 to main, manually
2 parents 0ed012c + 306df72 commit 6a5d229

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,23 @@ private bool TryPerformAutomaticRecovery()
166166
catch (Exception e)
167167
{
168168
ESLog.Error("Exception when recovering connection. Will try again after retry interval.", e);
169+
try
170+
{
171+
/*
172+
* To prevent connection leaks on the next recovery loop,
173+
* we abort the delegated connection if it is still open.
174+
* We do not want to block the abort forever (potentially deadlocking recovery),
175+
* so we specify the same configured timeout used for connection.
176+
*/
177+
if (_innerConnection?.IsOpen == true)
178+
{
179+
_innerConnection.Abort(Constants.InternalError, "FailedAutoRecovery", _factory.RequestedConnectionTimeout);
180+
}
181+
}
182+
catch (Exception e2)
183+
{
184+
ESLog.Warn("Exception when aborting previous auto recovery connection.", e2);
185+
}
169186
}
170187

171188
return false;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,16 @@ public Connection(IConnectionFactory factory, IFrameHandler frameHandler, string
8181
};
8282

8383
_mainLoopTask = Task.Factory.StartNew(MainLoop, TaskCreationOptions.LongRunning);
84-
Open();
84+
try
85+
{
86+
Open();
87+
}
88+
catch
89+
{
90+
var ea = new ShutdownEventArgs(ShutdownInitiator.Library, Constants.InternalError, "FailedOpen");
91+
Close(ea, true, TimeSpan.FromSeconds(5));
92+
throw;
93+
}
8594
}
8695

8796
public Guid Id => _id;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,18 @@ public void Close()
203203
try
204204
{
205205
_channelWriter.Complete();
206-
_writerTask.GetAwaiter().GetResult();
206+
_writerTask?.GetAwaiter().GetResult();
207207
}
208-
catch (Exception)
208+
catch
209209
{
210+
// ignore, we are closing anyway
210211
}
211212

212213
try
213214
{
214215
_socket.Close();
215216
}
216-
catch (Exception)
217+
catch
217218
{
218219
// ignore, we are closing anyway
219220
}

0 commit comments

Comments
 (0)