-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Use a dedicated thread for timers in rather than a Timer #21490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,8 @@ internal class Heartbeat : IDisposable | |
private readonly IDebugger _debugger; | ||
private readonly IKestrelTrace _trace; | ||
private readonly TimeSpan _interval; | ||
private Timer _timer; | ||
private int _executingOnHeartbeat; | ||
private long _lastHeartbeatTicks; | ||
private Thread _timerThread; | ||
private volatile bool _stopped; | ||
|
||
public Heartbeat(IHeartbeatHandler[] callbacks, ISystemClock systemClock, IDebugger debugger, IKestrelTrace trace) | ||
{ | ||
|
@@ -27,58 +26,62 @@ public Heartbeat(IHeartbeatHandler[] callbacks, ISystemClock systemClock, IDebug | |
_debugger = debugger; | ||
_trace = trace; | ||
_interval = Interval; | ||
_timerThread = new Thread(state => ((Heartbeat)state).TimerLoop()) | ||
{ | ||
Name = "Kestrel Timer", | ||
IsBackground = true | ||
}; | ||
} | ||
|
||
public void Start() | ||
{ | ||
OnHeartbeat(); | ||
_timer = new Timer(OnHeartbeat, state: this, dueTime: _interval, period: _interval); | ||
} | ||
|
||
private static void OnHeartbeat(object state) | ||
{ | ||
((Heartbeat)state).OnHeartbeat(); | ||
_timerThread.Start(this); | ||
} | ||
|
||
// Called by the Timer (background) thread | ||
internal void OnHeartbeat() | ||
{ | ||
var now = _systemClock.UtcNow; | ||
|
||
if (Interlocked.Exchange(ref _executingOnHeartbeat, 1) == 0) | ||
try | ||
{ | ||
Volatile.Write(ref _lastHeartbeatTicks, now.Ticks); | ||
foreach (var callback in _callbacks) | ||
{ | ||
callback.OnHeartbeat(now); | ||
} | ||
|
||
try | ||
if (!_debugger.IsAttached) | ||
{ | ||
foreach (var callback in _callbacks) | ||
var after = _systemClock.UtcNow; | ||
|
||
var duration = TimeSpan.FromTicks(after.Ticks - now.Ticks); | ||
|
||
if (duration > _interval) | ||
{ | ||
callback.OnHeartbeat(now); | ||
_trace.HeartbeatSlow(duration, _interval, now); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_trace.LogError(0, ex, $"{nameof(Heartbeat)}.{nameof(OnHeartbeat)}"); | ||
} | ||
finally | ||
{ | ||
Interlocked.Exchange(ref _executingOnHeartbeat, 0); | ||
} | ||
} | ||
else | ||
catch (Exception ex) | ||
{ | ||
if (!_debugger.IsAttached) | ||
{ | ||
var lastHeartbeatTicks = Volatile.Read(ref _lastHeartbeatTicks); | ||
|
||
_trace.HeartbeatSlow(TimeSpan.FromTicks(now.Ticks - lastHeartbeatTicks), _interval, now); | ||
} | ||
_trace.LogError(0, ex, $"{nameof(Heartbeat)}.{nameof(OnHeartbeat)}"); | ||
} | ||
} | ||
|
||
private void TimerLoop() | ||
{ | ||
while (!_stopped) | ||
{ | ||
Thread.Sleep(_interval); | ||
halter73 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
OnHeartbeat(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_timer?.Dispose(); | ||
_stopped = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not implement DisposeAsync and wait? Maybe throw after a timeout. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because it’s not important. Any hung thread will be visible in the debugger/dump. |
||
// Don't block waiting for the thread to exit | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In LibuvThread, we set IsBackground to false in debug so we can find issues where the thread isn't properly stopping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure but the libuv thread does more than this. I wouldn’t compare the 2. We could set it to true in debug if you’re paranoid about the timer not shutting down.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How/when does this get initiated? Is it lazy and triggered by a first request? i.e. does it need executioncontext flow supression?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't want an HttpContext stuck hanging around on a Thread 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Startup. Could suppress execution context but that isn't new behavior. The Timer before didn't suppress it 😄 , nothing new here.