question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug Report] Interceptor may throw a IllegalStateException: cache is closed

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
vedant1811commented, Jun 10, 2016

With great pain, I am using the following hack:

OkHttpClient.Builder builder = getBuilder()
        .addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
                        .addHeader("Client-Key", CLIENT_KEY)
                        .build();
                try {
                    return chain.proceed(request);
                } catch (IllegalStateException e) {
                    LogWrapper.e(TAG, "error in client key interceptor", e);
                    // this method "throws IOException" anyway so we will not get a crash.
                    throw new IOException(e);
                }
            }
        });
0reactions
CodeK1988commented, Oct 18, 2018

@MariannaVR how to resolve it,please? thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

900946 – (JBPAPP6-1274) IllegalStateException: Cache is in ...
lang.IllegalStateException: Default cache is in 'TERMINATED' state and so it does not accept new invocations. Either restart it or recreate the cache container....
Read more >
Retrofit : java.lang.IllegalStateException: closed - Stack Overflow
When I try to execute below code, in file named SynchronizationManager.kt, it gives me an error. var rulesResourcesServices = RetrofitInstance( ...
Read more >
quarkus-cache interceptors and @NonBlocking methods
As a consequence of that change, the cache interceptors now contain an await() instruction which is causing the issue Rostislav found.
Read more >
okhttp/src/main/java/com/squareup/okhttp/internal/http ...
<p>The request and response may be served by the HTTP response cache, by the ... @throws RouteException if the was a problem during...
Read more >
Performing Basic Cache Operations - Coherence
An illegal state exception is thrown if an application attempts use a closed session or its resources. Use the close method to close...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found