spring-attic / spring-social

Allows you to connect your applications with SaaS providers such as Facebook and Twitter.

Home Page:http://projects.spring.io/spring-social

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ClientHttpRequestFactorySelector is leaking sockets in CLOSE_WAIT state

vminkov opened this issue · comments

https://github.com/spring-projects/spring-social/blob/master/spring-social-core/src/main/java/org/springframework/social/support/ClientHttpRequestFactorySelector.java#L91

Even though HttpComponentsClientHttpRequestFactory is a DisposableBean, it is not handled by any BeanFactory when created like that and thus it is too late later when GC tries to close the CloseableHttpClient (and the sockets), but has to wait in it's finilize() method on an internal lock (PoolingHttpClientConnectionManager.shutdown()) . This makes GC way too slow to catch up with the socket creation on our production servers for example.

http://stackoverflow.com/questions/4724193/how-can-i-ensure-that-my-httpclient-4-1-does-not-leak-sockets

We solved the problem by faking that HttpComponents are not on the classpath and then it uses SimpleClientHttpRequestFactory to make each request without pooling the sockets.

          Field field = ClientHttpRequestFactorySelector.class.getDeclaredField("HTTP_COMPONENTS_AVAILABLE");
          field.setAccessible(true);

            Field modifiersField = Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

            field.set(null, false);