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

SDK Clients are `Closeable` and documented as thread safe but don't provide an `isClosed()` method

Open markjschreiber opened this issue 1 year ago • 3 comments

Describe the bug

Because SDK clients are Closeable they might be auto-closed if placed in a "try with resources" block. The SDK documentation encourages re-use of clients and advertises them as thread safe. This can mean another thread or a try with resources block might close a client without you realizing. Unfortunately you cannot check if the client is still open because there is no isClosed() method.

Expected Behavior

Ability to determine if a client is closed.

Current Behavior

No ability to determine if a client is closed (without making an API call).

Reproduction Steps

S3Client client;

// on another thread
try (client = S3Client.builder().build()) {
   // do some things
}

// back on the main thread the client has been auto closed but you don't know and cannot tell
client.headBucketRequest(...);  // will fail

### Possible Solution

* presence of an `isClosed()` method
* Or, to make it truly safe, future calls to a closed client could automatically re-establish a connection pool internally

### Additional Information/Context

_No response_

### AWS Java SDK version used

2.25.51

### JDK version used

OpenJDK Runtime Environment Corretto-17.0.11.9.1 (build 17.0.11+9-LTS)

### Operating System and version

Mac OS 14.4.1 (23E224)

markjschreiber avatar May 20 '24 20:05 markjschreiber

@markjschreiber feature request acknowledged, added to the backlog.

debora-ito avatar Jun 04 '24 18:06 debora-ito

Additionally it will also allow skipping of redundant clean up. We for certain code paths have not been able to implement the AutoClosable interface. But we have made sure to manually close the sdk object and redundancies for it.

Having an isClosed will allow us to skip these redundant closures.

sid22 avatar May 15 '25 05:05 sid22

Using profiling I found that creating a client can be very expensive (around 1000 milliseconds) therefore I would like to be able to re-use them as often as possible but would need to be able to ensure they are not closed.

markjschreiber avatar May 15 '25 14:05 markjschreiber