From cb2962280087aa097f99c6f95c3c8a4416fdbf07 Mon Sep 17 00:00:00 2001 From: Tal Zaccai Date: Fri, 7 Mar 2025 18:03:46 -0800 Subject: [PATCH] bugfix --- libs/server/GarnetDatabase.cs | 8 ++++++-- libs/server/Storage/SizeTracker/CacheSizeTracker.cs | 12 +++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/libs/server/GarnetDatabase.cs b/libs/server/GarnetDatabase.cs index 21bf726de6..0373a59c9b 100644 --- a/libs/server/GarnetDatabase.cs +++ b/libs/server/GarnetDatabase.cs @@ -140,8 +140,12 @@ public void Dispose() if (ObjectStoreSizeTracker != null) { - while (!ObjectStoreSizeTracker.Stopped) - Thread.Yield(); + // If tracker has previously started, wait for it to stop + if (!ObjectStoreSizeTracker.TryPreventStart()) + { + while (!ObjectStoreSizeTracker.Stopped) + Thread.Yield(); + } } disposed = true; diff --git a/libs/server/Storage/SizeTracker/CacheSizeTracker.cs b/libs/server/Storage/SizeTracker/CacheSizeTracker.cs index be2b7cc9ef..fe226a500f 100644 --- a/libs/server/Storage/SizeTracker/CacheSizeTracker.cs +++ b/libs/server/Storage/SizeTracker/CacheSizeTracker.cs @@ -27,7 +27,7 @@ public class CacheSizeTracker int isStarted = 0; private const int deltaFraction = 10; // 10% of target size - private TsavoriteKV store; + TsavoriteKV store; internal bool Stopped => (mainLogTracker == null || mainLogTracker.Stopped) && (readCacheTracker == null || readCacheTracker.Stopped); @@ -118,5 +118,15 @@ public void AddReadCacheTrackedSize(long size) // just for the main log this.readCacheTracker?.IncrementSize(size); } + + /// + /// If tracker has not started, prevent it from starting + /// + /// True if tracker hasn't previously started + public bool TryPreventStart() + { + var prevStarted = Interlocked.CompareExchange(ref isStarted, 1, 0); + return prevStarted == 0; + } } } \ No newline at end of file