Skip to content

Commit 6f82824

Browse files
Merge pull request #230 from rabbitmq/rabbitmq-dotnet-client-220
reduce chances of NullReference exception in MaybeStopHeartbeatTimers…
2 parents 167e29c + 7b1f65c commit 6f82824

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -1114,18 +1114,24 @@ protected void MaybeStopHeartbeatTimers()
11141114

11151115
private void MaybeDisposeTimer(ref Timer timer)
11161116
{
1117-
if (timer != null)
1117+
// capture the timer to reduce chance of a null ref exception
1118+
var captured = timer;
1119+
if (captured != null)
11181120
{
11191121
try
11201122
{
1121-
timer.Change(Timeout.Infinite, Timeout.Infinite);
1122-
timer.Dispose();
1123+
captured.Change(Timeout.Infinite, Timeout.Infinite);
1124+
captured.Dispose();
11231125
timer = null;
11241126
}
1125-
catch (ObjectDisposedException ignored)
1127+
catch (ObjectDisposedException)
11261128
{
11271129
// we are shutting down, ignore
11281130
}
1131+
catch(NullReferenceException)
1132+
{
1133+
// this should be very rare but could occur from a race condition
1134+
}
11291135
}
11301136
}
11311137

0 commit comments

Comments
 (0)