aws-sdk-java-v2 icon indicating copy to clipboard operation
aws-sdk-java-v2 copied to clipboard

EC2 Waiter `waitUntilInstanceRunning` Hanging When `DescribeInstancesRequest` Empty

Open Petersoj opened this issue 1 year ago • 5 comments

Describe the bug

The following code hangs indefinitely when there are no pending EC2 instances and only running EC2 instances. Adding both pending and running to the filter works properly, but intuitively, it seems like when DescribeInstancesRequest returns no instances, the waiter should return or fail, not wait indefinitely.

ec2Client.waiter().waitUntilInstanceRunning(DescribeInstancesRequest.builder()
         .filters(Filter.builder()
                 .name("instance-state-name")
                 .values("pending")
                 .build())
         .build());

Expected Behavior

The waitUntilInstanceRunning call to return immediately when the DescribeInstancesRequest yields no instances.

Current Behavior

The waitUntilInstanceRunning call with the aforementioned DescribeInstancesRequest and running EC2 instance states hangs and waits indefinitely.

Reproduction Steps

See description.

Possible Solution

When the DescribeInstancesRequest given to the waitUntilInstanceRunning yields an empty result (no instances), the waiter should succeed and return immediately. This may have unintended consequences so perhaps it should just fail or the WaiterOverrideConfiguration could have an option to allow empty results from the DescribeInstancesRequest.

Additional Information/Context

No response

AWS Java SDK version used

2.25.15

JDK version used

21

Operating System and version

Linux Ubuntu 22.04

Petersoj avatar Apr 11 '24 01:04 Petersoj

Hi @Petersoj,

Thank you for reporting the issue. I am able to reproduce the scenario. I will take this forward to discuss with the Java SDK team.

Regards, Chaitanya

bhoradc avatar Apr 23 '24 16:04 bhoradc

@bhoradc that's great to hear. And just FYI, when I stated "indefinitely" in my original post, I forgot to mention that I was using a modified WaiterOverrideConfiguration that effectively made the waiter() hang indefinitely. Of course, without the override, the waiter eventually times out and throws based on the default waiter configuration.

Petersoj avatar Apr 23 '24 16:04 Petersoj

Yes, that makes sense. Thanks for mentioning it though.

Regards, Chaitanya

bhoradc avatar Apr 23 '24 16:04 bhoradc

Hi @Petersoj,

This happens to be an expected behaviour with the ec2Client.waiter().waitUntilInstanceRunning() when filtered with pending state instances.

The InstanceRunning condition doesn't check or isn't designed to check for the pending state as per the reference.

If you want to independently filter out for pending state instances, you may want to work with waitUntilInstanceStopped() or waitUntilInstanceTerminated().

Please let me know if that helps or if you have further queries.

Regards, Chaitanya

bhoradc avatar Apr 25 '24 21:04 bhoradc

Thanks for the info. I understand this is expected behavior, but my objective with this issue is to show how this behavior seems unintuitive and should be changed as noted in my original post.

Petersoj avatar Apr 26 '24 15:04 Petersoj