From c9c5bbb687170bcbf2583c90a3a4e8b5cd495b6b Mon Sep 17 00:00:00 2001 From: Mbucari <37587114+Mbucari@users.noreply.github.com> Date: Sun, 2 Mar 2025 10:49:09 -0700 Subject: [PATCH] Fix errorre rmoving entries from the cache (#1167) --- Source/LibationFileManager/FilePathCache.cs | 23 ++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Source/LibationFileManager/FilePathCache.cs b/Source/LibationFileManager/FilePathCache.cs index 96724d4a..469de1fd 100644 --- a/Source/LibationFileManager/FilePathCache.cs +++ b/Source/LibationFileManager/FilePathCache.cs @@ -55,8 +55,9 @@ namespace LibationFileManager { if (!File.Exists(matchingFiles[i].Path)) { + var entryToRemove = matchingFiles[i]; matchingFiles.RemoveAt(i); - cacheChanged |= Remove(matchingFiles[i]); + cacheChanged |= Remove(entryToRemove); } } if (cacheChanged) @@ -79,8 +80,9 @@ namespace LibationFileManager return matchingFiles[i].Path; else { + var entryToRemove = matchingFiles[i]; matchingFiles.RemoveAt(i); - cacheChanged |= Remove(matchingFiles[i]); + cacheChanged |= Remove(entryToRemove); } } return null; @@ -141,6 +143,7 @@ namespace LibationFileManager { [JsonProperty] private readonly ConcurrentDictionary> Dictionary = new(); + private static object lockObject = new(); public List GetIdEntries(string id) { @@ -164,7 +167,21 @@ namespace LibationFileManager } public bool Remove(string id, TEntry entry) - => Dictionary.TryGetValue(id, out List? entries) && entries.Remove(entry); + { + lock (lockObject) + { + if (Dictionary.TryGetValue(id, out List? entries)) + { + var removed = entries?.Remove(entry) ?? false; + if (removed && entries?.Count == 0) + { + Dictionary.Remove(id, out _); + } + return removed; + } + else return false; + } + } } } }