Skip to content

Commit afc3857

Browse files
authored
Merge pull request #708 from ionite34/backport/main/pr-707
[dev to main] backport: Fix exceptions in litedb deserialize for ModelIndexService (707)
2 parents 94efba3 + 5e00e12 commit afc3857

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning 2.0](https://semver.org/spec/v2
1515
- Fooocus Package - Added `pip>=23.3.2,<24.1` specifier before install, fixes potential install errors due to deprecated requirement spec used by `torchsde`.
1616
- Fixed error when launching SwarmUI when installed to a path with spaces
1717
- Fixed issue where model folders were being created too late in certain cases
18+
- Fixed [#683](https://github.com/LykosAI/StabilityMatrix/issues/683) - Model indexing causing LiteDB errors after upgrading from older versions due to updated enum values
1819
### Supporters
1920
#### Visionaries
2021
- Huge thanks to our Visionary-tier supporters on Patreon, **Scopp Mcdee** and **Waterclouds**! Your support helps us continue to improve Stability Matrix!

StabilityMatrix.Core/Database/ILiteDbContext.cs

+5
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public interface ILiteDbContext : IDisposable
2121
Task<bool> UpsertCivitModelQueryCacheEntryAsync(CivitModelQueryCacheEntry entry);
2222
Task<GithubCacheEntry?> GetGithubCacheEntry(string cacheKey);
2323
Task<bool> UpsertGithubCacheEntry(GithubCacheEntry cacheEntry);
24+
25+
/// <summary>
26+
/// Clear all Collections that store re-fetchable cache type data.
27+
/// </summary>
28+
Task ClearAllCacheCollectionsAsync();
2429
}

StabilityMatrix.Core/Database/LiteDbContext.cs

+24
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,30 @@ public async Task<bool> UpsertCivitModelQueryCacheEntryAsync(CivitModelQueryCach
174174
public Task<bool> UpsertGithubCacheEntry(GithubCacheEntry cacheEntry) =>
175175
GithubCache.UpsertAsync(cacheEntry);
176176

177+
/// <summary>
178+
/// Clear all Collections that store re-fetchable cache type data.
179+
/// </summary>
180+
public async Task ClearAllCacheCollectionsAsync()
181+
{
182+
var collectionNames = new List<string>
183+
{
184+
nameof(CivitModels),
185+
nameof(CivitModelVersions),
186+
nameof(CivitModelQueryCache),
187+
nameof(GithubCache),
188+
nameof(LocalModelFiles),
189+
nameof(LocalImageFiles)
190+
};
191+
192+
logger.LogInformation("Clearing all cache collections: [{@Names}]", collectionNames);
193+
194+
foreach (var name in collectionNames)
195+
{
196+
var collection = Database.GetCollection(name);
197+
await collection.DeleteAllAsync().ConfigureAwait(false);
198+
}
199+
}
200+
177201
public void Dispose()
178202
{
179203
if (lazyDatabase.IsValueCreated)

StabilityMatrix.Core/Services/ModelIndexService.cs

+58-9
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
using System.Collections.Immutable;
33
using System.Diagnostics;
44
using System.Text;
5-
using System.Text.Json;
65
using AsyncAwaitBestPractices;
76
using AutoCtor;
87
using KGySoft.CoreLibraries;
8+
using LiteDB;
9+
using LiteDB.Async;
910
using Microsoft.Extensions.Logging;
1011
using StabilityMatrix.Core.Attributes;
1112
using StabilityMatrix.Core.Database;
@@ -15,6 +16,7 @@
1516
using StabilityMatrix.Core.Models.Api;
1617
using StabilityMatrix.Core.Models.Database;
1718
using StabilityMatrix.Core.Models.FileInterfaces;
19+
using JsonSerializer = System.Text.Json.JsonSerializer;
1820

1921
namespace StabilityMatrix.Core.Services;
2022

@@ -103,9 +105,34 @@ private async Task LoadFromDbAsync()
103105

104106
logger.LogInformation("Loading models from database...");
105107

106-
var allModels = (
107-
await liteDbContext.LocalModelFiles.IncludeAll().FindAllAsync().ConfigureAwait(false)
108-
).ToImmutableArray();
108+
ImmutableArray<LocalModelFile> allModels = [];
109+
try
110+
{
111+
allModels =
112+
[
113+
..(
114+
await liteDbContext.LocalModelFiles.IncludeAll().FindAllAsync().ConfigureAwait(false)
115+
)
116+
];
117+
}
118+
catch (Exception e)
119+
{
120+
// Handle enum deserialize exceptions from changes
121+
if (e is LiteException or LiteAsyncException && e.InnerException is ArgumentException inner)
122+
{
123+
logger.LogWarning(
124+
e,
125+
"LiteDb Deserialize error while fetching LocalModelFiles '{Inner}', cache collections will be cleared",
126+
inner.ToString()
127+
);
128+
129+
await liteDbContext.ClearAllCacheCollectionsAsync().ConfigureAwait(false);
130+
}
131+
else
132+
{
133+
throw;
134+
}
135+
}
109136

110137
ModelIndex = allModels.GroupBy(m => m.SharedFolderType).ToDictionary(g => g.Key, g => g.ToList());
111138

@@ -479,12 +506,34 @@ private async Task RefreshIndexParallelCore()
479506

480507
if (model.LatestModelInfo == null && model.HasConnectedModel)
481508
{
482-
var civitModel = await liteDbContext
483-
.CivitModels.Include(m => m.ModelVersions)
484-
.FindByIdAsync(model.ConnectedModelInfo.ModelId)
485-
.ConfigureAwait(false);
509+
try
510+
{
511+
model.LatestModelInfo = await liteDbContext
512+
.CivitModels.Include(m => m.ModelVersions)
513+
.FindByIdAsync(model.ConnectedModelInfo.ModelId)
514+
.ConfigureAwait(false);
515+
}
516+
catch (Exception e)
517+
{
518+
// Handle enum deserialize exceptions from changes
519+
if (
520+
e is LiteException or LiteAsyncException
521+
&& e.InnerException is ArgumentException inner
522+
)
523+
{
524+
logger.LogWarning(
525+
e,
526+
"LiteDb Deserialize error while fetching CivitModels '{Inner}', cache collections will be cleared",
527+
inner.ToString()
528+
);
486529

487-
model.LatestModelInfo = civitModel;
530+
await liteDbContext.ClearAllCacheCollectionsAsync().ConfigureAwait(false);
531+
}
532+
else
533+
{
534+
throw;
535+
}
536+
}
488537
}
489538
var list = newIndex.GetOrAdd(model.SharedFolderType);
490539
list.Add(model);

0 commit comments

Comments
 (0)