SimpleClientHttpResponse.close() does not call close() on URLConnection.getInputStream() if SimpleClientHttpResponse.getBody() was not called before [SPR-17181] #21717
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: enhancement
A general enhancement
Milestone
Jonas Woerlein opened SPR-17181 and commented
We've noticed
SimpleClientHttpResponse.close()
does not callclose()
onURLConnection.getInputStream()
ifSimpleClientHttpResponse.getBody()
was not called before as theresponseStream
field is lazily initalized within the latter method.JDK8 documentation states the
InputStream
returned byURLConnection.getInputStream()
has to be read till the end or explicitlyclose()
d to allow the JDK's protocol handler to clean up the connection and put it into a connection cache for reuse by future HTTP requests.By debugging, we found the
KeepAliveStream
being returned byURLConnection.getInputStream()
to be created not on calling this method but on parsing the HTTP response.Caused by this issue, we experience HTTP connections created or reused by a PUT request not being reused for any subsequent requests. This leads to higher network load and increased latency due to additional TLS handshakes.
The requests were emitted with RestTemplate
The server responds with something like
So there is a response body, but as it contains no interesting information, taking the status into consideration is sufficient for us.
The current workaround is to read the body into a String but ignore it, like so:
This way, the
HttpMessageConverterExtractor
callsSimpleClientHttpResponse.getBody()
and thus initializes theresponseStream
field. When theRestTemplate
thenclose()
s the SimpleClientHttpResponse, theresponseStream
is also closed.As we believe creating a String object which is never read is bad practice, we would appreciate the Spring Team to consider changing the behavior in
SimpleClientHttpResponse
. CallinggetBody()
inclose()
ifresponseStream
isnull
might be the solution to this issue.We encountered this issue with version 4.3.12, but it also exists in 5.0.7 so I marked this version as affected. The issue might be related to #12775.
Affects: 4.3.18, 5.0.7
Issue Links:
Referenced from: commits 23fc6f6
The text was updated successfully, but these errors were encountered: