Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] ClassCastException occurs in extractAndWrapCause method of AwsSdk2Transport when ExecutionException's cause is not an Exception or Error #1480

Closed
wonthechan opened this issue Mar 13, 2025 · 1 comment · Fixed by #1483
Labels
bug Something isn't working

Comments

@wonthechan
Copy link
Contributor

What is the bug?

In the extractAndWrapCause method of AwsSdk2Transport, when handling an ExecutionException, the code assumes that executionException.getCause() is always an Exception or Error.

However, getCause() can return a Throwable that is neither an Exception nor an Error.
If the cause is another type of Throwable (e.g., a custom subclass that is neither Error nor Exception), the following line will cause a ClassCastException:

exception = (Exception)t;

This results in the loss of the original exception details.

How can one reproduce the bug?

This issue can be reproduced when using NettyNioAsyncHttpClient as the SdkAsyncHttpClient with a low connectionAcquisitionTimeout value.

Steps to reproduce:
1. Set up an OpenSearchClient using AwsSdk2Transport with NettyNioAsyncHttpClient.
2. Configure the client with a very low connectionAcquisitionTimeout, such as 1 millisecond.
3. Attempt to make a request to OpenSearch
4. When a connectionAcquisitionTimeout occurs, it triggers an ExecutionException.
5. The extractAndWrapCause method processes the exception, but if the cause is a Throwable that is neither an Error nor an Exception, a ClassCastException is thrown.

This results in the loss of the original exception context, making it difficult to diagnose the actual timeout issue.

What is the expected behavior?

When a connectionAcquisitionTimeout occurs, the exception should be correctly propagated without causing a ClassCastException.
• If the cause of ExecutionException is an Error, it should be rethrown (which is already handled correctly in the current implementation).
• If the cause is an Exception, it should be processed as intended.
• If the cause is a Throwable that is neither an Error nor an Exception, the code should not attempt to cast it to an Exception, as this leads to a ClassCastException. Instead, it should be wrapped in a more appropriate exception (e.g., RuntimeException).

What is your host/environment?

Mac OS Sonoma 14.6
opensearch-java:2.22.0
netty-nio-client:2.30.37
org.springframework.boot 3.2.7

Do you have any screenshots?

N/A

If possible, I would be happy to create a PR to fix this issue.

@wonthechan wonthechan added bug Something isn't working untriaged labels Mar 13, 2025
@Xtansia Xtansia removed the untriaged label Mar 13, 2025
@Xtansia
Copy link
Collaborator

Xtansia commented Mar 13, 2025

@wonthechan Thanks for reporting this, if you're interested in creating a fix for this it would be greatly welcomed.

@wonthechan wonthechan changed the title [BUG] ClassCastException Occurs in extractAndWrapCause Method of AwsSdk2Transport When ExecutionException's Cause is Not an Exception or Error [BUG] ClassCastException occurs in extractAndWrapCause method of AwsSdk2Transport when ExecutionException's cause is not an Exception or Error Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants