question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using CopyToAsync when copying stream from GetObjectAsync

See original GitHub issue

Hello.

I want to follow best practices and use async when copying from the Stream to MemoryStream in the below code block, but the following will occasionally hard crash my application with the following error:

Unhandled Exception: System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request. ---> System.Net.Sockets.SocketException: The I/O operation has been aborted because of either a thread exit or an application request
   --- End of inner exception stack trace ---
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
   at System.Net.Http.HttpConnection.FillAsync()
   at System.Net.Http.HttpConnection.CopyToExactLengthAsync(Stream destination, UInt64 length, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.ContentLengthReadStream.CompleteCopyToAsync(Task copyTask, CancellationToken cancellationToken)
   at Hyperview.Manager.AssetUtils.FileStorage.MinioService.<>c__DisplayClass3_0.<<GetImageByteArrayAsync>b__0>d.MoveNext() in C:\Projects\manager\src\AssetUtils\FileStorage\MinioService.cs:line 26
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

Unhandled Exception: System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request. ---> System.Net.Sockets.SocketException: The I/O operation has been aborted because of either a thread exit or an application request
   --- End of inner exception stack trace ---
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
   at System.Net.Http.HttpConnection.FillAsync()
   at System.Net.Http.HttpConnection.CopyToExactLengthAsync(Stream destination, UInt64 length, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.ContentLengthReadStream.CompleteCopyToAsync(Task copyTask, CancellationToken cancellationToken)
   at Hyperview.Manager.AssetUtils.FileStorage.MinioService.<>c__DisplayClass3_0.<<GetImageByteArrayAsync>b__0>d.MoveNext() in C:\Projects\manager\src\AssetUtils\FileStorage\MinioService.cs:line 26
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

My ideal solution.

try {
	await minioObjectOperationsClient.GetObjectAsync(bucketName, imageId.ToString(), async (stream) => {
			using (var responseStream = new MemoryStream()) {
				await stream.CopyToAsync(responseStream).ConfigureAwait(false);
				image = responseStream.ToArray();
			}
		}).ConfigureAwait(false);

		return image;
	} catch (Exception exception) {
		throw new Exception($"GetImageByteArrayAsync OnError. ObjectId: {imageId}, Bucket: {bucketName}", exception);
	}

What I’m doing right now.

try {
	await minioObjectOperationsClient.GetObjectAsync(bucketName, imageId.ToString(), (stream) => {
		using (var responseStream = new MemoryStream()) {
			stream.CopyTo(responseStream);
			image = responseStream.ToArray();
		}
	}).ConfigureAwait(false);

	return image;
} catch (Exception exception) {
	throw new Exception($"GetImageByteArrayAsync OnError. ObjectId: {imageId}, Bucket: {bucketName}", exception);
}

I noticed also that none of the example code used CopyToAsync and that perhaps this is required.

Please let me know how best to proceed.

Thank you.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
damikuncommented, Jun 22, 2020

Hi im geting now this error any way how to deal wit it ?

This is my API for small files works ok for bigger i get exeption with non-negative blablabla…

image

Its working only if i set options.AllowSynchronousIO = true; and change code from copyAsyncto to CopyTo…

image

0reactions
kannappanrcommented, Oct 1, 2019

@cklogs Closing this issue based on the above comment. Please feel free to reach out to us if you have further questions. You can also reach out to us at https://slack.min.io.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No of bytes copied from a file to a stream is null
Your problem is right here: getObjectResponse.Result.ResponseStream.CopyToAsync(ms);. Who is waiting for that Task to finish?
Read more >
Stream.CopyToAsync Method (System.IO)
Asynchronously reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of...
Read more >
Random-Access (Seekable) Streams for Amazon S3 in C# ...
using var response = await s3.GetObjectAsync(BUCKET, KEY); var ms = new MemoryStream(); await response.ResponseStream.CopyToAsync(ms);
Read more >
Upload/Download/Delete Files From AWS S3 Using .NET ...
In this article, you will learn about how to upload, download and ... GetObjectAsync method is used to retrieve objects from Amazon S3....
Read more >
C# API to Zip S3 files - Codes In .Net
GetObjectAsync (request). In our original version we were copying this into memory, this looks like it was unnessary. The final version. The file ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found