mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-08-31 19:08:29 +00:00
feat: add thread safety to error timer management in StatusMonitorBase
This commit is contained in:
parent
42b358862f
commit
4252e9c8e6
1 changed files with 21 additions and 4 deletions
|
|
@ -87,6 +87,14 @@ namespace PepperDash.Essentials.Core
|
||||||
long ErrorTime;
|
long ErrorTime;
|
||||||
Timer WarningTimer;
|
Timer WarningTimer;
|
||||||
Timer ErrorTimer;
|
Timer ErrorTimer;
|
||||||
|
|
||||||
|
// Guards WarningTimer/ErrorTimer against concurrent access - StartErrorTimers/StopErrorTimers/
|
||||||
|
// ResetErrorTimers can each be invoked from different threads (e.g. a socket data-received
|
||||||
|
// callback vs. a connection-status-changed callback), and without this lock a null-check followed
|
||||||
|
// by a dereference (e.g. in ResetErrorTimers) can race with StopErrorTimers nulling the field in
|
||||||
|
// between, throwing an unhandled NullReferenceException on a background thread that crashes the
|
||||||
|
// whole program.
|
||||||
|
private readonly object _timerLock = new object();
|
||||||
/// Constructor
|
/// Constructor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="parent">parent device</param>
|
/// <param name="parent">parent device</param>
|
||||||
|
|
@ -154,6 +162,8 @@ namespace PepperDash.Essentials.Core
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected void StartErrorTimers()
|
protected void StartErrorTimers()
|
||||||
{
|
{
|
||||||
|
lock (_timerLock)
|
||||||
|
{
|
||||||
if (WarningTimer == null)
|
if (WarningTimer == null)
|
||||||
{
|
{
|
||||||
WarningTimer = new Timer(WarningTime) { AutoReset = false };
|
WarningTimer = new Timer(WarningTime) { AutoReset = false };
|
||||||
|
|
@ -167,22 +177,28 @@ namespace PepperDash.Essentials.Core
|
||||||
ErrorTimer.Start();
|
ErrorTimer.Start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Stops the error timers
|
/// Stops the error timers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected void StopErrorTimers()
|
protected void StopErrorTimers()
|
||||||
|
{
|
||||||
|
lock (_timerLock)
|
||||||
{
|
{
|
||||||
if (WarningTimer != null) WarningTimer.Stop();
|
if (WarningTimer != null) WarningTimer.Stop();
|
||||||
if (ErrorTimer != null) ErrorTimer.Stop();
|
if (ErrorTimer != null) ErrorTimer.Stop();
|
||||||
WarningTimer = null;
|
WarningTimer = null;
|
||||||
ErrorTimer = null;
|
ErrorTimer = null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resets the error timers
|
/// Resets the error timers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected void ResetErrorTimers()
|
protected void ResetErrorTimers()
|
||||||
|
{
|
||||||
|
lock (_timerLock)
|
||||||
{
|
{
|
||||||
if (WarningTimer != null)
|
if (WarningTimer != null)
|
||||||
{
|
{
|
||||||
|
|
@ -198,4 +214,5 @@ namespace PepperDash.Essentials.Core
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue