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

Commit 711190b

Browse files
committed
Fix continuing when something fails
1 parent c9436c2 commit 711190b

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

src/GitHub.Api/Tasks/TaskBase.cs

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -382,49 +382,64 @@ protected virtual void RaiseOnStart()
382382
OnStart?.Invoke(this);
383383
}
384384

385+
protected virtual bool RaiseFaultHandlers(Exception ex)
386+
{
387+
taskFailed = true;
388+
exception = ex;
389+
if (catchHandler == null)
390+
return false;
391+
foreach (var handler in catchHandler.GetInvocationList())
392+
{
393+
if ((bool)handler.DynamicInvoke(new object[] { ex }))
394+
{
395+
exceptionWasHandled = true;
396+
break;
397+
}
398+
}
399+
// if a catch handler returned true, don't throw
400+
return exceptionWasHandled;
401+
}
402+
385403
protected virtual void RaiseOnEnd()
386404
{
387405
OnEnd?.Invoke(this, !taskFailed, exception);
406+
SetupContinuations();
407+
//Logger.Trace($"Finished {ToString()}");
408+
}
409+
410+
protected void SetupContinuations()
411+
{
388412
if (!taskFailed || exceptionWasHandled)
389413
{
390-
if (continuationOnSuccess == null)
391-
CallFinallyHandler();
414+
var taskToContinueWith = continuationOnSuccess ?? continuationOnAlways;
415+
if (taskToContinueWith != null)
416+
SetContinuation(taskToContinueWith, runOnSuccessOptions);
392417
else
393-
SetContinuation(continuationOnSuccess, runOnSuccessOptions);
418+
{ // there are no more tasks to schedule, call a finally handler if it exists
419+
// we need to do this only when there are no more continuations
420+
// so that the in-thread finally handler is guaranteed to run after any Finally tasks
421+
CallFinallyHandler();
422+
}
394423
}
395424
else
396425
{
397-
if (continuationOnFailure == null)
398-
CallFinallyHandler();
426+
var taskToContinueWith = continuationOnFailure ?? continuationOnAlways;
427+
if (taskToContinueWith != null)
428+
SetContinuation(taskToContinueWith, runOnFaultOptions);
399429
else
400-
SetContinuation(continuationOnFailure, runOnSuccessOptions);
430+
{ // there are no more tasks to schedule, call a finally handler if it exists
431+
// we need to do this only when there are no more continuations
432+
// so that the in-thread finally handler is guaranteed to run after any Finally tasks
433+
CallFinallyHandler();
434+
}
401435
}
402-
//Logger.Trace($"Finished {ToString()}");
403436
}
404437

405-
protected void CallFinallyHandler()
438+
protected virtual void CallFinallyHandler()
406439
{
407440
finallyHandler?.Invoke(!taskFailed);
408441
}
409442

410-
protected virtual bool RaiseFaultHandlers(Exception ex)
411-
{
412-
taskFailed = true;
413-
exception = ex;
414-
if (catchHandler == null)
415-
return continuationOnFailure != null;
416-
foreach (var handler in catchHandler.GetInvocationList())
417-
{
418-
if ((bool)handler.DynamicInvoke(new object[] { ex }))
419-
{
420-
exceptionWasHandled = true;
421-
break;
422-
}
423-
}
424-
// if a catch handler returned true or we have a continuation for failure cases, don't throw
425-
return exceptionWasHandled || continuationOnFailure != null;
426-
}
427-
428443
protected Exception GetThrownException()
429444
{
430445
if (DependsOn == null)
@@ -617,16 +632,15 @@ protected virtual void RaiseOnEnd(TResult data)
617632
{
618633
this.result = data;
619634
OnEnd?.Invoke(this, result, !taskFailed, exception);
620-
if (continuationOnSuccess == null && continuationOnFailure == null)
621-
{
622-
finallyHandler?.Invoke(!taskFailed, result);
623-
CallFinallyHandler();
624-
}
625-
else if (continuationOnSuccess != null)
626-
SetContinuation(continuationOnSuccess, runOnSuccessOptions);
635+
SetupContinuations();
627636
//Logger.Trace($"Finished {ToString()} {result}");
628637
}
629638

639+
protected override void CallFinallyHandler()
640+
{
641+
finallyHandler?.Invoke(!taskFailed, result);
642+
}
643+
630644
public new Task<TResult> Task
631645
{
632646
get { return base.Task as Task<TResult>; }

src/tests/TaskSystemIntegrationTests/Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ public async Task RunningDifferentTasksDependingOnPreviousResult()
850850
var callOrder = new List<string>();
851851

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

855855
var taskStart = new FuncTask<bool>(Token, _ =>
856856
{

0 commit comments

Comments
 (0)