Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 9cdc874

Browse files
committed
Fix running TaskQueue and catching errors
1 parent 05fd289 commit 9cdc874

File tree

7 files changed

+65
-46
lines changed

7 files changed

+65
-46
lines changed

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ private ITask HookupHandlers(ITask task, bool filesystemChangesExpected)
460460
}
461461
};
462462

463-
task.Finally(success =>
463+
task.OnEnd += (_, __, ___) =>
464464
{
465465
if (filesystemChangesExpected)
466466
{
@@ -473,6 +473,21 @@ private ITask HookupHandlers(ITask task, bool filesystemChangesExpected)
473473
//Logger.Trace("Ended Operation - Clearing Busy Flag");
474474
IsBusy = false;
475475
}
476+
};
477+
task.Catch(_ =>
478+
{
479+
if (filesystemChangesExpected)
480+
{
481+
//Logger.Trace("Ended Operation - Enable Watcher");
482+
watcher.Start();
483+
}
484+
485+
if (isExclusive)
486+
{
487+
//Logger.Trace("Ended Operation - Clearing Busy Flag");
488+
IsBusy = false;
489+
}
490+
476491
});
477492
return task;
478493
}

src/GitHub.Api/Installer/UnzipTask.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public override NPath RunWithReturn(bool success)
3838
}
3939
catch (Exception ex)
4040
{
41-
Errors = ex.Message;
4241
if (!RaiseFaultHandlers(ex))
4342
throw;
4443
}

src/GitHub.Api/Tasks/ActionTask.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TaskQueue : TaskBase
1010
private TaskCompletionSource<bool> aggregateTask = new TaskCompletionSource<bool>();
1111
private readonly List<ITask> queuedTasks = new List<ITask>();
1212
private volatile bool isSuccessful = true;
13-
private volatile Exception exception;
13+
private volatile Exception taskException;
1414
private int finishedTaskCount;
1515

1616
public TaskQueue() : base()
@@ -21,6 +21,7 @@ public TaskQueue() : base()
2121
public ITask Queue(ITask task)
2222
{
2323
task.OnEnd += TaskFinished;
24+
task.Catch(e => TaskFinished(task, false, e));
2425
queuedTasks.Add(task);
2526
return this;
2627
}
@@ -37,7 +38,7 @@ private void TaskFinished(ITask task, bool success, Exception ex)
3738
if (!success)
3839
{
3940
isSuccessful = false;
40-
exception = ex;
41+
taskException = ex;
4142
}
4243
var count = Interlocked.Increment(ref finishedTaskCount);
4344
if (count == queuedTasks.Count)
@@ -48,7 +49,7 @@ private void TaskFinished(ITask task, bool success, Exception ex)
4849
}
4950
else
5051
{
51-
aggregateTask.TrySetException(exception);
52+
aggregateTask.TrySetException(taskException.GetBaseException());
5253
}
5354
}
5455
}
@@ -105,7 +106,6 @@ public override void Run(bool success)
105106
}
106107
catch (Exception ex)
107108
{
108-
Errors = ex.Message;
109109
if (!RaiseFaultHandlers(ex))
110110
throw;
111111
}
@@ -207,7 +207,6 @@ protected virtual void Run(bool success, T previousResult)
207207
}
208208
catch (Exception ex)
209209
{
210-
Errors = ex.Message;
211210
if (!RaiseFaultHandlers(ex))
212211
throw;
213212
}
@@ -275,7 +274,6 @@ public override T RunWithReturn(bool success)
275274
}
276275
catch (Exception ex)
277276
{
278-
Errors = ex.Message;
279277
if (!RaiseFaultHandlers(ex))
280278
throw;
281279
}
@@ -336,7 +334,6 @@ protected override TResult RunWithData(bool success, T previousResult)
336334
}
337335
catch (Exception ex)
338336
{
339-
Errors = ex.Message;
340337
if (!RaiseFaultHandlers(ex))
341338
throw;
342339
}
@@ -402,16 +399,8 @@ public override List<T> RunWithReturn(bool success)
402399
result = CallbackWithException(success, thrown);
403400
}
404401
}
405-
catch (AggregateException ex)
406-
{
407-
var e = ex.GetBaseException();
408-
Errors = e.Message;
409-
if (!RaiseFaultHandlers(e))
410-
throw e;
411-
}
412402
catch (Exception ex)
413403
{
414-
Errors = ex.Message;
415404
if (!RaiseFaultHandlers(ex))
416405
throw;
417406
}
@@ -469,7 +458,6 @@ protected override List<TResult> RunWithData(bool success, T previousResult)
469458
}
470459
catch (Exception ex)
471460
{
472-
Errors = ex.Message;
473461
if (!RaiseFaultHandlers(ex))
474462
throw;
475463
}
@@ -481,4 +469,4 @@ protected override List<TResult> RunWithData(bool success, T previousResult)
481469
return result;
482470
}
483471
}
484-
}
472+
}

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public override NPath RunWithReturn(bool success)
6161
}
6262
catch (Exception ex)
6363
{
64-
Errors = ex.Message;
6564
if (!RaiseFaultHandlers(ex))
6665
throw;
6766
}

