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

Commit d954f0f

Browse files
Merge pull request #808 from github-for-unity/fixes/shutdown
Make sure we shutdown promptly
2 parents b88a47e + 3f55d66 commit d954f0f

File tree

4 files changed

+65
-20
lines changed

4 files changed

+65
-20
lines changed

src/GitHub.Api/Managers/Downloader.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,17 @@ public static bool Download(ILogging logger, UriString url,
151151
else
152152
logger.Trace($"Downloading {url}");
153153

154+
if (!onProgress(bytes, bytes * 2))
155+
return false;
156+
154157
using (var webResponse = (HttpWebResponse)webRequest.GetResponseWithoutException())
155158
{
156159
var httpStatusCode = webResponse.StatusCode;
157160
logger.Trace($"Downloading {url} StatusCode:{(int)webResponse.StatusCode}");
158161

159162
if (expectingResume && httpStatusCode == HttpStatusCode.RequestedRangeNotSatisfiable)
160163
{
161-
onProgress(bytes, bytes);
162-
return true;
164+
return !onProgress(bytes, bytes);
163165
}
164166

165167
if (!(httpStatusCode == HttpStatusCode.OK || httpStatusCode == HttpStatusCode.PartialContent))

src/GitHub.Api/Tasks/ActionTask.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,24 @@ public ActionTask(CancellationToken token, Action<bool, T> action, Func<T> getPr
8383
{
8484
Guard.ArgumentNotNull(action, "action");
8585
this.Callback = action;
86-
Task = new Task(() => Run(DependsOn?.Successful ?? true,
86+
Task = new Task(() =>
87+
{
88+
Token.ThrowIfCancellationRequested();
89+
var previousIsSuccessful = previousSuccess.HasValue ? previousSuccess.Value : (DependsOn?.Successful ?? true);
90+
8791
// if this task depends on another task and the dependent task was successful, use the value of that other task as input to this task
8892
// otherwise if there's a method to retrieve the value, call that
8993
// otherwise use the PreviousResult property
90-
(DependsOn?.Successful ?? false) ? ((ITask<T>)DependsOn).Result : getPreviousResult != null ? getPreviousResult() : PreviousResult),
91-
Token, TaskCreationOptions.None);
94+
T prevResult = PreviousResult;
95+
if (previousIsSuccessful && DependsOn != null && DependsOn is ITask<T>)
96+
prevResult = ((ITask<T>)DependsOn).Result;
97+
else if (getPreviousResult != null)
98+
prevResult = getPreviousResult();
99+
100+
Run(previousIsSuccessful, prevResult);
101+
102+
}, Token, TaskCreationOptions.None);
103+
92104
Name = $"ActionTask<{typeof(T)}>";
93105
}
94106

@@ -103,12 +115,23 @@ public ActionTask(CancellationToken token, Action<bool, Exception, T> action, Fu
103115
{
104116
Guard.ArgumentNotNull(action, "action");
105117
this.CallbackWithException = action;
106-
Task = new Task(() => Run(DependsOn?.Successful ?? true,
118+
Task = new Task(() =>
119+
{
120+
Token.ThrowIfCancellationRequested();
121+
var previousIsSuccessful = previousSuccess.HasValue ? previousSuccess.Value : (DependsOn?.Successful ?? true);
122+
107123
// if this task depends on another task and the dependent task was successful, use the value of that other task as input to this task
108124
// otherwise if there's a method to retrieve the value, call that
109125
// otherwise use the PreviousResult property
110-
(DependsOn?.Successful ?? false) ? ((ITask<T>)DependsOn).Result : getPreviousResult != null ? getPreviousResult() : PreviousResult),
111-
Token, TaskCreationOptions.None);
126+
T prevResult = PreviousResult;
127+
if (previousIsSuccessful && DependsOn != null && DependsOn is ITask<T>)
128+
prevResult = ((ITask<T>)DependsOn).Result;
129+
else if (getPreviousResult != null)
130+
prevResult = getPreviousResult();
131+
132+
Run(previousIsSuccessful, prevResult);
133+
134+
}, Token, TaskCreationOptions.None);
112135
Name = $"ActionTask<Exception, {typeof(T)}>";
113136
}
114137

@@ -409,5 +432,4 @@ protected override List<TResult> RunWithData(bool success, T previousResult)
409432
return result;
410433
}
411434
}
412-
413435
}

src/GitHub.Api/Tasks/ProcessTask.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public void Run()
113113
{
114114
Logger.Trace($"Running '{Process.StartInfo.FileName} {taskName}'");
115115

116+
token.ThrowIfCancellationRequested();
116117
Process.Start();
117118

118119
if (Process.StartInfo.RedirectStandardInput)

src/GitHub.Api/Tasks/TaskBase.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ protected TaskBase(CancellationToken token)
122122
Token = token;
123123
Task = new Task(() =>
124124
{
125+
Token.ThrowIfCancellationRequested();
125126
var previousIsSuccessful = previousSuccess.HasValue ? previousSuccess.Value : (DependsOn?.Successful ?? true);
126127
Run(previousIsSuccessful);
127128
},
@@ -134,6 +135,8 @@ protected TaskBase(Task task)
134135
{
135136
Task = new Task(t =>
136137
{
138+
Token.ThrowIfCancellationRequested();
139+
137140
var scheduler = TaskManager.GetScheduler(Affinity);
138141
RaiseOnStart();
139142
var tk = ((Task)t);
@@ -142,6 +145,7 @@ protected TaskBase(Task task)
142145
if (tk.Status == TaskStatus.Created && !tk.IsCompleted &&
143146
((tk.CreationOptions & (TaskCreationOptions)512) == TaskCreationOptions.None))
144147
{
148+
Token.ThrowIfCancellationRequested();
145149
tk.RunSynchronously(scheduler);
146150
}
147151
}
@@ -150,6 +154,7 @@ protected TaskBase(Task task)
150154
Errors = ex.Message;
151155
if (!RaiseFaultHandlers(ex))
152156
throw;
157+
Token.ThrowIfCancellationRequested();
153158
}
154159
finally
155160
{
@@ -278,7 +283,12 @@ public T Finally<T>(T taskToContinueWith)
278283
/// <param name="handler"></param>
279284
internal void SetFaultHandler(TaskBase handler)
280285
{
281-
Task.ContinueWith(t => handler.Start(t), Token,
286+
Task.ContinueWith(t =>
287+
{
288+
Token.ThrowIfCancellationRequested();
289+
handler.Start(t);
290+
},
291+
Token,
282292
TaskContinuationOptions.OnlyOnFaulted,
283293
TaskManager.GetScheduler(handler.Affinity));
284294
DependsOn?.SetFaultHandler(handler);
@@ -356,9 +366,15 @@ protected void SetContinuation()
356366

357367
protected void SetContinuation(TaskBase continuation, TaskContinuationOptions runOptions)
358368
{
359-
Task.ContinueWith(_ => ((TaskBase)(object)continuation).Run(), Token,
360-
runOptions,
361-
TaskManager.GetScheduler(continuation.Affinity));
369+
Token.ThrowIfCancellationRequested();
370+
Task.ContinueWith(_ =>
371+
{
372+
Token.ThrowIfCancellationRequested();
373+
((TaskBase)(object)continuation).Run();
374+
},
375+
Token,
376+
runOptions,
377+
TaskManager.GetScheduler(continuation.Affinity));
362378
}
363379

364380
protected ITask SetDependsOn(ITask dependsOn)
@@ -406,6 +422,7 @@ public virtual void Run(bool success)
406422
taskFailed = false;
407423
hasRun = false;
408424
exception = null;
425+
Token.ThrowIfCancellationRequested();
409426
}
410427

411428
protected virtual void RaiseOnStart()
@@ -526,6 +543,7 @@ protected TaskBase(CancellationToken token)
526543
{
527544
Task = new Task<TResult>(() =>
528545
{
546+
Token.ThrowIfCancellationRequested();
529547
var previousIsSuccessful = previousSuccess.HasValue ? previousSuccess.Value : (DependsOn?.Successful ?? true);
530548
var ret = RunWithReturn(previousIsSuccessful);
531549
tcs.SetResult(ret);
@@ -538,6 +556,8 @@ protected TaskBase(Task<TResult> task)
538556
{
539557
Task = new Task<TResult>(t =>
540558
{
559+
Token.ThrowIfCancellationRequested();
560+
541561
TResult ret = default(TResult);
542562
RaiseOnStart();
543563
var tk = ((Task<TResult>)t);
@@ -546,6 +566,7 @@ protected TaskBase(Task<TResult> task)
546566
if (tk.Status == TaskStatus.Created && !tk.IsCompleted &&
547567
((tk.CreationOptions & (TaskCreationOptions)512) == TaskCreationOptions.None))
548568
{
569+
Token.ThrowIfCancellationRequested();
549570
tk.RunSynchronously();
550571
}
551572
ret = tk.Result;
@@ -555,6 +576,7 @@ protected TaskBase(Task<TResult> task)
555576
Errors = ex.Message;
556577
if (!RaiseFaultHandlers(ex))
557578
throw;
579+
Token.ThrowIfCancellationRequested();
558580
}
559581
finally
560582
{
@@ -626,8 +648,7 @@ public ITask<TResult> Finally(Action<bool, TResult> handler)
626648
public ITask<TResult> Finally(Func<bool, Exception, TResult, TResult> continuation, TaskAffinity affinity = TaskAffinity.Concurrent)
627649
{
628650
Guard.ArgumentNotNull(continuation, "continuation");
629-
var ret = Then(new FuncTask<TResult, TResult>(Token, continuation) { Affinity = affinity, Name = "Finally" }, TaskRunOptions.OnAlways);
630-
return ret;
651+
return Then(new FuncTask<TResult, TResult>(Token, continuation) { Affinity = affinity, Name = "Finally" }, TaskRunOptions.OnAlways);
631652
}
632653

633654
/// <summary>
@@ -636,8 +657,7 @@ public ITask<TResult> Finally(Func<bool, Exception, TResult, TResult> continuati
636657
public ITask Finally(Action<bool, Exception, TResult> continuation, TaskAffinity affinity = TaskAffinity.Concurrent)
637658
{
638659
Guard.ArgumentNotNull(continuation, "continuation");
639-
var ret = Then(new ActionTask<TResult>(Token, continuation) { Affinity = affinity, Name = "Finally" }, TaskRunOptions.OnAlways);
640-
return ret;
660+
return Then(new ActionTask<TResult>(Token, continuation) { Affinity = affinity, Name = "Finally" }, TaskRunOptions.OnAlways);
641661
}
642662

643663
public new ITask<TResult> Start()
@@ -704,13 +724,13 @@ public TaskBase(CancellationToken token)
704724
{
705725
Task = new Task<TResult>(() =>
706726
{
727+
Token.ThrowIfCancellationRequested();
707728
var previousIsSuccessful = previousSuccess.HasValue ? previousSuccess.Value : (DependsOn?.Successful ?? true);
708729
T prevResult = previousIsSuccessful && DependsOn != null && DependsOn is ITask<T> ? ((ITask<T>)DependsOn).Result : default(T);
709730
var ret = RunWithData(previousIsSuccessful, prevResult);
710731
tcs.SetResult(ret);
711732
return ret;
712-
},
713-
Token, TaskCreationOptions.None);
733+
}, Token, TaskCreationOptions.None);
714734
}
715735

716736
public TaskBase(Task<TResult> task)
@@ -764,4 +784,4 @@ public enum TaskAffinity
764784
Exclusive,
765785
UI
766786
}
767-
}
787+
}

0 commit comments

Comments
 (0)