Skip to content

Commit

Permalink
Handle AppDomain unload
Browse files Browse the repository at this point in the history
Fixes #826
  • Loading branch information
lukebakken committed May 30, 2024
1 parent 7bea841 commit 484cf6d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
13 changes: 7 additions & 6 deletions projects/RabbitMQ.Client/client/api/ConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -723,14 +723,15 @@ private List<AmqpTcpEndpoint> LocalEndpoints()

private static string EnsureClientProvidedNameLength(string clientProvidedName)
{
if (clientProvidedName.Length > InternalConstants.DefaultRabbitMqMaxClientProvideNameLength)
if (clientProvidedName != null)
{
return clientProvidedName.Substring(0, InternalConstants.DefaultRabbitMqMaxClientProvideNameLength);
}
else
{
return clientProvidedName;
if (clientProvidedName.Length > InternalConstants.DefaultRabbitMqMaxClientProvideNameLength)
{
return clientProvidedName.Substring(0, InternalConstants.DefaultRabbitMqMaxClientProvideNameLength);
}
}

return clientProvidedName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,23 @@ static bool ShouldTriggerConnectionRecovery(ShutdownEventArgs args)
}
}

// happens when EOF is reached, e.g. due to RabbitMQ node
// connectivity loss or abrupt shutdown
if (args.Initiator == ShutdownInitiator.Library)
{
return true;
/*
* https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/826
* Happens when an AppDomain is unloaded
*/
if (args.Exception is ThreadAbortException &&
args.ReplyCode == Constants.InternalError)
{
return false;
}
else
{
// happens when EOF is reached, e.g. due to RabbitMQ node
// connectivity loss or abrupt shutdown
return true;
}
}

return false;
Expand Down
13 changes: 13 additions & 0 deletions projects/RabbitMQ.Client/client/impl/Connection.Receive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ private async Task MainLoop()
await ReceiveLoopAsync(mainLoopToken)
.ConfigureAwait(false);
}
#if NETSTANDARD
catch (ThreadAbortException taex)
{
/*
* https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/826
*/
var ea = new ShutdownEventArgs(ShutdownInitiator.Library,
Constants.InternalError,
"Thread aborted (AppDomain unloaded?)",
exception: taex);
HandleMainLoopException(ea);
}
#endif
catch (EndOfStreamException eose)
{
// Possible heartbeat exception
Expand Down

0 comments on commit 484cf6d

Please sign in to comment.