Skip to content

Commit d7372c0

Browse files
committed
Remove the WithCancellation
Check cancellation before action
1 parent 9f18f36 commit d7372c0

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

src/Docker.DotNet/Endpoints/StreamUtil.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
using Newtonsoft.Json;
12
using System;
23
using System.IO;
34
using System.Net.Http;
45
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
7-
using Newtonsoft.Json;
88

99
namespace Docker.DotNet.Models
1010
{
@@ -20,7 +20,7 @@ internal static async Task MonitorStreamAsync(Task<Stream> streamTask, DockerCli
2020
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
2121
{
2222
string line;
23-
while ((line = await reader.ReadLineAsync()) != null && !cancel.IsCancellationRequested)
23+
while (!cancel.IsCancellationRequested && (line = await reader.ReadLineAsync()) != null)
2424
{
2525
progress.Report(line);
2626
}
@@ -34,7 +34,7 @@ internal static async Task MonitorStreamForMessagesAsync<T>(Task<Stream> streamT
3434
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
3535
using (var jsonReader = new JsonTextReader(reader) { SupportMultipleContent = true })
3636
{
37-
while (await jsonReader.ReadAsync().WithCancellation(cancel))
37+
while (!cancel.IsCancellationRequested && await jsonReader.ReadAsync())
3838
{
3939
var ev = _serializer.Deserialize<T>(jsonReader);
4040
progress?.Report(ev);
@@ -56,7 +56,7 @@ internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseM
5656
string line;
5757
try
5858
{
59-
while ((line = await reader.ReadLineAsync()) != null && !cancel.IsCancellationRequested)
59+
while (!cancel.IsCancellationRequested && (line = await reader.ReadLineAsync()) != null)
6060
{
6161
var prog = client.JsonSerializer.DeserializeObject<T>(line);
6262
if (prog == null) continue;
@@ -73,19 +73,5 @@ internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseM
7373
}
7474
}
7575
}
76-
77-
private static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
78-
{
79-
var tcs = new TaskCompletionSource<bool>();
80-
using (cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs))
81-
{
82-
if (task != await Task.WhenAny(task, tcs.Task))
83-
{
84-
throw new OperationCanceledException(cancellationToken);
85-
}
86-
}
87-
88-
return await task;
89-
}
9076
}
9177
}

0 commit comments

Comments
 (0)