awslabs / aws-crt-java

Java bindings for the AWS Common Runtime

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

closing CRT in a JVM shutdown hook times-outs its usages in other shutdown hooks, e.g. Spring's

faucct opened this issue · comments

Describe the bug

When I try to use the S3-client during the Spring-application shutdown, I get cryptic time-out exceptions:

java.lang.IllegalStateException: The service request was not made within 10 seconds of outputStream being invoked. Make sure to invoke the service request BEFORE invoking outputStream if your caller is single-threaded.
	at software.amazon.awssdk.core.async.BlockingOutputStreamAsyncRequestBody.waitForSubscriptionIfNeeded(BlockingOutputStreamAsyncRequestBody.java:93)
	at software.amazon.awssdk.core.async.BlockingOutputStreamAsyncRequestBody.outputStream(BlockingOutputStreamAsyncRequestBody.java:68)
	at it.joom.quality.infra.storage.s3.RemoteS3FileTest.lambda$testHangs$1(RemoteS3FileTest.java:44)
	at java.base/java.lang.Thread.run(Thread.java:829)

Expected Behavior

I expect that it would be possible to use S3 in a shutdown hook.

Current Behavior

It hangs for 10 seconds and fails with the cryptic time-out exception.

Reproduction Steps

var s3 = S3AsyncClient.crtBuilder().region(Region.EU_CENTRAL_1).build();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    try (s3) {
        var blockingOutputStreamAsyncRequestBody = AsyncRequestBody.forBlockingOutputStream(null);
        var response = s3.putObject(
                PutObjectRequest.builder().bucket("bucket").key("key").build(),
                blockingOutputStreamAsyncRequestBody
        );
        try (var ignored = blockingOutputStreamAsyncRequestBody.outputStream()) {
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        response.join();
    } catch (Exception e) {
        e.printStackTrace();
    }
}));

Possible Solution

No response

Additional Information/Context

No response

aws-crt-java version used

0.23.2

Java version used

openjdk version "17.0.2" 2022-01-18 OpenJDK Runtime Environment (build 17.0.2+8-86) OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

Operating System and version

macOS 13.4.1, Ubuntu 18

A duplicate of #561