okhttp: [Bug Report] Interceptor may throw a IllegalStateException: cache is closed
I am using the following code to add a custom header:
OkHttpClient.Builder builder = getBuilder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
.addHeader("Session-Token", accessToken)
.build();
return chain.proceed(request);
}
});
But sometimes I get an error:
Fatal Exception: java.lang.IllegalStateException: cache is closed
at okhttp3.internal.DiskLruCache.checkNotClosed(DiskLruCache.java:635)
at okhttp3.internal.DiskLruCache.edit(DiskLruCache.java:453)
at okhttp3.internal.DiskLruCache.edit(DiskLruCache.java:447)
at okhttp3.Cache.put(Cache.java:244)
at okhttp3.Cache.access$000(Cache.java:135)
at okhttp3.Cache$1.put(Cache.java:147)
at okhttp3.internal.http.HttpEngine.maybeCache(HttpEngine.java:389)
at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:633)
at okhttp3.RealCall.getResponse(RealCall.java:241)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
at co.voggle.core.ApiProvider$3.intercept(ApiProvider.java:149)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
at okhttp3.RealCall.access$100(RealCall.java:30)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
The error makes sense but I don’t understand how to work around it, as I do not have the required objects in the intercept(Chain chain)
method to check if the cache is closed or not.
It would be okay to drop the call if the cache is closed.
I am using OkHttp via Retorofit for my android application
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (6 by maintainers)
With great pain, I am using the following hack: