Java java.io.EOFException during AWS S3 headObject with GZIP decompression
I'm encountering a perplexing java.io.EOFException wrapped in a java.io.UncheckedIOException when my Java application attempts to perform an S3Client.headObject operation. The error seems to originate within the AWS SDK's Crc32Validation and GZIPInputStream classes, suggesting an issue with GZIP decompression of the S3 object's metadata or content.
The application is a microservice that interacts with AWS S3. Specifically, the error occurs when calling s3Client.headObject() within a utility class (S3JsonStore) which is then called by a Storage class, ultimately triggered by a SyncController.
The full stack trace is as follows:
java.io.EOFException: java.io.UncheckedIOException
java.io.UncheckedIOException: java.io.EOFException
at software.amazon.awssdk.utils.FunctionalUtils.asRuntimeException(FunctionalUtils.java:180)
at software.amazon.awssdk.utils.FunctionalUtils.lambda$safeSupplier$4(FunctionalUtils.java:110)
at software.amazon.awssdk.utils.FunctionalUtils.invokeSafely(FunctionalUtils.java:136)
at software.amazon.awssdk.core.http.Crc32Validation.decompressing(Crc32Validation.java:85)
at software.amazon.awssdk.core.http.Crc32Validation.process(Crc32Validation.java:62)
at software.amazon.awssdk.core.http.Crc32Validation.validate(Crc32Validation.java:44)
at software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler$Crc32ValidationResponseHandler.handle(AwsSyncClientHandler.java:95)
at software.amazon.awssdk.core.internal.handler.BaseClientHandler.lambda$successTransformationResponseHandler$6(BaseClientHandler.java:234)
at software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.execute(HandleResponseStage.java:40)
at software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.execute(HandleResponseStage.java:30)
at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(ApiCallAttemptTimeoutTrackingStage.java:73)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(ApiCallAttemptTimeoutTrackingStage.java:42)
at software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(TimeoutExceptionHandlingStage.java:78)
at software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(TimeoutExceptionHandlingStage.java:40)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptMetricCollectionStage.execute(ApiCallAttemptMetricCollectionStage.java:50)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptMetricCollectionStage.execute(ApiCallAttemptMetricCollectionStage.java:36)
at software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:81)
at software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:36)
at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
at software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:56)
at software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:36)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.executeWithTimer(ApiCallTimeoutTrackingStage.java:80)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:60)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:42)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallMetricCollectionStage.execute(ApiCallMetricCollectionStage.java:48)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallMetricCollectionStage.execute(ApiCallMetricCollectionStage.java:31)
at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionStage.java:37)
at software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionStage.java:26)
at software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient$RequestExecutionBuilderImpl.execute(AmazonSyncHttpClient.java:193)
at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.invoke(BaseSyncClientHandler.java:103)
at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.doExecute(BaseSyncClientHandler.java:167)
at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.lambda$execute$1(BaseSyncClientHandler.java:82)
at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.measureApiCallSuccess(BaseSyncClientHandler.java:175)
at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.execute(BaseSyncClientHandler.java:76)
at software.amazon.awssdk.core.client.handler.SdkSyncClientHandler.execute(SdkSyncClientHandler.java:45)
at software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler.execute(AwsSyncClientHandler.java:56)
at software.amazon.awssdk.services.s3.DefaultS3Client.headObject(DefaultS3Client.java:5089)
at com.mycompany.myapp.util.S3JsonStore.getObjectMetadata(S3JsonStore.java:253)
at com.mycompany.myapp.microservices.sync.Storage.writeSyncTrigger(Storage.java:383)
at com.mycompany.myapp.microservices.sync.SyncController.sync(SyncController.java:64)
at com.mycompany.myapp.microservices.sync.SyncHandler.handleRequest(SyncHandler.java:34)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
Caused by: java.io.EOFException
at java.base/java.util.zip.GZIPInputStream.readUByte(Unknown Source)
at java.base/java.util.zip.GZIPInputStream.readUShort(Unknown Source)
at java.base/java.util.zip.GZIPInputStream.readHeader(Unknown Source)
at java.base/java.util.zip.GZIPInputStream.<init>(Unknown Source)
at java.base/java.util.zip.GZIPInputStream.<init>(Unknown Source)
at software.amazon.awssdk.core.http.Crc32Validation.lambda$decompressing$2(Crc32Validation.java:85)
at software.amazon.awssdk.utils.FunctionalUtils.lambda$safeSupplier$4(FunctionalUtils.java:108)
... 44 more
Context:
- Application: A Java microservice running on AWS Lambda (implied by
handleRequest and SyncHandler).
- AWS SDK: Using AWS SDK for Java 2.x (indicated by
software.amazon.awssdk packages).
- Operation: The error occurs during
S3Client.headObject(). This operation is typically used to retrieve object metadata without downloading the object content.
- GZIP: The
Caused by: java.io.EOFException at java.base/java.util.zip.GZIPInputStream.readHeader(Unknown Source) strongly suggests that the S3 object (or its metadata response) is being treated as a GZIP compressed stream, and the GZIP header is either truncated or invalid.
Relevant Code Snippets (simplified and generalized):
S3JsonStore.java (line 253):
public ObjectMetadata getObjectMetadata(String bucketName, String key) {
try {
HeadObjectRequest headObjectRequest = HeadObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
return s3Client.headObject(headObjectRequest); // This is line 253
} catch (S3Exception e) {
if (e.statusCode() == 404) {
return null; // Object not found
}
throw e;
}
}
Storage.java (line 383):
public void writeSyncTrigger(String triggerKey, SyncTrigger trigger) {
// ... other logic ...
ObjectMetadata metadata = s3JsonStore.getObjectMetadata(myBucket, triggerKey); // This is line 383
if (metadata == null) {
// ... create object ...
} else {
// ... update object ...
}
}
SyncController.java (line 64):
public void sync(SyncRequest request) {
// ...
storage.writeSyncTrigger(request.getTriggerKey(), request.getTrigger()); // This is line 64
// ...
}
Problem Description:
The EOFException indicates that the GZIPInputStream unexpectedly reached the end of the stream while trying to read the GZIP header. This happens when s3Client.headObject() is called.
What I've checked/considered:
- Object Existence: The
getObjectMetadata method handles 404s, so it's not simply a missing object. The error occurs even for objects that are known to exist.
- Object Content-Encoding: I've verified that the S3 objects themselves are not GZIP compressed. They are plain JSON files.
- AWS SDK Configuration: I haven't explicitly configured any GZIP decompression settings in the
S3Client builder. It seems the SDK is attempting to decompress the response automatically.
- Network Issues: While an
EOFException can sometimes indicate a truncated network stream, the fact that it's specifically within GZIPInputStream.readHeader and Crc32Validation points more towards an issue with how the SDK is interpreting the S3 response, rather than a general network problem.
- S3 Object Integrity: The objects in S3 are valid JSON and can be downloaded and read without issues.
My Hypothesis:
It appears the AWS SDK for Java 2.x's Crc32Validation component is attempting to decompress the headObject response as a GZIP stream. Why?
Question:
How can I diagnose and resolve this java.io.EOFException during S3Client.headObject?
- Is there a way to disable GZIP decompression for
headObject responses in the AWS SDK for Java 2.x?
- What could cause the
Crc32Validation to attempt GZIP decompression on a non-GZIP S3 object's metadata response?
- Are there any known issues or specific configurations related to
Crc32Validation and headObject that could lead to this?
Any insights or debugging steps would be greatly appreciated!
this seems to be a bug in the AWS SDK, the only workaround I found was to use GetObject instead of HeadObject, and discard the input stream