-
Notifications
You must be signed in to change notification settings - Fork 2.3k
HttpUrlConnector.secureConnection not always setting custom SSL socket factory #3293
Comments
Reported by benpiper |
nkoterba said: I spent 2+ days trying to understand why simultaneous JERSEY client SSL REST requests to our server were failing. As mentioned, out of N requests, N-1 would ALWAYS fail, 1 request would always succeed. If I added a delay between REST requests of around 100-200 ms, all requests would succeed indicating to me a threading/deadlock/resource competition in the JERSEY client logic. On the recommendation of a colleague, I switched from using the default JERSEY REST client connector (Based on HttpUrlConnector) to the Apache REST client connector (maven: org.apache.httpcomponents:httpclient:4.5.2), and immediately all N requests were successfully sent and received. This seems like a MASSIVE bug and it sounds like the OP has isolated the exact location and cause of this issue. I am commenting here versus opening up a new ticket because I'm hoping this will get addressed very soon in future Jersey versions. Until then, we will be using the Apache Connector. |
nkoterba said: |
nkoterba said: However, I did encounter two additional issues that I'm posting here for others: |
This issue was imported from java.net JIRA JERSEY-3021 |
…y() to avoid a race condition in HttpUrlConnector#secureConnection
To be more precise, due to changes introduced by #3171 (jersey/jersey@57f5daf#diff-2af8c9e0f0f9963b8b1ce356cf17ecb7), a custom SSL socket factory is not set on an HttpsURLConnection 'suc' in HttpUrlConnector unless HttpsURLConnection.getDefaultSSLSocketFactory returns the exact same object as suc.getSSLSocketFactory(). This is not a robust way of determining if the custom socket factory has already been set because HttpsURLConnection.getDefaultSSLSocketFactory() can be called simultaneously by multiple threads, returning a different object for each invocation, thus resulting in up to n-1 threads getting a mismatch in HttpUrlConnector.secureConnection, even though suc.getSSLSocketFactory() is actually returning a default SSL socket factory (just not necessarily the exact instance we expected, which is ultimately irrelevant).
This is fairly easy to reproduce, given an HTTPS server with a self-signed certificate. The following code is not a self-contained unit test, but will demonstrate the issue nevertheless, by spitting out at least one SSLHandshakeException (caused by the custom SSLContext not being used). If numThreads is set to 1, then it will never fail due to a certificate validation error. The code will also run without error on Jersey 2.21 or earlier.
I would suggest that the change to HttpUrlConnector.secureConnection could be reverted, or at least if the purpose of it was to avoid redundant calls to setSSLSocketFactory (the cost of which is arguably insignificant), that comparing suc.getSSLSocketFactory to sslSocketFactory.get() might be more appropriate than comparing it to HttpsUrlConnection.getDefaultSSLSocketFactory().
Environment
Ubuntu 14.04, JDK 1.8.0_45
Affected Versions
[2.22]
The text was updated successfully, but these errors were encountered: