From 6f0cf9ede6b7d257b93ede26cf87c593cffbaa11 Mon Sep 17 00:00:00 2001 From: Salvador Cipolla Date: Sat, 11 Apr 2026 18:16:33 -0300 Subject: [PATCH] Correct Infinite Busy-Wait Loop (M14 fix) --- Knossos.NET/Classes/KnUtils.cs | 5 ++++- .../ViewModels/Templates/Tasks/PrepareModPkg.cs | 15 +++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Knossos.NET/Classes/KnUtils.cs b/Knossos.NET/Classes/KnUtils.cs index 6c25c9c0..e48fb1af 100644 --- a/Knossos.NET/Classes/KnUtils.cs +++ b/Knossos.NET/Classes/KnUtils.cs @@ -1212,7 +1212,7 @@ public static bool IsFileInUse(string filePath) /// /// Deletes a file checking if it exists first and then waits for the file to be closed. /// - public static void DeleteFileSafe(string filePath) + public static async Task DeleteFileSafe(string filePath, CancellationTokenSource? cancellationToken = null) { try { @@ -1221,6 +1221,9 @@ public static void DeleteFileSafe(string filePath) while (IsFileInUse(filePath)) { Log.Add(Log.LogSeverity.Information, "TaskItemViewModel.PrepareModPkg()", "Waiting for file to be closed to delete it: " + filePath); + await Task.Delay(100); + if (cancellationToken != null && cancellationToken.IsCancellationRequested) + throw new TaskCanceledException(); } File.Delete(filePath); } diff --git a/Knossos.NET/ViewModels/Templates/Tasks/PrepareModPkg.cs b/Knossos.NET/ViewModels/Templates/Tasks/PrepareModPkg.cs index f691253f..01b929da 100644 --- a/Knossos.NET/ViewModels/Templates/Tasks/PrepareModPkg.cs +++ b/Knossos.NET/ViewModels/Templates/Tasks/PrepareModPkg.cs @@ -112,7 +112,7 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "Error while compressing the package"); //Disable failing and instead delete the file if it exists //throw new TaskCanceledException(); - KnUtils.DeleteFileSafe(zipPath); + await KnUtils.DeleteFileSafe(zipPath, cancellationTokenSource); } else { @@ -132,7 +132,7 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance ProgressBarMax = 100; ProgressCurrent = 0; Info = "Retry: Compressing (7z)"; - KnUtils.DeleteFileSafe(zipPath); + await KnUtils.DeleteFileSafe(zipPath, cancellationTokenSource); crcAttempt++; } } @@ -193,7 +193,7 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "Error while compressing the package"); //Disable failing and instead delete the file if it exists //throw new TaskCanceledException(); - KnUtils.DeleteFileSafe(zipPath + ".tar.gz"); + await KnUtils.DeleteFileSafe(zipPath + ".tar.gz", cancellationTokenSource); } else { @@ -213,7 +213,7 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance ProgressBarMax = 100; ProgressCurrent = 0; Info = "Retry: Compressing (.tar.gz)"; - KnUtils.DeleteFileSafe(zipPath + ".tar.gz"); + await KnUtils.DeleteFileSafe(zipPath + ".tar.gz", cancellationTokenSource); crcAttempt++; } } @@ -232,7 +232,7 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.PrepareModPkg()", "Error while compressing the package"); //Disable failing and instead delete the file if it exists //throw new TaskCanceledException(); - KnUtils.DeleteFileSafe(zipPath); + await KnUtils.DeleteFileSafe(zipPath, cancellationTokenSource); } else { @@ -252,7 +252,7 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance ProgressBarMax = 100; ProgressCurrent = 0; Info = "Retry: Compressing (7z)"; - KnUtils.DeleteFileSafe(zipPath); + await KnUtils.DeleteFileSafe(zipPath, cancellationTokenSource); crcAttempt++; } } @@ -267,6 +267,9 @@ private async Task PrepareModPkg(ModPackage pkg, string modFullPath, Cance { Info = "Waiting for file to be closed"; Log.Add(Log.LogSeverity.Information, "TaskItemViewModel.PrepareModPkg()", "Waiting for file to be closed: " + zipPath); + await Task.Delay(100); + if (cancellationTokenSource.IsCancellationRequested) + throw new TaskCanceledException(); } Info = "Getting Hash";