Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 19 additions & 25 deletions src/Docker.DotNet/Endpoints/StreamUtil.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Newtonsoft.Json;
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
{
Expand All @@ -17,15 +17,12 @@ internal static async Task MonitorStreamAsync(Task<Stream> streamTask, DockerCli
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)))
{
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
string line;
while ((line = await reader.ReadLineAsync()) != null && !cancel.IsCancellationRequested)
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
progress.Report(line);
}
progress.Report(line);
}
}
}
Expand All @@ -49,32 +46,29 @@ internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseM
{
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)))
{
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
string line;
try
{
string line;
try
while ((line = await reader.ReadLineAsync()) != null && !cancel.IsCancellationRequested)
{
while ((line = await reader.ReadLineAsync()) != null)
{
var prog = client.JsonSerializer.DeserializeObject<T>(line);
if (prog == null) continue;
await client.HandleIfErrorResponseAsync(response.StatusCode, response);

progress.Report(prog);
}
}
catch (ObjectDisposedException)
{
// The subsequent call to reader.ReadLineAsync() after cancellation
// will fail because we disposed the stream. Just ignore here.
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.
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/Docker.DotNet.Tests/ISystemOperations.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ public async Task MonitorEventsAsync_Succeeds()

await _client.Images.CreateImageAsync(new ImagesCreateParameters { FromImage = "hello-world" }, null, progressJSONMessage);

var task = Task.Run(() => _client.System.MonitorEventsAsync(new ContainerEventsParameters(), progressMessage, cts.Token));
var task = _client.System.MonitorEventsAsync(new ContainerEventsParameters(), progressMessage, cts.Token);

await _client.Images.TagImageAsync(repository, new ImageTagParameters { RepositoryName = repository, Tag = newTag });

Thread.Sleep(500); // Let's wait some time before cancelling the monitor stream to make sure we got some progress back.
cts.Cancel();

bool taskIsCancelled = false;
Expand Down