Skip to content

Commit d4e52b5

Browse files
committed
fix(HPatchZNative): unwrap OperationCanceledException from AggregateException
SharpHDiffPatch.Core uses Task.WaitAll internally which wraps OperationCanceledException inside AggregateException. The existing catch (OperationCanceledException) didn't match, causing the cancel to fall through to the generic catch and get wrapped as an InvalidOperationException - surfacing as 'corrupted installation'. Add FindCancellation() recursive helper that walks AggregateException and InnerException chains. New catch clause unwraps and re-throws the original OperationCanceledException for both ApplyPatch and ApplyDirPatch.
1 parent 8b4ce67 commit d4e52b5

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Hi3Helper.Plugin.Wuwa/Utils/HPatchZNative.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ internal static void ApplyPatch(string sourceFilePath, string diffFilePath, stri
5757
catch { /* ignore cleanup errors */ }
5858
throw;
5959
}
60+
catch (Exception ex) when (FindCancellation(ex) is { } oce)
61+
{
62+
try { if (File.Exists(outputFilePath)) File.Delete(outputFilePath); }
63+
catch { /* ignore cleanup errors */ }
64+
throw oce;
65+
}
6066
catch (Exception ex)
6167
{
6268
SharedStatic.InstanceLogger.LogError(
@@ -121,6 +127,14 @@ internal static void ApplyDirPatch(string sourceDir, string diffFilePath, string
121127
catch { /* ignore cleanup errors */ }
122128
throw;
123129
}
130+
catch (Exception ex) when (FindCancellation(ex) is { } oce)
131+
{
132+
// SharpHDiffPatch wraps OperationCanceledException inside AggregateException
133+
// from Task.WaitAll. Unwrap and re-throw as a proper cancellation.
134+
try { if (Directory.Exists(outputDir)) Directory.Delete(outputDir, true); }
135+
catch { /* ignore cleanup errors */ }
136+
throw oce;
137+
}
124138
catch (Exception ex)
125139
{
126140
SharedStatic.InstanceLogger.LogError(
@@ -137,4 +151,26 @@ internal static void ApplyDirPatch(string sourceDir, string diffFilePath, string
137151
SharedStatic.InstanceLogger.LogDebug(
138152
"[HPatchZNative::ApplyDirPatch] Dir patch applied successfully: {Output}", outputDir);
139153
}
154+
155+
/// <summary>
156+
/// Walks the exception's InnerException chain (and AggregateException.InnerExceptions)
157+
/// looking for an <see cref="OperationCanceledException"/>.
158+
/// </summary>
159+
private static OperationCanceledException? FindCancellation(Exception ex)
160+
{
161+
if (ex is OperationCanceledException oce)
162+
return oce;
163+
164+
if (ex is AggregateException agg)
165+
{
166+
foreach (var inner in agg.InnerExceptions)
167+
{
168+
var found = FindCancellation(inner);
169+
if (found != null)
170+
return found;
171+
}
172+
}
173+
174+
return ex.InnerException != null ? FindCancellation(ex.InnerException) : null;
175+
}
140176
}

0 commit comments

Comments
 (0)