Skip to content

Commit 2f473a1

Browse files
committed
better git call handling
prevents overlapping calls per-folder cleanly
1 parent d56fd90 commit 2f473a1

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

src/BuiltinExtensions/ComfyUIBackend/ComfyUISelfStartBackend.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public static async Task<bool> EnsureNodeRepo(string url)
6060
string folderName = url.AfterLast('/');
6161
if (!Directory.Exists($"{nodePath}/{folderName}"))
6262
{
63-
await Process.Start(new ProcessStartInfo("git", $"clone {url}") { WorkingDirectory = nodePath }).WaitForExitAsync(Program.GlobalProgramCancel);
63+
string response = await Utilities.RunGitProcess($"clone {url}", nodePath);
64+
Logs.Debug($"Comfy node clone response for {folderName}: {response.Trim()}");
6465
string reqFile = $"{nodePath}/{folderName}/requirements.txt";
6566
ComfyUISelfStartBackend[] backends = [.. Program.Backends.RunningBackendsOfType<ComfyUISelfStartBackend>()];
6667
if (File.Exists(reqFile) && backends.Any())
@@ -92,7 +93,8 @@ public static async Task<bool> EnsureNodeRepo(string url)
9293
}
9394
else
9495
{
95-
await NetworkBackendUtils.RunProcessWithMonitoring(new ProcessStartInfo("git", "pull") { WorkingDirectory = Path.GetFullPath($"{nodePath}/{folderName}") }, $"comfy node pull ({folderName})", "comfynodepull");
96+
string response = await Utilities.RunGitProcess($"pull", $"{nodePath}/{folderName}");
97+
Logs.Debug($"Comfy node pull response for {folderName}: {response.Trim()}");
9698
}
9799
return false;
98100
}
@@ -117,7 +119,8 @@ public static async Task EnsureNodeRepos()
117119
{
118120
if (Directory.Exists($"{node}/.git"))
119121
{
120-
tasks.Add(NetworkBackendUtils.RunProcessWithMonitoring(new ProcessStartInfo("git", "pull") { WorkingDirectory = node }, $"comfy node pull ({node.Replace('\\', '/').AfterLast('/')})", "comfynodepull"));
122+
string response = await Utilities.RunGitProcess($"pull", node);
123+
Logs.Debug($"Comfy node pull response for {node.Replace('\\', '/').AfterLast('/')}: {response.Trim()}");
121124
}
122125
}
123126
await Task.WhenAll(tasks);
@@ -234,15 +237,8 @@ public override async Task Init()
234237
{
235238
try
236239
{
237-
ProcessStartInfo psi = new("git", "pull")
238-
{
239-
WorkingDirectory = Path.GetFullPath(settings.StartScript).Replace('\\', '/').BeforeLast('/'),
240-
RedirectStandardError = true,
241-
RedirectStandardOutput = true
242-
};
243-
Process p = Process.Start(psi);
244-
NetworkBackendUtils.ReportLogsFromProcess(p, "ComfyUI (Git Pull)", "");
245-
await p.WaitForExitAsync(Program.GlobalProgramCancel);
240+
string response = await Utilities.RunGitProcess($"pull", Path.GetFullPath(settings.StartScript).Replace('\\', '/').BeforeLast('/'));
241+
Logs.Debug($"Comfy git pull response: {response.Trim()}");
246242
}
247243
catch (Exception ex)
248244
{

src/SwarmUI.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="FreneticLLC.FreneticUtilities" Version="1.0.29" />
12-
<PackageReference Include="Hardware.Info" Version="100.1.0" />
11+
<PackageReference Include="FreneticLLC.FreneticUtilities" Version="1.0.32" />
12+
<PackageReference Include="Hardware.Info" Version="100.1.0.1" />
1313
<PackageReference Include="LiteDB" Version="5.0.17" />
1414
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1515
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />

src/Utils/Utilities.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -840,17 +840,32 @@ void Warn()
840840
});
841841
}
842842

843+
/// <summary>MultiSemaphoreSet to prevent git calls in the same directory from overlapping.</summary>
844+
public static MultiSemaphoreSet<string> GitOverlapLocks = new(32);
845+
843846
/// <summary>Launch, run, and return the text output of, a 'git' command input.</summary>
844-
public static async Task<string> RunGitProcess(string args)
847+
public static async Task<string> RunGitProcess(string args, string dir = null)
845848
{
849+
dir ??= Environment.CurrentDirectory;
850+
dir = Path.GetFullPath(dir);
846851
ProcessStartInfo start = new("git", args)
847852
{
848853
RedirectStandardOutput = true,
849-
UseShellExecute = false
854+
UseShellExecute = false,
855+
WorkingDirectory = dir
850856
};
851-
Process p = Process.Start(start);
852-
await p.WaitForExitAsync(Program.GlobalProgramCancel);
853-
return await p.StandardOutput.ReadToEndAsync();
857+
SemaphoreSlim semaphore = GitOverlapLocks.GetLock(dir);
858+
semaphore.Wait();
859+
try
860+
{
861+
Process p = Process.Start(start);
862+
await p.WaitForExitAsync(Program.GlobalProgramCancel);
863+
return await p.StandardOutput.ReadToEndAsync();
864+
}
865+
finally
866+
{
867+
semaphore.Release();
868+
}
854869
}
855870

856871
/// <summary>Deletes a file 'cleanly' by sending it to the recycle bin.</summary>

src/WebAPI/BasicAPIFeatures.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ void moveFolder()
222222
await output("Fixing Comfy install...");
223223
// Note: the old Python 3.10 comfy file is needed for AMD, and it has a cursed git config (mandatory auth header? argh) so this is a hack-fix for that
224224
File.WriteAllBytes("dlbackend/comfy/ComfyUI/.git/config", "[core]\n\trepositoryformatversion = 0\n\tfilemode = false\n\tbare = false\n\tlogallrefupdates = true\n\tignorecase = true\n[remote \"origin\"]\n\turl = https://github.com/comfyanonymous/ComfyUI\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n[gc]\n\tauto = 0\n[branch \"master\"]\n\tremote = origin\n\tmerge = refs/heads/master\n[lfs]\n\trepositoryformatversion = 0\n[remote \"upstream\"]\n\turl = https://github.com/comfyanonymous/ComfyUI.git\n\tfetch = +refs/heads/*:refs/remotes/upstream/*\n".EncodeUTF8());
225-
await NetworkBackendUtils.RunProcessWithMonitoring(new ProcessStartInfo("git", "pull") { WorkingDirectory = $"{comfyFolderPath}/ComfyUI" }, "ComfyUI Install (git pull)", "comfyinstall");
225+
string response = await Utilities.RunGitProcess($"pull", $"{comfyFolderPath}/ComfyUI");
226+
Logs.Debug($"ComfyUI Install git pull response: {response}");
226227
await NetworkBackendUtils.RunProcessWithMonitoring(new ProcessStartInfo($"{comfyFolderPath}/python_embeded/python.exe", "-s -m pip install -U -r ComfyUI/requirements.txt") { WorkingDirectory = comfyFolderPath }, "ComfyUI Install (python requirements)", "comfyinstall");
227228
await output("Installing AMD compatible Torch-DirectML...");
228229
await NetworkBackendUtils.RunProcessWithMonitoring(new ProcessStartInfo($"{comfyFolderPath}/python_embeded/python.exe", "-s -m pip install torch-directml") { UseShellExecute = false, WorkingDirectory = comfyFolderPath }, "ComfyUI Install (directml)", "comfyinstall");

0 commit comments

Comments
 (0)