JDK-8267140 : Clarify that HttpClient does not have to be closed / support closing HttpClient
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 16
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2021-05-13
  • Updated: 2021-05-14
Description
A DESCRIPTION OF THE PROBLEM :
The documentation for java.net.http.HttpClient does not explain if / when underlying resources are closed. This is causing confusion (see https://stackoverflow.com/q/53919721 ) because users are normally used to closing the client when they are done with their HTTP requests, see for example:
- Apache HttpClient: https://hc.apache.org/httpcomponents-client-5.1.x/current/httpclient5/apidocs/org/apache/hc/client5/http/impl/classic/CloseableHttpClient.html
- Ktor: https://api.ktor.io/1.5.4/io.ktor.client/-http-client/close.html

Therefore it would be good to explain in the HttpClient documentation when resources are closed.
Alternatively / additionally (depending on how reliable the current implementation is), it might be good to provide a method to close the HttpClient manually (e.g. by implementing Closeable).



Comments
The HttpClient will terminate and release all its resources when: 1. all referenced to the HttpClient are released 2. all operations are finished (all request responses have been received and relayed to calling code) Adding a close() method may be envisaged but the semantics and behaviour of closing a client when it is still in use and some operations are still in flight must be carefully analyzed, in particular WRT what should happen to the requests that may not have completed yet. Close might be the wrong primitive here: stop/shutdown might be preferable. The implementation of a close/stop/shutdown method that behaves correctly is not trivial.
14-05-2021

Moved to JDK to discuss the more clarifications of closing underlying resources are closed of HttpClient.
14-05-2021