Skip to content

Commit 484cf6d

Browse files
committed
Handle AppDomain unload
Fixes #826
1 parent 7bea841 commit 484cf6d

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

projects/RabbitMQ.Client/client/api/ConnectionFactory.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -723,14 +723,15 @@ private List<AmqpTcpEndpoint> LocalEndpoints()
723723

724724
private static string EnsureClientProvidedNameLength(string clientProvidedName)
725725
{
726-
if (clientProvidedName.Length > InternalConstants.DefaultRabbitMqMaxClientProvideNameLength)
726+
if (clientProvidedName != null)
727727
{
728-
return clientProvidedName.Substring(0, InternalConstants.DefaultRabbitMqMaxClientProvideNameLength);
729-
}
730-
else
731-
{
732-
return clientProvidedName;
728+
if (clientProvidedName.Length > InternalConstants.DefaultRabbitMqMaxClientProvideNameLength)
729+
{
730+
return clientProvidedName.Substring(0, InternalConstants.DefaultRabbitMqMaxClientProvideNameLength);
731+
}
733732
}
733+
734+
return clientProvidedName;
734735
}
735736
}
736737
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,23 @@ static bool ShouldTriggerConnectionRecovery(ShutdownEventArgs args)
7171
}
7272
}
7373

74-
// happens when EOF is reached, e.g. due to RabbitMQ node
75-
// connectivity loss or abrupt shutdown
7674
if (args.Initiator == ShutdownInitiator.Library)
7775
{
78-
return true;
76+
/*
77+
* https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/826
78+
* Happens when an AppDomain is unloaded
79+
*/
80+
if (args.Exception is ThreadAbortException &&
81+
args.ReplyCode == Constants.InternalError)
82+
{
83+
return false;
84+
}
85+
else
86+
{
87+
// happens when EOF is reached, e.g. due to RabbitMQ node
88+
// connectivity loss or abrupt shutdown
89+
return true;
90+
}
7991
}
8092

8193
return false;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ private async Task MainLoop()
5353
await ReceiveLoopAsync(mainLoopToken)
5454
.ConfigureAwait(false);
5555
}
56+
#if NETSTANDARD
57+
catch (ThreadAbortException taex)
58+
{
59+
/*
60+
* https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/826
61+
*/
62+
var ea = new ShutdownEventArgs(ShutdownInitiator.Library,
63+
Constants.InternalError,
64+
"Thread aborted (AppDomain unloaded?)",
65+
exception: taex);
66+
HandleMainLoopException(ea);
67+
}
68+
#endif
5669
catch (EndOfStreamException eose)
5770
{
5871
// Possible heartbeat exception

0 commit comments

Comments
 (0)