Skip to content

Fix throws only ResourceAccessException on timeout #34721

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

giampa91
Copy link

@giampa91 giampa91 commented Apr 5, 2025

CancellationExceptions are thrown instead of the expected ResourceAccessException during timeout scenarios.
Handle the CancellationExceptions in order to throw an ResourceAccessException when timeout occurred

Closes gh-33973

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 5, 2025
@giampa91 giampa91 force-pushed the main branch 2 times, most recently from 1b96990 to 1717ef7 Compare April 5, 2025 12:53
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Apr 8, 2025
@jhoeller jhoeller requested a review from rstoyanchev July 28, 2025 21:13
Copy link
Contributor

@rstoyanchev rstoyanchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be no purpose to moving the TimeoutHandler

@@ -96,12 +96,13 @@ public URI getURI() {
@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers, @Nullable Body body) throws IOException {
CompletableFuture<HttpResponse<InputStream>> responseFuture = null;
TimeoutHandler timeoutHandler = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be no purpose to this change that I can see. Is this intended?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change was to make the TimeoutHandler readable outside the try

@@ -224,6 +228,7 @@ public ByteBuffer map(byte[] b, int off, int len) {
private static final class TimeoutHandler {

private final CompletableFuture<Void> timeoutFuture;
private boolean isTimeout=false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise here, this flag is never used.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry my mistake, now this variable has a purpose

@rstoyanchev rstoyanchev self-assigned this Aug 1, 2025
…essException during timeout scenarios.

Handle the CancellationExceptions in order to throw an ResourceAccessException when timeout occurred

Closes spring-projectsgh-33973

Signed-off-by: giampaolo <giampaorr@gmail.com>

fix: use timeoutHandler with a flag isTimeout

    Closes spring-projectsgh-33973

    Signed-off-by: giampaolo <giampaorr@gmail.com>
@giampa91
Copy link
Author

giampa91 commented Aug 2, 2025

Thank you for your patience @rstoyanchev ! After a careful review, I realized I hadn’t double-checked the code thoroughly enough.

I’ve now updated the implementation to better distinguish between a cancelled request and a genuine timeout by explicitly checking the timeout variable.

Additionally, I’ve added tests covering both scenarios. While there might still be room for optimization, I believe this approach reflects real-world usage and edge cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Random CancellationException instead of ResourceAccessException after upgrading to Spring Boot 3.4.0
4 participants