Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,20 @@ internal void OnHeartbeat()
}
catch (Exception ex)
{
_trace.LogError(0, ex, $"{nameof(Heartbeat)}.{nameof(OnHeartbeat)}");
if (!_stopped)
{
_trace.LogError(0, ex, $"{nameof(Heartbeat)}.{nameof(OnHeartbeat)}");
}
}
}

private void TimerLoop()
{
Thread.Sleep(_interval);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an initial Thread.Sleep before the timer loop to avoid a potential breaking change. The previous code guaranteed a Thread.Sleep call before the first invocation of OnHeatbeat. I wanted to avoid the possibility of any latent issues of a heartbeat handler failing if called too soon. This change maintains this behavior.

while (!_stopped)
{
Thread.Sleep(_interval);

OnHeartbeat();
Thread.Sleep(_interval);
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/Servers/Kestrel/Core/test/HeartbeatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,20 @@ public void ExceptionFromHeartbeatHandlerIsLoggedAsError()

Assert.Equal(ex, TestSink.Writes.Single(message => message.LogLevel == LogLevel.Error).Exception);
}

[Fact]
public void ExceptionFromHeartbeatHandlerIsNotLoggedIfDisposed()
{
var systemClock = new MockSystemClock();
var heartbeatHandler = new Mock<IHeartbeatHandler>();
var debugger = new Mock<IDebugger>();
var kestrelTrace = new KestrelTrace(LoggerFactory);
var ex = new Exception();
heartbeatHandler.Setup(h => h.OnHeartbeat(systemClock.UtcNow)).Throws(ex);
debugger.Setup(d => d.IsAttached).Returns(true);
var heartbeat = new Heartbeat(new[] { heartbeatHandler.Object }, systemClock, debugger.Object, kestrelTrace);
heartbeat.Dispose();
heartbeat.OnHeartbeat();
Assert.Empty(TestSink.Writes);
}
}