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

Commit 79af1d0

Browse files
committed
Make sure fault handlers are set correctly
1 parent c1558a5 commit 79af1d0

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/GitHub.Api/Tasks/TaskBase.cs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,36 +127,37 @@ public virtual T Then<T>(T nextTask, TaskRunOptions runOptions = TaskRunOptions.
127127
where T : ITask
128128
{
129129
Guard.ArgumentNotNull(nextTask, nameof(nextTask));
130-
var taskBase = ((TaskBase)(object)nextTask);
130+
var nextTaskBase = ((TaskBase)(object)nextTask);
131131

132-
// find the first task of the continuation chain being appended to this task
133-
var firstTaskBase = taskBase.GetTopMostTask() ?? taskBase;
134-
// set this task as a dependency of the first task of the continuation chain
135-
firstTaskBase.SetDependsOn(this);
132+
// find the task at the top of the chain
133+
nextTaskBase = nextTaskBase.GetTopMostTask() ?? nextTaskBase;
134+
// make the next task dependent on this one so it can get values from us
135+
nextTaskBase.SetDependsOn(this);
136136

137137
if (runOptions == TaskRunOptions.OnSuccess)
138138
{
139-
this.continuationOnSuccess = firstTaskBase;
139+
this.continuationOnSuccess = nextTaskBase;
140+
140141
// if there are fault handlers in the chain we're appending, propagate them
141142
// up this chain as well
142-
if (firstTaskBase.continuationOnFailure != null)
143-
SetFaultHandler(firstTaskBase.continuationOnFailure);
144-
else if (firstTaskBase.continuationOnAlways != null)
145-
SetFaultHandler(firstTaskBase.continuationOnAlways);
146-
if (firstTaskBase.catchHandler != null)
147-
Catch(firstTaskBase.catchHandler);
148-
if (firstTaskBase.finallyHandler != null)
149-
Finally(firstTaskBase.finallyHandler);
143+
if (nextTaskBase.continuationOnFailure != null)
144+
SetFaultHandler(nextTaskBase.continuationOnFailure);
145+
else if (nextTaskBase.continuationOnAlways != null)
146+
SetFaultHandler(nextTaskBase.continuationOnAlways);
147+
if (nextTaskBase.catchHandler != null)
148+
Catch(nextTaskBase.catchHandler);
149+
if (nextTaskBase.finallyHandler != null)
150+
Finally(nextTaskBase.finallyHandler);
150151
}
151152
else if (runOptions == TaskRunOptions.OnFailure)
152153
{
153-
this.continuationOnFailure = firstTaskBase;
154-
DependsOn?.SetFaultHandler(firstTaskBase);
154+
this.continuationOnFailure = nextTaskBase;
155+
DependsOn?.SetFaultHandler(nextTaskBase);
155156
}
156157
else
157158
{
158-
this.continuationOnAlways = firstTaskBase;
159-
DependsOn?.SetFaultHandler(firstTaskBase);
159+
this.continuationOnAlways = nextTaskBase;
160+
DependsOn?.SetFaultHandler(nextTaskBase);
160161
}
161162
return nextTask;
162163
}
@@ -214,9 +215,12 @@ public ITask Finally<T>(T taskToContinueWith)
214215

215216
internal void SetFaultHandler(TaskBase handler)
216217
{
217-
Task.ContinueWith(t => handler.Start(t), Token,
218-
TaskContinuationOptions.OnlyOnFaulted,
219-
TaskManager.GetScheduler(handler.Affinity));
218+
if (Task.Status == TaskStatus.Created)
219+
this.continuationOnFailure = handler;
220+
else
221+
Task.ContinueWith(t => handler.Start(t), Token,
222+
TaskContinuationOptions.OnlyOnFaulted,
223+
TaskManager.GetScheduler(handler.Affinity));
220224
DependsOn?.SetFaultHandler(handler);
221225
}
222226

0 commit comments

Comments
 (0)