Skip to content

Handle direct CanncelationException on timeout in JdkClientHttpRequest #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

Closed
wants to merge 1 commit into from

Conversation

giampa91
Copy link
Contributor

@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

@@ -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
Contributor 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 <[email protected]>

fix: use timeoutHandler with a flag isTimeout

    Closes spring-projectsgh-33973

    Signed-off-by: giampaolo <[email protected]>
@giampa91
Copy link
Contributor 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.

@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 8, 2025
@rstoyanchev rstoyanchev added this to the 6.2.10 milestone Aug 8, 2025
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.

Okay the intent is more clear now.

rstoyanchev pushed a commit that referenced this pull request Aug 8, 2025
Handle CancellationException in order to throw an HttpTimeoutException
when the timeout handler caused the cancellation.

See gh-34721

Signed-off-by: giampaolo <[email protected]>

fix: use timeoutHandler with a flag isTimeout

    Closes gh-33973

    Signed-off-by: giampaolo <[email protected]>
Copy link
Contributor

github-actions bot commented Aug 8, 2025

Fixed via 600d6c6

@rstoyanchev rstoyanchev changed the title Fix throws only ResourceAccessException on timeout Handle direct CanncelationException on timeout in JdkClientHttpRequest Aug 12, 2025
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) type: bug A general bug
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