dotnet / dotnet/Docker.DotNet

Exception when simultaneous read/write to stdin/stdout

Open
#448 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2.4k
Forks
416
PR merge metrics
No merged PRs in 30d

Description

**Output of `dotnet --info`:**

```
.NET Core SDK (reflecting any global.json):
Version: 3.1.101
Commit: b377529961

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.101\

Host (useful for support):
Version: 3.1.1
Commit: a1388f194c

.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.504 [C:\Program Files\dotnet\sdk]
2.1.509 [C:\Program Files\dotnet\sdk]
2.1.802 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]
3.1.100 [C:\Program Files\dotnet\sdk]
3.1.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
```

**What version of Docker.DotNet?:**

```
Tested with latest master and v3.125.2
```

**Steps to reproduce the issue:**

```c#
var client = new DockerClientConfiguration(new Uri("https://docker-server:2376")).CreateClient();

// Create Container
var response = await client.Containers.CreateContainerAsync(new CreateContainerParameters
{
Image = "jbarlow83/ocrmypdf:latest",
AttachStdin = true,
AttachStdout = true,
AttachStderr = false,
StdinOnce = true,
OpenStdin = true,
HostConfig = new HostConfig
{
AutoRemove = true,
CPUCount = 1
},
Cmd = new string[] { "--redo-ocr", "-", "-" }
});

var inputFile = @"C:\temp\test.pdf";
var outputFile = @"C:\temp\test-output.pdf";

var attachParams = new ContainerAttachParameters
{
Stderr = false,
Stdin = true,
Stdout = true,
Stream = true
};

// Attach Container
using (var stream = await client.Containers.AttachContainerAsync(response.ID, false, attachParams))
{
var inputBytes = File.ReadAllBytes(inputFile);

// Start Container
await client.Containers.StartContainerAsync(response.ID, new ContainerStartParameters());

var outputStream = new MemoryStream();

// Write bytes to STDIN
var taskWrite = Task.Run(async () =>
{
await stream.WriteAsync(inputBytes, 0, inputBytes.Length, CancellationToken.None);
});

// Read bytes from STDOUT to MemoryStream
var taskRead = Task.Run(async () =>
{
// Copied from MultiplexedStream.CopyOutputToAsync method:
var buffer = new byte[81920];

for (; ; )
{
// Exception is thrown here after "await WaitContainerAsync" below:
var result = await stream.ReadOutputAsync(buffer, 0, buffer.Length, default(CancellationToken)).ConfigureAwait(false);

if (result.EOF)
{
return;
}

await outputStream.WriteAsync(buffer, 0, result.Count, default(CancellationToken)).ConfigureAwait(false);
}
// End CopyOutputToAsync
});

await taskWrite;
stream.CloseWrite();
await client.Containers.WaitContainerAsync(response.ID);
await taskRead;

// Write MemoryStream to file
if (File.Exists(outputFile))
File.Delete(outputFile);

outputStream.Position = 0;
using (var filestream = File.Create(outputFile))
{
await outputStream.CopyToAsync(filestream);
}
```

**What actually happened?:**

When I provide a large(ish) input, the following exception will occur on `ReadOutputAsync`

```
System.IO.IOException: 'Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.'

at System.Net.Security._SslStream.EndRead(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncTrimPromise`1.Complete(TInstance thisRef, Func`3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Docker.DotNet.MultiplexedStream.d__12.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
```

**What did you expect to happen?:**

I expected the read to succeed, like it does with smaller files.

**Additional information:**

This docker instance takes a PDF file through STDIN and produces another PDF file (OCR'ed) to STDOUT.

This docker client command is exactly what I'm trying to replicate with Docker.DotNet but am unable to (with LARGER PDFs, small files work fine):

```
type c:\temp\test.pdf | docker --tlsverify -H "docker-server:2376" run -i -a stdin -a stdout --cpus 1 jbarlow83/ocrmypdf --redo-ocr - - > c:\temp\test-output.pdf
```

I've used various combinations of the code from issue #223 but he seemed to solve his issue (which was different from mine).

I also thought the Pull-Request from Issue #388 might be helpful, but when I use `Peek` I get an exception "_stream isn't a peekable stream"

This works without an exception with an 8mb test file, but another 20mb test file fails with this exception. Both files work fine from the docker CLI.

Basically, I'm wondering if I'm doing something wrong (and if so, how can I fix it?) - or is this an issue with Docker.DotNet? Thank you!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.