{"action":"created","issue":{"url":"https://api.github.com/repos/mebigfatguy/fb-contrib/issues/59","repository_url":"https://api.github.com/repos/mebigfatguy/fb-contrib","labels_url":"https://api.github.com/repos/mebigfatguy/fb-contrib/issues/59/labels{/name}","comments_url":"https://api.github.com/repos/mebigfatguy/fb-contrib/issues/59/comments","events_url":"https://api.github.com/repos/mebigfatguy/fb-contrib/issues/59/events","html_url":"https://github.com/mebigfatguy/fb-contrib/issues/59","id":107768324,"number":59,"title":"HCP_HTTP_REQUEST_RESOURCES_NOT_FREED_* does not apply to HttpClient 4.4","user":{"login":"kdeenanauth","id":112271,"avatar_url":"https://avatars.githubusercontent.com/u/112271?v=3","gravatar_id":"","url":"https://api.github.com/users/kdeenanauth","html_url":"https://github.com/kdeenanauth","followers_url":"https://api.github.com/users/kdeenanauth/followers","following_url":"https://api.github.com/users/kdeenanauth/following{/other_user}","gists_url":"https://api.github.com/users/kdeenanauth/gists{/gist_id}","starred_url":"https://api.github.com/users/kdeenanauth/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kdeenanauth/subscriptions","organizations_url":"https://api.github.com/users/kdeenanauth/orgs","repos_url":"https://api.github.com/users/kdeenanauth/repos","events_url":"https://api.github.com/users/kdeenanauth/events{/privacy}","received_events_url":"https://api.github.com/users/kdeenanauth/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2015-09-22T18:05:41Z","updated_at":"2016-12-29T22:31:07Z","closed_at":null,"body":"FindBugs noticed our usage of HttpClient as per http://fb-contrib.sourceforge.net/bugdescriptions.html#HCP_HTTP_REQUEST_RESOURCES_NOT_FREED_LOCAL . The fix is to call .reset:\n\n``` java\npublic String requestInfo(URI u) {\n    HttpGet httpGet = new HttpGet(u);\n    try(CloseableHttpResponse response = client.execute(httpGet);) {\n        return getResponseAsString(response);\n    }\n    catch (IOException e) {\n        e.printStackTrace();\n    }\n    finally {\n        httpGet.reset();\n    }\n    return null;\n}\n```\n\nI was very surprised to see that this is an issue as it is not documented anywhere on Apache's site. I dug into code for HttpClient 4.4 to investigate. It appears that calling `HttpGet.reset()` (or any of the Http request objects that extend from `AbstractExecutionAwareRequest`) calls `.cancel` on an arbitrary `Cancellable` object held on the request. Throughout the operation of `client.execute` the cancellable is swapped depending on the stage the request might be in (e.g. requesting a connection from the connection pool, consuming the http entity).\n\nCalling `.reset()` prior to `.execute(...)` does nothing because the cancellable would not be set. The call to `.execute(...)` is well encapsulated in a try-catch block to abort the connection if needed:\n\n``` java\ntry {\n...\n} catch (final ConnectionShutdownException ex) {\n            final InterruptedIOException ioex = new InterruptedIOException(\n                    \"Connection has been shut down\");\n            ioex.initCause(ex);\n            throw ioex;\n        } catch (final HttpException ex) {\n            releaseTrigger.abortConnection();\n            throw ex;\n        } catch (final IOException ex) {\n            releaseTrigger.abortConnection();\n            throw ex;\n        } catch (final RuntimeException ex) {\n            releaseTrigger.abortConnection();\n            throw ex;\n        }\n```\n\nThe only time calling `.reset()` would be handy is if the http request object is being reused or to cancel the request asynchronously.\n\nCan the documentation for HCP_HTTP_REQUEST_RESOURCES_NOT_FREED_\\* be updated to clarify which version(s) of HttpClient this applies to?\n\nI realize a more thorough investigation may be needed for older versions of HttpClient.\n"},"comment":{"url":"https://api.github.com/repos/mebigfatguy/fb-contrib/issues/comments/269703720","html_url":"https://github.com/mebigfatguy/fb-contrib/issues/59#issuecomment-269703720","issue_url":"https://api.github.com/repos/mebigfatguy/fb-contrib/issues/59","id":269703720,"user":{"login":"cogman","id":4906592,"avatar_url":"https://avatars.githubusercontent.com/u/4906592?v=3","gravatar_id":"","url":"https://api.github.com/users/cogman","html_url":"https://github.com/cogman","followers_url":"https://api.github.com/users/cogman/followers","following_url":"https://api.github.com/users/cogman/following{/other_user}","gists_url":"https://api.github.com/users/cogman/gists{/gist_id}","starred_url":"https://api.github.com/users/cogman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cogman/subscriptions","organizations_url":"https://api.github.com/users/cogman/orgs","repos_url":"https://api.github.com/users/cogman/repos","events_url":"https://api.github.com/users/cogman/events{/privacy}","received_events_url":"https://api.github.com/users/cogman/received_events","type":"User","site_admin":false},"created_at":"2016-12-29T22:31:07Z","updated_at":"2016-12-29T22:31:07Z","body":"Can I bring this up again?\r\n\r\nIt depends a lot on usage when you need to call reset or even entityutils.consume.  Circa 4.2 or 4.3 (I can't remember which) they added a response handler parameter to the execute.   If you use that, the client will automatically consume the entity at the end of the request ( see the documentation here https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/CloseableHttpClient.html#execute(org.apache.http.HttpHost,%20org.apache.http.HttpRequest,%20org.apache.http.client.ResponseHandler) )"}}
{"actor":{"display_login":"cogman"}}