Skip to content

Commit 103dac3

Browse files
committed
Fixed lost promotion during RandomAccessCache resize
1 parent 6b7aa7d commit 103dac3

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎src/DotNext.Tests/Runtime/Caching/RandomAccessCacheTests.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,8 @@ static async Task AddAsync(CacheWithWeight cache, string key)
568568
[Fact]
569569
public static async Task ConcurrentResize()
570570
{
571-
await using var cache = new CacheWithWeight(2, long.MaxValue, 1);
572-
await Parallel.ForAsync(0, 20_000, new ParallelOptions { CancellationToken = TestToken }, async (i, token) =>
571+
await using var cache = new CacheWithWeight(2, long.MaxValue, 2);
572+
await Parallel.ForAsync(0, 1000, new ParallelOptions { CancellationToken = TestToken }, async (i, token) =>
573573
{
574574
var session = await cache.ChangeAsync(i.ToString(), token);
575575
session.SetValue(1L);

‎src/DotNext.Threading/Runtime/Caching/RandomAccessCache.Dictionary.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,14 @@ private async ValueTask GrowAsync(BucketList oldVersion, Timeout timeout, Cancel
579579
ProcessQueue();
580580

581581
// restart eviction process
582-
RebuildEvictionState(buckets = new(oldVersion, newSize));
582+
var newVersion = new BucketList(oldVersion, newSize);
583+
RebuildEvictionState(newVersion);
583584
queueHead = queueTail = new FakeKeyValuePair();
584585
evictionTask = DoEvictionAsync(newSource);
586+
587+
// Publish the new version last. The buckets beyond the old length have new locks that are not held,
588+
// so the writers can promote to the queue as soon as the new version is visible
589+
buckets = newVersion;
585590
}
586591
finally
587592
{

0 commit comments

Comments
 (0)