Deadlock detection? #585
-
|
I ran into a deadlock in the process of converting some older caching code to use FusionCache. The below program reproduces it. Basically the idea was that This obviously (in retrospect) deadlocks. If I uncomment This all makes sense tho I clearly missed it and made some mistakes in my conversion. My question is: what support does FusionCache have for deadlock detection? This is obviously an easy case: a single call chain It's worth mentioning that, with a debugger attached, the real caching object construction methods involved are complex & slow enough that it's not always obvious if something has deadlocked or is still plugging along 5 minutes later. #:package ZiggyCreatures.FusionCache@2.4.0
using ZiggyCreatures.Caching.Fusion;
using System.Collections.Generic;
var options = new FusionCacheOptions() {
DefaultEntryOptions = new FusionCacheEntryOptions {
// LockTimeout = TimeSpan.FromSeconds(1),
},
};
var cache = new FusionCache(options);
var d = GetDict();
foreach (var (k, v) in d) {
System.Console.WriteLine($"[{k}] = {v}");
}
IReadOnlyDictionary<string, int> BuildDict(int i) {
return new Dictionary<string, int>() {
["i"] = i,
};
}
int GetInt() {
return cache.GetOrSet<int>("i", ct => {
var i = 10;
// cache.Set("d", () => BuildDict(i));
cache.GetOrSet("d", () => BuildDict(i));
return i;
});
}
IReadOnlyDictionary<string, int> GetDict() {
return cache.GetOrSet<IReadOnlyDictionary<string, int>>("d", ct => {
var i = GetInt();
return BuildDict(i);
});
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @oconnor0 , currently a lock cannot be acquired twice (see: re-entrancy) by the same subject. Currently in FusionCache the component responsible for it is the I thought about thsi in the past, but I've never been sure if it was possible at all, at least considering all the constraints:
Also, there's this. Opinions? Let me know what you think. |
Beta Was this translation helpful? Give feedback.
Hi @oconnor0 , currently a lock cannot be acquired twice (see: re-entrancy) by the same subject.
So yes, trying to do that will inadvertently get into a deadlock.
Currently in FusionCache the component responsible for it is the
StandardDistributedLocker(default implementation of theIFusionCacheMemoryLockercore abstraction), which internally uses a series ofSemaphoreSlims.I thought about thsi in the past, but I've never been sure if it was possible at all, at least considering all the constraints:
TimeSpan.Zero), because it's used for Eager Refresh and other stuffAlso, there's this.
O…