Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threads hanging due to errors in OkHttp TaskRunner thread #6521

Closed
GeorgeFitzpatrick opened this issue Jan 21, 2021 · 2 comments
Closed

Threads hanging due to errors in OkHttp TaskRunner thread #6521

GeorgeFitzpatrick opened this issue Jan 21, 2021 · 2 comments
Labels
bug Bug in existing code needs info More information needed from reporter

Comments

@GeorgeFitzpatrick
Copy link

GeorgeFitzpatrick commented Jan 21, 2021

After sending a lot of consecutive requests, around 5/okhttp client instance/second, from anywhere between half an hour to 3 hours I start to get the following exception:

Exception in thread "OkHttp TaskRunner" java.lang.RuntimeException: invalid key or spec in GCM mode
	at java.base/sun.security.ssl.SSLCipher$T13GcmWriteCipherGenerator$GcmWriteCipher.encrypt(SSLCipher.java:2023)
	at java.base/sun.security.ssl.OutputRecord.t13Encrypt(OutputRecord.java:500)
	at java.base/sun.security.ssl.OutputRecord.encrypt(OutputRecord.java:463)
	at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:80)
	at java.base/sun.security.ssl.TransportContext.warning(TransportContext.java:248)
	at java.base/sun.security.ssl.SSLSocketImpl.deliverClosedNotify(SSLSocketImpl.java:639)
	at java.base/sun.security.ssl.SSLSocketImpl.closeNotify(SSLSocketImpl.java:627)
	at java.base/sun.security.ssl.SSLSocketImpl.duplexCloseOutput(SSLSocketImpl.java:550)
	at java.base/sun.security.ssl.SSLSocketImpl.close(SSLSocketImpl.java:485)
	at okhttp3.internal.Util.closeQuietly(Util.kt:498)
	at okhttp3.internal.connection.RealConnectionPool.cleanup(RealConnectionPool.kt:184)
	at okhttp3.internal.connection.RealConnectionPool$cleanupTask$1.runOnce(RealConnectionPool.kt:44)
	at okhttp3.internal.concurrent.TaskRunner.runTask(TaskRunner.kt:116)
	at okhttp3.internal.concurrent.TaskRunner.access$runTask(TaskRunner.kt:42)
	at okhttp3.internal.concurrent.TaskRunner$runnable$1.run(TaskRunner.kt:65)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.security.InvalidAlgorithmParameterException: Cannot reuse iv for GCM encryption
	at java.base/com.sun.crypto.provider.CipherCore.init(CipherCore.java:582)
	at java.base/com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:346)
	at java.base/javax.crypto.Cipher.init(Cipher.java:1431)
	at java.base/sun.security.ssl.SSLCipher$T13GcmWriteCipherGenerator$GcmWriteCipher.encrypt(SSLCipher.java:2019)
	... 17 more

The error is not propagated to the callee causing the thread of the callee to hang indefinitely, this is causing me large issues as a workaround I'm using the guava TimeLimiter but this is far from an ideal solution. I'm assuming "OkHttp TaskRunner" threads are shared between OkHttpClient instances as well as when the error occurs other threads hang on their next request.

My Environment is as follows:

  • Windows 10
  • Oracle JDK SE 11.0.9
  • OkHttp 4.9.0

I don't see why it would, but If it makes a difference I am also using the following network interceptor

private Interceptor createNetworkInterceptorInterceptor() {
        return chain -> {
           try {
               HttpUrl url = chain.request().url();
               Request.Builder builder = chain.request().newBuilder();

               builder.removeHeader("User-Agent");
               builder.addHeader("User-Agent", useragent);

               builder.removeHeader("cookie");
               builder.addHeader("cookie", getCookiesAsHeader(url));

               Response response = chain.proceed(builder.build());
               Cookie.parseAll(url, response.headers()).forEach(cookie ->
                       addCookie(url, cookie));
               return response;
           } catch (Exception e){
               throw new IOException(e.getMessage());
           }
        };
    }
@GeorgeFitzpatrick GeorgeFitzpatrick added the bug Bug in existing code label Jan 21, 2021
@swankjesse
Copy link
Collaborator

This looks a lot like a JDK race condition:
https://bugs.openjdk.java.net/browse/JDK-8206968

But what’s weird is that was fixed in JDK 11.0.2.

Could you please report to the JDK issue tracker?

@yschimke yschimke added the needs info More information needed from reporter label Jan 23, 2021
@yschimke
Copy link
Collaborator

yschimke commented Feb 2, 2021

Closing out here - needs to be reported to JDK issue tracker.

@yschimke yschimke closed this as completed Feb 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code needs info More information needed from reporter
Projects
None yet
Development

No branches or pull requests

3 participants