Fix Dispose lock on GLPriceCache
Some checks failed
Build .NET Project / build (push) Has been cancelled
Some checks failed
Build .NET Project / build (push) Has been cancelled
This commit is contained in:
@@ -15,6 +15,10 @@ namespace MarketData.Cache
|
||||
// -- Singleton ------------------------------------------------------------
|
||||
private static readonly object instanceLock = new object();
|
||||
private static GLPriceCache instance = null;
|
||||
// -- Disposal -------------------------------------------------------------
|
||||
private volatile bool disposed = false;
|
||||
|
||||
private int refreshInProgress = 0;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
@@ -52,10 +56,6 @@ namespace MarketData.Cache
|
||||
// return int.TryParse(configured, out int parsed) && parsed > 0 ? parsed : @default;
|
||||
}
|
||||
|
||||
// -- Disposal -------------------------------------------------------------
|
||||
private volatile bool disposed = false;
|
||||
private int refreshInProgress = 0;
|
||||
|
||||
// -- Constructor ----------------------------------------------------------
|
||||
private GLPriceCache()
|
||||
{
|
||||
@@ -63,14 +63,17 @@ namespace MarketData.Cache
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispoal
|
||||
/// Dispose
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispose
|
||||
/// </summary>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed) return;
|
||||
@@ -78,21 +81,15 @@ namespace MarketData.Cache
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
Timer timerToDispose;
|
||||
Timer timerToDispose = null;
|
||||
lock (instanceLock)
|
||||
{
|
||||
timerToDispose = refreshTimer;
|
||||
refreshTimer = null;
|
||||
instance = null;
|
||||
timerToDispose = refreshTimer;
|
||||
refreshTimer = null;
|
||||
instance = null;
|
||||
}
|
||||
// Block until any in-flight tick completes before disposing
|
||||
using (ManualResetEventSlim waited = new ManualResetEventSlim(false))
|
||||
{
|
||||
if (timerToDispose != null)
|
||||
timerToDispose.Dispose(waited.WaitHandle);
|
||||
waited.Wait(TimeSpan.FromSeconds(10));
|
||||
}
|
||||
|
||||
// Dispose timer immediately; no blocking wait
|
||||
timerToDispose?.Dispose();
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, "[GLPriceCache:Dispose] Disposed.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user