Skip to content

Commit

Permalink
Port #1145 to main
Browse files Browse the repository at this point in the history
#1145

1fac6fc

Finish porting #1145 to main
  • Loading branch information
lukebakken committed Feb 23, 2022
1 parent 0ed012c commit 306df72
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ private bool TryPerformAutomaticRecovery()
catch (Exception e)
{
ESLog.Error("Exception when recovering connection. Will try again after retry interval.", e);
try
{
/*
* To prevent connection leaks on the next recovery loop,
* we abort the delegated connection if it is still open.
* We do not want to block the abort forever (potentially deadlocking recovery),
* so we specify the same configured timeout used for connection.
*/
if (_innerConnection?.IsOpen == true)
{
_innerConnection.Abort(Constants.InternalError, "FailedAutoRecovery", _factory.RequestedConnectionTimeout);
}
}
catch (Exception e2)
{
ESLog.Warn("Exception when aborting previous auto recovery connection.", e2);
}
}

return false;
Expand Down
11 changes: 10 additions & 1 deletion projects/RabbitMQ.Client/client/impl/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,16 @@ public Connection(IConnectionFactory factory, IFrameHandler frameHandler, string
};

_mainLoopTask = Task.Factory.StartNew(MainLoop, TaskCreationOptions.LongRunning);
Open();
try
{
Open();
}
catch
{
var ea = new ShutdownEventArgs(ShutdownInitiator.Library, Constants.InternalError, "FailedOpen");
Close(ea, true, TimeSpan.FromSeconds(5));
throw;
}
}

public Guid Id => _id;
Expand Down
7 changes: 4 additions & 3 deletions projects/RabbitMQ.Client/client/impl/SocketFrameHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,18 @@ public void Close()
try
{
_channelWriter.Complete();
_writerTask.GetAwaiter().GetResult();
_writerTask?.GetAwaiter().GetResult();
}
catch (Exception)
catch
{
// ignore, we are closing anyway
}

try
{
_socket.Close();
}
catch (Exception)
catch
{
// ignore, we are closing anyway
}
Expand Down

0 comments on commit 306df72

Please sign in to comment.