HttpURLConnection.getResponseCode() documentation comment
boris.x.shukhat at barclays.com
boris.x.shukhat at barclays.com
Wed Aug 3 17:55:55 UTC 2016
To whom it may concern,
This comment is about API documentation on HttpURLConnection.getResponseCode() and it is based on the implementation details as well.
HttpURLConnection implementation caches response code that allows to repeatedly get a response code from HttpURLConnection even after disconnect() is called. If a response code had been cached before disconnect() was called then its value is returned. But if the return code has not been cached, it (under some circumstances) opens a connection and leaves it opened if disconnect() is not called again. If called in the loop multiple times this design potentially can cause connection leakage and it happens in open source library
https://github.com/elastic/elasticsearch-metrics-reporter-java/blob/master/src/main/java/org/elasticsearch/metrics/ElasticsearchReporter.java
lines 397-406
I do not think that it is reasonable to change the implementation at this point because it can break existing software but it has to be reflected in API help on HttpURLConnection.getResponseCode() that it can leave connection open and another disconnect() may need to be called.
The problem can be reproduced by running the following code long enough and using ss Linux command (netstat command on Windows) to periodically monitor sockets with established connection.
public class Test
{
public static void main(String...args) throws Exception
{
while(true)
{
URL url=new URL("...");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.connect();
connection.disconnect();
int responseCode=connection.getResponseCode();
Thread.sleep(10);
}
}
}
Thanks,
Boris Shukhat
_______________________________________________
This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.
For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.
_______________________________________________
More information about the core-libs-dev
mailing list