Skip to content

Commit 62ab4c2

Browse files
committed
Strip persisted ComfyUI --normalvram from settings.json on settings load
1 parent da01036 commit 62ab4c2

2 files changed

Lines changed: 178 additions & 0 deletions

File tree

StabilityMatrix.Core/Services/SettingsManager.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ public void SetEulaAccepted()
460460
/// </summary>
461461
protected virtual void LoadSettings(CancellationToken cancellationToken = default)
462462
{
463+
var shouldPersistNormalizedSettings = false;
464+
463465
fileLock.Wait(cancellationToken);
464466

465467
try
@@ -478,13 +480,26 @@ protected virtual void LoadSettings(CancellationToken cancellationToken = defaul
478480
}
479481

480482
Settings = DeserializeOrRecoverSettings(rawBytes);
483+
shouldPersistNormalizedSettings = NormalizeLoadedSettings(Settings);
481484
}
482485
finally
483486
{
484487
fileLock.Release();
485488

486489
isLoaded = true;
487490

491+
if (shouldPersistNormalizedSettings)
492+
{
493+
try
494+
{
495+
SaveSettings(cancellationToken);
496+
}
497+
catch (Exception ex)
498+
{
499+
logger.LogWarning(ex, "Failed to persist normalized settings after load");
500+
}
501+
}
502+
488503
Loaded?.Invoke(this, EventArgs.Empty);
489504
}
490505
}
@@ -495,6 +510,8 @@ protected virtual void LoadSettings(CancellationToken cancellationToken = defaul
495510
/// </summary>
496511
protected virtual async Task LoadSettingsAsync(CancellationToken cancellationToken = default)
497512
{
513+
var shouldPersistNormalizedSettings = false;
514+
498515
await fileLock.WaitAsync(cancellationToken).ConfigureAwait(false);
499516

500517
try
@@ -514,17 +531,61 @@ protected virtual async Task LoadSettingsAsync(CancellationToken cancellationTok
514531
}
515532

516533
Settings = DeserializeOrRecoverSettings(rawBytes);
534+
shouldPersistNormalizedSettings = NormalizeLoadedSettings(Settings);
517535
}
518536
finally
519537
{
520538
fileLock.Release();
521539

522540
isLoaded = true;
523541

542+
if (shouldPersistNormalizedSettings)
543+
{
544+
try
545+
{
546+
await SaveSettingsAsync(cancellationToken).ConfigureAwait(false);
547+
}
548+
catch (Exception ex)
549+
{
550+
logger.LogWarning(ex, "Failed to persist normalized settings after load");
551+
}
552+
}
553+
524554
Loaded?.Invoke(this, EventArgs.Empty);
525555
}
526556
}
527557

558+
private bool NormalizeLoadedSettings(Settings settings)
559+
{
560+
var removedCount = 0;
561+
562+
foreach (var package in settings.InstalledPackages)
563+
{
564+
if (
565+
package.PackageName?.StartsWith("ComfyUI", StringComparison.OrdinalIgnoreCase) != true
566+
|| package.LaunchArgs is not { Count: > 0 }
567+
)
568+
{
569+
continue;
570+
}
571+
572+
removedCount += package.LaunchArgs.RemoveAll(option =>
573+
option.Name.Equals("--normalvram", StringComparison.OrdinalIgnoreCase)
574+
);
575+
}
576+
577+
if (removedCount > 0)
578+
{
579+
logger.LogInformation(
580+
"Removed {RemovedCount} obsolete ComfyUI launch args from loaded settings",
581+
removedCount
582+
);
583+
return true;
584+
}
585+
586+
return false;
587+
}
588+
528589
/// <summary>
529590
/// Attempts to deserialize settings from raw bytes, falling back to sanitization
530591
/// and recovery if the JSON is corrupted. Returns default settings as a last resort.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System.Text.Json;
2+
using Microsoft.Extensions.Logging.Abstractions;
3+
using StabilityMatrix.Core.Models.Settings;
4+
using StabilityMatrix.Core.Services;
5+
6+
namespace StabilityMatrix.Tests.Core;
7+
8+
[TestClass]
9+
public class SettingsManagerTests
10+
{
11+
private string? tempDirectory;
12+
13+
[TestCleanup]
14+
public void Cleanup()
15+
{
16+
if (tempDirectory is not null && Directory.Exists(tempDirectory))
17+
{
18+
Directory.Delete(tempDirectory, recursive: true);
19+
}
20+
}
21+
22+
[TestMethod]
23+
public void TryFindLibrary_StripsObsoleteComfyNormalVramLaunchArgsAndPersists()
24+
{
25+
tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
26+
Directory.CreateDirectory(tempDirectory);
27+
28+
var settingsPath = Path.Combine(tempDirectory, "settings.json");
29+
File.WriteAllText(
30+
settingsPath,
31+
"""
32+
{
33+
"Version": 1,
34+
"InstalledPackages": [
35+
{
36+
"Id": "11111111-1111-1111-1111-111111111111",
37+
"PackageName": "ComfyUI",
38+
"LaunchArgs": [
39+
{
40+
"Name": "--normalvram",
41+
"Type": "Bool",
42+
"OptionValue": true
43+
},
44+
{
45+
"Name": "--lowvram",
46+
"Type": "Bool",
47+
"OptionValue": false
48+
}
49+
]
50+
},
51+
{
52+
"Id": "22222222-2222-2222-2222-222222222222",
53+
"PackageName": "ComfyUI-Zluda",
54+
"LaunchArgs": [
55+
{
56+
"Name": "--normalvram",
57+
"Type": "Bool",
58+
"OptionValue": true
59+
}
60+
]
61+
},
62+
{
63+
"Id": "33333333-3333-3333-3333-333333333333",
64+
"PackageName": "OtherPackage",
65+
"LaunchArgs": [
66+
{
67+
"Name": "--normalvram",
68+
"Type": "Bool",
69+
"OptionValue": true
70+
}
71+
]
72+
}
73+
]
74+
}
75+
"""
76+
);
77+
78+
var settingsManager = new SettingsManager(NullLogger<SettingsManager>.Instance);
79+
settingsManager.SetLibraryDirOverride(tempDirectory);
80+
81+
var wasFound = settingsManager.TryFindLibrary();
82+
83+
Assert.IsTrue(wasFound);
84+
85+
var comfyPackage = settingsManager.Settings.InstalledPackages.Single(package =>
86+
package.PackageName == "ComfyUI"
87+
);
88+
var zludaPackage = settingsManager.Settings.InstalledPackages.Single(package =>
89+
package.PackageName == "ComfyUI-Zluda"
90+
);
91+
var otherPackage = settingsManager.Settings.InstalledPackages.Single(package =>
92+
package.PackageName == "OtherPackage"
93+
);
94+
95+
Assert.IsFalse(comfyPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
96+
Assert.IsFalse(zludaPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
97+
Assert.IsTrue(otherPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
98+
99+
var persistedSettings = JsonSerializer.Deserialize<Settings>(File.ReadAllText(settingsPath));
100+
101+
Assert.IsNotNull(persistedSettings);
102+
103+
var persistedComfyPackage = persistedSettings.InstalledPackages.Single(package =>
104+
package.PackageName == "ComfyUI"
105+
);
106+
var persistedZludaPackage = persistedSettings.InstalledPackages.Single(package =>
107+
package.PackageName == "ComfyUI-Zluda"
108+
);
109+
var persistedOtherPackage = persistedSettings.InstalledPackages.Single(package =>
110+
package.PackageName == "OtherPackage"
111+
);
112+
113+
Assert.IsFalse(persistedComfyPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
114+
Assert.IsFalse(persistedZludaPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
115+
Assert.IsTrue(persistedOtherPackage.LaunchArgs!.Any(option => option.Name == "--normalvram"));
116+
}
117+
}

0 commit comments

Comments
 (0)