8245462: HttpClient send throws InterruptedException when interrupted but does not cancel request
Daniel Fuchs
daniel.fuchs at oracle.com
Tue Jun 23 17:47:51 UTC 2020
Hi,
Please find below a fix for:
8245462: HttpClient send throws InterruptedException when
interrupted but does not cancel request
https://bugs.openjdk.java.net/browse/JDK-8245462
webrev:
http://cr.openjdk.java.net/~dfuchs/webrev_8245462/webrev.00/index.html
The fix goes beyond fixing the problem described in the issue and
provides a way for cancelling asynchronous requests too.
Indeed, up to now the HttpClient didn't provide an easy way to
stop/cancel an ongoing HTTP request.
Up to now the only possible way of doing that would have been
to provide your own BodyPublisher/BodyHandler/BodySubsriber so
that you could stop sending the request body (by invoking onError
on the HttpClient subscriber) or stop the reception of the response
body (by cancelling the subsription you were given). However there
would be no way to stop the request outside of these limits.
If the request body was already sent, you would have to wait for
the response headers to have been received before you could
actually attempt to interrupt the exchange.
This is a bit of a shame since the HTTP/2 protocol does allow
for resetting streams if the client is no longer interested
in receiving a response.
This fix proposes to arrange for the HTTP exchange to be
stopped/cancelled if `cancel(true)` is invoked on the
CompletableFuture<> objects the default implementation
of the HttpClient returns, or on completable futures derived
from them. This makes it possible for the HttpClient implementation
to take note of the cancellation request asynchronously and then
stop the exchange as soon as it can.
The synchronous HttpClient::send also makes use of this new mechanism
to attempt to stop/cancel the exchange if the thread is interrupted
while it waits for the completable future to be completed.
best regards,
-- daniel
More information about the net-dev
mailing list