Skip to content

Commit 4b477ff

Browse files
Timers can be already disposed by the time they attempt to re-schedule
Due to connection closure.
1 parent 51ac73c commit 4b477ff

File tree

1 file changed

+56
-42
lines changed

1 file changed

+56
-42
lines changed

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

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -918,40 +918,47 @@ public void StartMainLoop(bool useBackgroundThread)
918918
public void HeartbeatReadTimerCallback(object state)
919919
{
920920
bool shouldTerminate = false;
921-
if (!m_closed)
921+
try
922922
{
923-
if (!m_heartbeatRead.WaitOne(0, false))
923+
if (!m_closed)
924924
{
925-
m_missedHeartbeats++;
925+
if (!m_heartbeatRead.WaitOne(0, false))
926+
{
927+
m_missedHeartbeats++;
928+
}
929+
else
930+
{
931+
m_missedHeartbeats = 0;
932+
}
933+
934+
// We check against 8 = 2 * 4 because we need to wait for at
935+
// least two complete heartbeat setting intervals before
936+
// complaining, and we've set the socket timeout to a quarter
937+
// of the heartbeat setting in setHeartbeat above.
938+
if (m_missedHeartbeats > 2 * 4)
939+
{
940+
String description = String.Format("Heartbeat missing with heartbeat == {0} seconds", m_heartbeat);
941+
var eose = new EndOfStreamException(description);
942+
m_shutdownReport.Add(new ShutdownReportEntry(description, eose));
943+
HandleMainLoopException(
944+
new ShutdownEventArgs(ShutdownInitiator.Library, 0, "End of stream", eose));
945+
shouldTerminate = true;
946+
}
926947
}
927-
else
948+
949+
if (shouldTerminate)
928950
{
929-
m_missedHeartbeats = 0;
951+
TerminateMainloop();
952+
FinishClose();
930953
}
931-
932-
// We check against 8 = 2 * 4 because we need to wait for at
933-
// least two complete heartbeat setting intervals before
934-
// complaining, and we've set the socket timeout to a quarter
935-
// of the heartbeat setting in setHeartbeat above.
936-
if (m_missedHeartbeats > 2 * 4)
954+
else
937955
{
938-
String description = String.Format("Heartbeat missing with heartbeat == {0} seconds", m_heartbeat);
939-
var eose = new EndOfStreamException(description);
940-
m_shutdownReport.Add(new ShutdownReportEntry(description, eose));
941-
HandleMainLoopException(
942-
new ShutdownEventArgs(ShutdownInitiator.Library, 0, "End of stream", eose));
943-
shouldTerminate = true;
956+
_heartbeatReadTimer.Change(Heartbeat * 1000, Timeout.Infinite);
944957
}
945-
}
946-
947-
if (shouldTerminate)
948-
{
949-
TerminateMainloop();
950-
FinishClose();
951-
}
952-
else
958+
} catch (ObjectDisposedException ignored)
953959
{
954-
_heartbeatReadTimer.Change(Heartbeat * 1000, Timeout.Infinite);
960+
// timer is already disposed,
961+
// e.g. due to shutdown
955962
}
956963
}
957964

@@ -960,26 +967,33 @@ public void HeartbeatWriteTimerCallback(object state)
960967
bool shouldTerminate = false;
961968
try
962969
{
963-
if (!m_closed)
970+
try
964971
{
965-
WriteFrame(m_heartbeatFrame);
966-
m_frameHandler.Flush();
972+
if (!m_closed)
973+
{
974+
WriteFrame(m_heartbeatFrame);
975+
m_frameHandler.Flush();
976+
}
977+
}
978+
catch (Exception e)
979+
{
980+
HandleMainLoopException(new ShutdownEventArgs(
981+
ShutdownInitiator.Library,
982+
0,
983+
"End of stream",
984+
e));
985+
shouldTerminate = true;
967986
}
968-
}
969-
catch (Exception e)
970-
{
971-
HandleMainLoopException(new ShutdownEventArgs(
972-
ShutdownInitiator.Library,
973-
0,
974-
"End of stream",
975-
e));
976-
shouldTerminate = true;
977-
}
978987

979-
if (m_closed || shouldTerminate)
988+
if (m_closed || shouldTerminate)
989+
{
990+
TerminateMainloop();
991+
FinishClose();
992+
}
993+
} catch (ObjectDisposedException ignored)
980994
{
981-
TerminateMainloop();
982-
FinishClose();
995+
// timer is already disposed,
996+
// e.g. due to shutdown
983997
}
984998
}
985999

0 commit comments

Comments
 (0)