aws-sdk-net: GetObjectRequest hangs randomly.

Randomly my application will hang infinity waiting for a response in the Amazon library for AmazonS3Client.GetOject see the stacktrace below, the object is not being reused.

image

here is a sample of the code run in that thread:

using (var amazonS3Client = CreateS3Client())
{
    var s3PartDownloadRequest = new GetObjectRequest
    {
        BucketName = download.Bucket,
        Key = download.ObjectKey,
        ByteRange = new ByteRange(position, end)
    };

    using (var getObjectResponse = amazonS3Client.GetObject(s3PartDownloadRequest)) //hangs on this line
    {    
        getObjectResponse.WriteResponseStreamToFile(dest);
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 47 (11 by maintainers)

Commits related to this issue

Most upvoted comments

In case anyone still has this issue, I’ve found two changes that removed the problem:

  • Before you initialize any of ASW SDK’s class set ServicePointManager.DefaultConnectionLimit = 1000;.
    I set it to 1000, but any big number works. MSDN says the default number is Int32.MaxValue, but in fact it is just 2. The AWS SDK for .Net does use a value of 50, but that might not be enough (I also use SWF, which requires a large number of open connections).
  • Do not share instances of AmazonS3Client between threads. The SDK uses HttpWebRequest, which isn’t thread safe. It isn’t clear if some methods are thread safe, and I don’t think the SDK guarantees this, so I try not to use the same instance in parallel.

Two more comments:

  • I got hanging connections when opening a stream using HttpWebRequest directly to a public S3 URL (public file/static content), without using the SDK at all. This is how I’ve found the two workarounds above.
  • I get more hanging connections at the office than at home.

From our experience concurreny doesn’t really matter, if you don’t dispose, the connection will hang around until the application terminates. We are simply disposing of the object as soon as possible and all the problems have disappeared.

It’s April and 2019 and, years later, we are STILL experiencing random and sporadic hangs when accessing S3 via the .Net AWSSDK with a high degree of parallelism.

We’ve tried disabling SSL. We’ve done multiple audits for leaked IDisposables. We’ve messed with ServicePointManager.DefaultConnectionLimit. The hangs still persist.

Are others still experiencing this issue as well?

I think the key might be to dispose of the GetObjectResponse as quickly as possible. In my program, I’m concurrently downloading the contents of an entire bucket (with 35 concurrent worker tasks). I noticed that I started seeing object requests hang indefinitely if I didn’t immediately read the contents of GetObjectResponse.ResponseStream into memory and dispose of the stream. Fiddling with the DefaultConnectionLimit didn’t seem to offer any improvement, only the quick disposal of the stream made any difference for me.

I’m a bit late to the party but I’d like to give my two cents on this issue.

We’re running .NET 4.7 on windows server 2016. We have multiple worker instances (some of which are running Quartz) and multiple instances running ASP.NET Web API. API instances never displayed any problems regarding S3 requests hanging, however, worker instances did.

At first, I thought that we might have some kind of deadlock because logging in place wasn’t verbose enough to pinpoint the exact location where the application stopped. It was clear that these hangs were happening only when the system was under a considerable load.

Dumping the stack trace pointed me in the wrong direction. It showed that my service blocked on the following snippet executing Exists:

public bool ObjectExists(string bucketName, string objectKey)
        {
            var s3FileInfo = new S3FileInfo(_s3Client, bucketName, objectKey);
            return s3FileInfo.Exists;
        }

I’ve tried everything I could find related to this issue including updating to .NET 4.8, using single instance S3 client, using different S3 clients for every unit of work, changing the existing implementation to use different SDK methods, etc.

Tuning the ServicePointManager.DefaultConnectionLimit = N certainly did have an effect on how soon the service ground to a halt. Using netstat I noticed that there were N connections to S3 with CLOSE_WAIT status. According to TCP specification, this state indicates that the server (S3) received and acknowledged signal for closing the connection but the client (your application) has not yet closed the socket.

This information steered me away from the problem source and into the esoteric search for the bug in the framework (hence the update to a newer version of the framework). While there might be cases where the framework bug caused your implementation to misbehave, that wasn’t my case.

Enter the following comment:

@dyardyGIT Disposing has certainly eliminated this problem for many. #152 (comment) Have you given this a try?

I inspected the facade implementation that we used to wrap S3 related actions and discovered that one of the s3Client.GetObjectAsync(getObjectRequest) objects was not disposed of. That method is called in the normal service flow but it’s kind of buried deeper in the service layer. This caused the number of open connections to grow and eventually lead service to a grinding halt. The interesting part is that because of the way the service was implemented, it would always stop on the same method (ObjectExists shown above) where there was no need for disposal per se.

Disposing of the object properly meant that no connections were left hanging and that the service could handle bigger loads without stopping.

I still don’t understand why the SDK lets this happen in the first place. I would’ve been happier with an exception telling me that the number of connections has been exceeded and that new connections cannot be established.

This may be related to https://github.com/dotnet/corefx/issues/21796 (.NET FX deadlock on sockets cleanup and due to ServicePoint.set_ConnectionLimit internal locks)

I’ve started getting deadlocks due to many S3 SDK GetObject/Metadata/Exists calls and eventually something deadlocks exactly like the above issue.

I’m not sure how to reopen this issue. So adding my problem here.

We have a server side application in .Net developed using Nancyfx framework. Running as selfhost. This application receives multipart request with multiple files(around 100). All these files are supposed to be upload to S3 bucket. Using Parallels to upload files to S3 bucket. Right now creating s3 object for every task. This is causing too many HTTP connections and after a while system is become slow or s3 latency increases. We have optimized at TCP level, to reduce the TcpWaitTimeDelay to 30 seconds.

Can anyone help how to resolve this issue ? How can we reduce AmazonS3Client HTTP connection pool ?

Using Windows 2012 AWS SDK version : 3.3.7

Let me know if any further information require.

Thanks in advance.

I have a related problem, using the asp.net core SDK version 3.2.3-beta. My problem is related to executing GetObjectAsync calls in parallel, but reading through the thread, it might be highly related. I’ve posted the problem at http://stackoverflow.com/questions/37471477/download-files-from-s3-in-parallel-aws-net-sdk before I found this thread.

Does the AmazonS3Client have any synchronization or shared state?

I’m using the Quartz Scheduler for .Net and I was receiving the problem intermittently during a Job Execution. I have implemented Kobi’s and Stormsys’s posts and they both improved matters - but didn’t fully fix the problem.

The only thing that HAS managed to fix the problem has been to make a new instance of my S3Client in my Job Execution class whenever it executes rather than resolving it using dependency injection - I think it has something to do with thread safety. After implementing this, along with Kobi’s and Stormsys’s posts I haven’t experienced the problem any more.

Using the following code works, so i believe there’s an issue with the sync version in the SDK:

using (var amazonS3Client = CreateS3Client())
{
    var s3PartDownloadRequest = new GetObjectRequest
    {
        BucketName = download.Bucket,
        Key = download.ObjectKey,
        ByteRange = new ByteRange(position, end)
    };

    using (var getObjectResponse = amazonS3Client.GetObjectAsync(s3PartDownloadRequest).GetResult())//async with my own sync method extension.
    {    
        getObjectResponse.WriteResponseStreamToFile(dest);
    }
}


//GetResult:
        public static T GetResult<T>(this Task<T> task)
        {
            task.ConfigureAwait(false);
            task.Wait();

            return task.Result;
        }