Skip to content

Commit 9f18f36

Browse files
committed
Response stream corruption when a request is cancelled
1 parent 8e3cf4f commit 9f18f36

1 file changed

Lines changed: 16 additions & 22 deletions

File tree

src/Docker.DotNet/Endpoints/StreamUtil.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ internal static async Task MonitorStreamAsync(Task<Stream> streamTask, DockerCli
1717
using (var stream = await streamTask)
1818
{
1919
// ReadLineAsync must be cancelled by closing the whole stream.
20-
using (cancel.Register(() => stream.Dispose()))
20+
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
2121
{
22-
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
22+
string line;
23+
while ((line = await reader.ReadLineAsync()) != null && !cancel.IsCancellationRequested)
2324
{
24-
string line;
25-
while ((line = await reader.ReadLineAsync()) != null)
26-
{
27-
progress.Report(line);
28-
}
25+
progress.Report(line);
2926
}
3027
}
3128
}
@@ -54,27 +51,24 @@ internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseM
5451
using (var stream = await response.Content.ReadAsStreamAsync())
5552
{
5653
// ReadLineAsync must be cancelled by closing the whole stream.
57-
using (cancel.Register(() => stream.Dispose()))
54+
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
5855
{
59-
using (var reader = new StreamReader(stream, new UTF8Encoding(false)))
56+
string line;
57+
try
6058
{
61-
string line;
62-
try
59+
while ((line = await reader.ReadLineAsync()) != null && !cancel.IsCancellationRequested)
6360
{
64-
while ((line = await reader.ReadLineAsync()) != null)
65-
{
66-
var prog = client.JsonSerializer.DeserializeObject<T>(line);
67-
if (prog == null) continue;
61+
var prog = client.JsonSerializer.DeserializeObject<T>(line);
62+
if (prog == null) continue;
6863

69-
progress.Report(prog);
70-
}
71-
}
72-
catch (ObjectDisposedException)
73-
{
74-
// The subsequent call to reader.ReadLineAsync() after cancellation
75-
// will fail because we disposed the stream. Just ignore here.
64+
progress.Report(prog);
7665
}
7766
}
67+
catch (ObjectDisposedException)
68+
{
69+
// The subsequent call to reader.ReadLineAsync() after cancellation
70+
// will fail because we disposed the stream. Just ignore here.
71+
}
7872
}
7973
}
8074
}

0 commit comments

Comments
 (0)