@@ -918,40 +918,47 @@ public void StartMainLoop(bool useBackgroundThread)
918
918
public void HeartbeatReadTimerCallback ( object state )
919
919
{
920
920
bool shouldTerminate = false ;
921
- if ( ! m_closed )
921
+ try
922
922
{
923
- if ( ! m_heartbeatRead . WaitOne ( 0 , false ) )
923
+ if ( ! m_closed )
924
924
{
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
+ }
926
947
}
927
- else
948
+
949
+ if ( shouldTerminate )
928
950
{
929
- m_missedHeartbeats = 0 ;
951
+ TerminateMainloop ( ) ;
952
+ FinishClose ( ) ;
930
953
}
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
937
955
{
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 ) ;
944
957
}
945
- }
946
-
947
- if ( shouldTerminate )
948
- {
949
- TerminateMainloop ( ) ;
950
- FinishClose ( ) ;
951
- }
952
- else
958
+ } catch ( ObjectDisposedException ignored )
953
959
{
954
- _heartbeatReadTimer . Change ( Heartbeat * 1000 , Timeout . Infinite ) ;
960
+ // timer is already disposed,
961
+ // e.g. due to shutdown
955
962
}
956
963
}
957
964
@@ -960,26 +967,33 @@ public void HeartbeatWriteTimerCallback(object state)
960
967
bool shouldTerminate = false ;
961
968
try
962
969
{
963
- if ( ! m_closed )
970
+ try
964
971
{
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 ;
967
986
}
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
- }
978
987
979
- if ( m_closed || shouldTerminate )
988
+ if ( m_closed || shouldTerminate )
989
+ {
990
+ TerminateMainloop ( ) ;
991
+ FinishClose ( ) ;
992
+ }
993
+ } catch ( ObjectDisposedException ignored )
980
994
{
981
- TerminateMainloop ( ) ;
982
- FinishClose ( ) ;
995
+ // timer is already disposed,
996
+ // e.g. due to shutdown
983
997
}
984
998
}
985
999
0 commit comments