src/GitHub.Api/Tasks/TaskBase.cs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,11 @@ protected void Initialize(Task task)
158158
Token.ThrowIfCancellationRequested();
159159
tk.RunSynchronously(scheduler);
160160
}
161+
else
162+
tk.Wait();
161163
}
162164
catch (Exception ex)
163165
{
164-
Errors = ex.Message;
165166
if (!RaiseFaultHandlers(ex))
166167
throw;
167168
Token.ThrowIfCancellationRequested();
@@ -245,11 +246,18 @@ public ITask Catch(Action<Exception> handler)
245246
public ITask Catch(Func<Exception, bool> handler)
246247
{
247248
Guard.ArgumentNotNull(handler, "handler");
248-
catchHandler += handler;
249+
CatchInternal(handler);
249250
DependsOn?.Catch(handler);
250251
return this;
251252
}
252253

254+
internal ITask CatchInternal(Func<Exception, bool> handler)
255+
{
256+
Guard.ArgumentNotNull(handler, "handler");
257+
catchHandler += handler;
258+
return this;
259+
}
260+
253261
/// <summary>
254262
/// Run a callback at the end of the task execution, on the same thread as the task that just finished, regardless of execution state
255263
/// This will always run on the same thread as the previous task
@@ -268,7 +276,14 @@ public ITask Finally(Action<bool> handler)
268276
public ITask Finally(Action<bool, Exception> actionToContinueWith, TaskAffinity affinity = TaskAffinity.Concurrent)
269277
{
270278
Guard.ArgumentNotNull(actionToContinueWith, nameof(actionToContinueWith));
271-
return Finally(new ActionTask(Token, actionToContinueWith) { Affinity = affinity, Name = "Finally" });
279+
return Then(new ActionTask(Token, (s, ex) =>
280+
{
281+
actionToContinueWith(s, ex);
282+
if (!s)
283+
throw ex;
284+
})
285+
{ Affinity = affinity, Name = "Finally" }, TaskRunOptions.OnAlways)
286+
.CatchInternal(_ => true);
272287
}
273288

274289
/// <summary>
@@ -438,13 +453,15 @@ protected virtual void RaiseOnStart()
438453

439454
protected virtual bool RaiseFaultHandlers(Exception ex)
440455
{
456+
exception = ex is AggregateException ? ex.GetBaseException() : ex;
457+
Errors = exception.Message;
441458
taskFailed = true;
442-
exception = ex;
443459
if (catchHandler == null)
444460
return false;
461+
var args = new object[] { exception };
445462
foreach (var handler in catchHandler.GetInvocationList())
446463
{
447-
if ((bool)handler.DynamicInvoke(new object[] { ex }))
464+
if ((bool)handler.DynamicInvoke(args))
448465
{
449466
exceptionWasHandled = true;
450467
break;
@@ -497,15 +514,14 @@ protected virtual void CallFinallyHandler()
497514

498515
protected Exception GetThrownException()
499516
{
500-
if (DependsOn == null)
501-
return null;
502-
503-
if (DependsOn.Task.Status == TaskStatus.Faulted)
517+
var depends = DependsOn;
518+
while (depends != null)
504519
{
505-
var ex = DependsOn.Task.Exception;
506-
return ex?.InnerException ?? ex;
520+
if (depends.taskFailed)
521+
return depends.exception;
522+
depends = depends.DependsOn;
507523
}
508-
return DependsOn.GetThrownException();
524+
return null;
509525
}
510526

511527
public void UpdateProgress(long value, long total, string message = null)
@@ -583,7 +599,6 @@ protected void Initialize(Task<TResult> task)
583599
}
584600
catch (Exception ex)
585601
{
586-
Errors = ex.Message;
587602
if (!RaiseFaultHandlers(ex))
588603
throw;
589604
Token.ThrowIfCancellationRequested();
@@ -635,7 +650,7 @@ public override T Then<T>(T continuation, TaskRunOptions runOptions = TaskRunOpt
635650
public new ITask<TResult> Catch(Func<Exception, bool> handler)
636651
{
637652
Guard.ArgumentNotNull(handler, "handler");
638-
catchHandler += handler;
653+
CatchInternal(handler);
639654
DependsOn?.Catch(handler);
640655
return this;
641656
}
@@ -667,7 +682,14 @@ public ITask<TResult> Finally(Func<bool, Exception, TResult, TResult> continuati
667682
public ITask Finally(Action<bool, Exception, TResult> continuation, TaskAffinity affinity = TaskAffinity.Concurrent)
668683
{
669684
Guard.ArgumentNotNull(continuation, "continuation");
670-
return Then(new ActionTask<TResult>(Token, continuation) { Affinity = affinity, Name = "Finally" }, TaskRunOptions.OnAlways);
685+
return Then(new ActionTask<TResult>(Token, (s, ex, res) =>
686+
{
687+
continuation(s, ex, res);
688+
if (!s)
689+
throw ex;
690+
})
691+
{ Affinity = affinity, Name = "Finally" }, TaskRunOptions.OnAlways)
692+
.CatchInternal(_ => true);
671693
}
672694

673695
public new ITask<TResult> Start()

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/ProjectWindowInterface.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,9 @@ private static ITask CreateLockObjectTask(Object selected)
146146
NPath assetPath = AssetDatabase.GetAssetPath(selected.GetInstanceID()).ToNPath();
147147
NPath repositoryPath = manager.Environment.GetRepositoryPath(assetPath);
148148

149-
return Repository.RequestLock(repositoryPath)
150-
.FinallyInUI((success, ex) =>
151-
{
152-
if (success)
153-
{
154-
manager.TaskManager.Run(manager.UsageTracker.IncrementUnityProjectViewContextLfsLock, null);
155-
}
156-
});
149+
var task = Repository.RequestLock(repositoryPath);
150+
task.OnEnd += (_, s, ___) => { if (s) manager.TaskManager.Run(manager.UsageTracker.IncrementUnityProjectViewContextLfsLock, null); };
151+
return task;
157152
}
158153

159154
[MenuItem(AssetsMenuReleaseLock, true, 1000)]

src/tests/TaskSystemIntegrationTests/Tests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ public async Task RunningDifferentTasksDependingOnPreviousResult()
915915
var callOrder = new List<string>();
916916

917917
var taskEnd = new ActionTask(Token, () => callOrder.Add("chain completed")) { Name = "Chain Completed" };
918-
var final = taskEnd.Finally((_, __) => { }, TaskAffinity.Concurrent);
918+
var final = taskEnd.Finally((_, __) => { }, TaskAffinity.Exclusive);
919919

920920
var taskStart = new FuncTask<bool>(Token, _ =>
921921
{
@@ -942,12 +942,13 @@ public async Task RunningDifferentTasksDependingOnPreviousResult()
942942

943943
await final.StartAndSwallowException();
944944

945-
CollectionAssert.AreEqual(new string[] {
945+
946+
Assert.AreEqual(new string[] {
946947
"chain start",
947948
"failing",
948949
"on failure",
949950
"chain completed"
950-
}, callOrder);
951+
}.Join(","), callOrder.Join(","));
951952
}
952953

953954
private T LogAndReturnResult<T>(List<string> callOrder, string msg, T result)

0 commit comments

Comments
 (0)