-
Notifications
You must be signed in to change notification settings - Fork 402
CancellationToken not cancelling #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
galvesribeiro
merged 1 commit into
dotnet:master
from
dgvives:bugfix/cancellation-token-not-cancelling
Mar 5, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,97 +1,55 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Docker.DotNet.Models | ||
| { | ||
| internal static class StreamUtil | ||
| { | ||
| private static Newtonsoft.Json.JsonSerializer _serializer = new Newtonsoft.Json.JsonSerializer(); | ||
|
|
||
| internal static async Task MonitorStreamAsync(Task<Stream> streamTask, DockerClient client, CancellationToken cancel, IProgress<string> progress) | ||
| { | ||
| using (var stream = await streamTask) | ||
| { | ||
| // ReadLineAsync must be cancelled by closing the whole stream. | ||
| using (cancel.Register(() => stream.Dispose())) | ||
| { | ||
| using (var reader = new StreamReader(stream, new UTF8Encoding(false))) | ||
| { | ||
| string line; | ||
| while ((line = await reader.ReadLineAsync()) != null) | ||
| { | ||
| progress.Report(line); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal static async Task MonitorStreamForMessagesAsync<T>(Task<Stream> streamTask, DockerClient client, CancellationToken cancel, IProgress<T> progress) | ||
| { | ||
| using (var stream = await streamTask) | ||
| using (var reader = new StreamReader(stream, new UTF8Encoding(false))) | ||
| using (var jsonReader = new JsonTextReader(reader) { SupportMultipleContent = true }) | ||
| { | ||
| while (await jsonReader.ReadAsync().WithCancellation(cancel)) | ||
| { | ||
| var ev = _serializer.Deserialize<T>(jsonReader); | ||
| progress?.Report(ev); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseMessage> responseTask, DockerClient client, CancellationToken cancel, IProgress<T> progress) | ||
| { | ||
| using (var response = await responseTask) | ||
| { | ||
| await client.HandleIfErrorResponseAsync(response.StatusCode, response); | ||
|
|
||
| using (var stream = await response.Content.ReadAsStreamAsync()) | ||
| { | ||
| // ReadLineAsync must be cancelled by closing the whole stream. | ||
| using (cancel.Register(() => stream.Dispose())) | ||
| { | ||
| using (var reader = new StreamReader(stream, new UTF8Encoding(false))) | ||
| { | ||
| string line; | ||
| try | ||
| { | ||
| while ((line = await reader.ReadLineAsync()) != null) | ||
| { | ||
| var prog = client.JsonSerializer.DeserializeObject<T>(line); | ||
| if (prog == null) continue; | ||
|
|
||
| progress.Report(prog); | ||
| } | ||
| } | ||
| catch (ObjectDisposedException) | ||
| { | ||
| // The subsequent call to reader.ReadLineAsync() after cancellation | ||
| // will fail because we disposed the stream. Just ignore here. | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken) | ||
| { | ||
| var tcs = new TaskCompletionSource<bool>(); | ||
| using (cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs)) | ||
| { | ||
| if (task != await Task.WhenAny(task, tcs.Task)) | ||
| { | ||
| throw new OperationCanceledException(cancellationToken); | ||
| } | ||
| } | ||
|
|
||
| return await task; | ||
| } | ||
| } | ||
| } | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Net.Http; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Docker.DotNet.Models | ||
| { | ||
| internal static class StreamUtil | ||
| { | ||
| internal static async Task MonitorStreamAsync(Task<Stream> streamTask, DockerClient client, CancellationToken cancellationToken, IProgress<string> progress) | ||
| { | ||
| var tcs = new TaskCompletionSource<string>(); | ||
|
|
||
| using (var stream = await streamTask) | ||
| using (var reader = new StreamReader(stream, new UTF8Encoding(false))) | ||
| using (cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken))) | ||
| { | ||
| string line; | ||
| while ((line = await await Task.WhenAny(reader.ReadLineAsync(), tcs.Task)) != null) | ||
| { | ||
| progress.Report(line); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal static async Task MonitorStreamForMessagesAsync<T>(Task<Stream> streamTask, DockerClient client, CancellationToken cancellationToken, IProgress<T> progress) | ||
| { | ||
| var tcs = new TaskCompletionSource<bool>(); | ||
|
|
||
| using (var stream = await streamTask) | ||
| using (var reader = new StreamReader(stream, new UTF8Encoding(false))) | ||
| using (var jsonReader = new JsonTextReader(reader) { SupportMultipleContent = true }) | ||
| using (cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken))) | ||
| { | ||
| while (await await Task.WhenAny(jsonReader.ReadAsync(cancellationToken), tcs.Task)) | ||
| { | ||
| var ev = await client.JsonSerializer.Deserialize<T>(jsonReader, cancellationToken); | ||
| progress.Report(ev); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseMessage> responseTask, DockerClient client, CancellationToken cancel, IProgress<T> progress) | ||
| { | ||
| using (var response = await responseTask) | ||
| { | ||
| await MonitorStreamForMessagesAsync<T>(response.Content.ReadAsStreamAsync(), client, cancel, progress); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to move the timeout complexity to a separate function? Then this whole method can be:
A RunWithTimeoutAsync implementation might look like this:
For added performance you could use a state parameter to avoid the closure. As a bonus, timeouts emit a separate exception than cancellation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Emdot great suggestion.
I'll wait for this to get approved and continue with improvements.