UploadImpl.waitForUploadResult() infinite loop when upload is already done
The code should check if monitor.isDone() is already true so it can just call the Future.get().
public UploadResult waitForUploadResult()
throws AmazonClientException, AmazonServiceException, InterruptedException {
try {
UploadResult result = null;
while (!monitor.isDone() || result == null) {
Future<?> f = monitor.getFuture();
result = (UploadResult)f.get();
}
return result;
} catch (ExecutionException e) {
rethrowExecutionException(e);
return null;
}
}
Also happens on void com.amazonaws.services.s3.transfer.internal.AbstractTransfer.waitForCompletion() throws AmazonClientException, AmazonServiceException, InterruptedException
Hi,Agus!
Thanks for your report, First, which SDK version you are using? the latest one now is 1.4.4.2.
If the problem exists in the lasted version of SDK, would you please give me a sample case which can reproduce the problem?
thanks,
Yifei
Hi @agusprasetyo811, do you have a repro case for this? We have not been able to recreate the problem.
Hi there,
I modified the v1.5.4 sample AmazonS3TransferProgress.java (below) and can recreate this issue. I tried uploading files > 5Mb as well as < 5Mb with the same results i.e. it waits indefinitely for an upload result. Interestingly, both uploads succeed and I can see them in the bucket yet waitForUploadResult() never returns.
NOTE: In a previous version I was using (v1.2.1), this never occurred.
Disturbingly, for uploads < 5Mb and when ProgressEvent.COMPLETED_EVENT_CODE fired, upload.isDone() returned "false", whilst for uploads > 5Mb upload.isDone() returned "true". I would've thought that if the upload has completed that upload.isDone() would always return "true". Maybe due to the multi-part nature?
Anyhow, I took 5 thread dumps in case they were of interest to you but can't attach them. If you need them let me know.
In the meantime, I'm going to need to rollback the version of the AWS SDK until this is resolved OR find a workaround i.e. not use waitForUploadResult()
Would be interested in a fix :-)
Thanks
import com.amazonaws.AmazonClientException; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.ProgressEvent; import com.amazonaws.services.s3.model.ProgressListener; import com.amazonaws.services.s3.model.PutObjectRequest; import com.amazonaws.services.s3.transfer.TransferManager; import com.amazonaws.services.s3.transfer.Upload; import com.amazonaws.services.s3.transfer.model.UploadResult;
import java.io.File;
public class S3TransferProgressSample { String accessKey = "access"; String secretKey = "secret"; String bucketName = "bucketname"; String fileToUpload = "/tmp/LargerThan5Mb.txt"; private Upload s3Upload;
public static void main(String[] args)
{
new S3TransferProgressSample();
}
public S3TransferProgressSample()
{
try
{
AWSCredentials myCredentials = new BasicAWSCredentials(accessKey, secretKey);
TransferManager tx = new TransferManager(myCredentials);
File file = new File(fileToUpload);
//Define a progress listener for our upload
ProgressListener progressListener = new ProgressListener()
{
@Override
public void progressChanged(ProgressEvent progressEvent)
{
if (s3Upload == null)
{
System.out.println("s3Upload was NULL!!!");
return;
}
switch (progressEvent.getEventCode())
{
case ProgressEvent.COMPLETED_EVENT_CODE:
{
System.out.println("S3 Progress Event " + progressEvent.getEventCode() + " % Complete: " + s3Upload.getProgress().getPercentTransferred());
System.out.println("S3 COMPLETED_EVENT_CODE s3Upload.isDone(): " + s3Upload.isDone());
try
{
System.out.println("Waiting for upload result...");
UploadResult result = s3Upload.waitForUploadResult();
if (result != null)
{
String uploadedS3Filename = result.getKey();
System.out.println("File uploaded with key: " + uploadedS3Filename);
}
}
catch (Throwable e)
{
System.out.println("Throwable during S3 waitForUploadResult.");
e.printStackTrace();
}
System.out.println("S3 UPLOAD COMPLETE.");
break;
}
case ProgressEvent.STARTED_EVENT_CODE:
{
System.out.println("S3 STARTED_EVENT_CODE");
System.out.println("S3 Progress Event " + progressEvent.getEventCode() + " % Complete: " + s3Upload.getProgress().getPercentTransferred());
break;
}
case ProgressEvent.FAILED_EVENT_CODE:
{
System.out.println("S3 FAILED_EVENT_CODE");
System.out.println("S3 Progress Event " + progressEvent.getEventCode() + " % Complete: " + s3Upload.getProgress().getPercentTransferred());
try
{
AmazonClientException e = s3Upload.waitForException();
System.err.println("S3 upload failed!");
}
catch (Throwable e)
{
System.err.println("Throwable during S3 upload.");
}
break;
}
}
}
};
PutObjectRequest request = new PutObjectRequest(bucketName, file.getName(), file)
.withProgressListener(progressListener)
.withCannedAcl(CannedAccessControlList.PublicRead);
//This starts the upload thread but the listener above controls what happens after this
s3Upload = tx.upload(request);
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
thanks a lot for giving this sample codes! Confirmed the issue. Will be fixed soon.
The root cause for this issue is that all ProgressListener callbacks are executed in the main thread. This means that the high-level logic of TransferManager could be stuck in an intermediate state, if the provided ProgressListener#progressChanged calls a blocking method.
For example, in the repro code shown above, waitForUploadResult() is called inside listener callback, at which point the TransferManager has finished the low-level transfer but hasn't updated its monitor status. As a result, waitForUploadResult() is stuck in an infinite loop while waiting for the monitor status update, and this in turn blocks the main thread to ever update such status.
In 1.6.0 version, we have fixed this issue by executing all listener callbacks in a separate thread. Also, as part of this change, we have created a new ProgressListener interface in "com.amzonaws.event" namespace, which is shared by both Amazon S3 and Amazon Glacier clients. The previous s3.model.ProgressListener is now deprecated.
For more information, see the release notes: http://aws.amazon.com/releasenotes/5783184875850572
Thanks again for reporting this issue.