spring-attic / spring-social

Allows you to connect your applications with SaaS providers such as Facebook and Twitter.
http://projects.spring.io/spring-social
Apache License 2.0
619 stars 351 forks source link

ClientHttpRequestFactorySelector is leaking sockets in CLOSE_WAIT state #197

Open vminkov opened 8 years ago

vminkov commented 8 years ago

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

vminkov commented 8 years ago

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);