Posting large MultipartFormDataContent requests results in Broken Pipe exception

Viewed 36

so i've stumbled over this weird issue when uploading files using HttpClient.

I'm trying to upload files from directory, posting multipart form data content to an API, which is working fine until the request becomes larger than 32MB or so. Then I get following exception.

System.Net.Http.HttpRequestException: Error while copying content to a stream.
 ---> System.IO.IOException: Unable to write data to the transport connection: Broken pipe.
 ---> System.Net.Sockets.SocketException (32): Broken pipe
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.CreateException(SocketError error, Boolean forAsyncThrow)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.SendAsyncForNetworkStream(Socket socket, CancellationToken cancellationToken)
   at System.Net.Sockets.NetworkStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.WriteToStreamAsync(ReadOnlyMemory`1 source, Boolean async)
   at System.Net.Http.HttpConnection.WriteAsync(ReadOnlyMemory`1 source, Boolean async)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at System.Net.Http.HttpConnection.ContentLengthWriteStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken ignored)
   at System.IO.Stream.<CopyToAsync>g__Core|29_0(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.ThreadPoolValueTaskSource.ExecuteInternal()
   at Microsoft.Win32.SafeHandles.SafeFileHandle.ThreadPoolValueTaskSource.System.Threading.IThreadPoolWorkItem.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()

Here is my code:


            // ...

            var streams = new List<Stream>();
            var content = new MultipartFormDataContent();

            foreach (var filePath in Directory.EnumerateFiles(path, "*", SearchOption.TopDirectoryOnly))
            {
                var fileStream = File.OpenRead(filePath);
                streams.Add(fileStream);

                content.Add(new StreamContent(fileStream), "files", Path.GetFileName(fileStream.Name));
            }

            using var response = await _client.PostAsync("...", content, cancellationToken: cancellationToken);

            foreach (var stream in streams)
            {
                await stream.DisposeAsync();
            }

            // ...

What's weird about this is that it only happens when the code is running inside docker container (mcr.microsoft.com/dotnet/aspnet:6.0.8-focal). When I run it directly on my machine everything works fine. Request limits on the API side are much higher than said 32 megabytes.

0 Answers
Related