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

Cache is closed exception with OkHttp 2.7.1 #2228

Closed
hidroh opened this issue Jan 12, 2016 · 9 comments
Closed

Cache is closed exception with OkHttp 2.7.1 #2228

hidroh opened this issue Jan 12, 2016 · 9 comments
Labels
needs info More information needed from reporter

Comments

@hidroh
Copy link

hidroh commented Jan 12, 2016

Got these (very rare) crashes recently, started after I upgraded to Retrofit 2.0.0-beta2. All of my network calls use asynchronous callbacks, either through OkHttpClient.newCall(Request).enqueue(OkHttp.Callback) or Retrofit.Call.enqueue(Retrofit.Callback)

I'm using OkHttp 2.7.1 and Retrofit 2.0.0-beta2.

Fatal Exception: java.lang.IllegalStateException: cache is closed
       at com.squareup.okhttp.internal.DiskLruCache.checkNotClosed(DiskLruCache.java:630)
       at com.squareup.okhttp.internal.DiskLruCache.get(DiskLruCache.java:424)
       at com.squareup.okhttp.Cache.get(Cache.java:184)
       at com.squareup.okhttp.Cache$1.get(Cache.java:139)
       at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
       at com.squareup.okhttp.Call.getResponse(Call.java:286)
       at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
       at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
       at com.squareup.okhttp.Call.access$100(Call.java:35)
       at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:171)
       at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
       at java.lang.Thread.run(Thread.java:818)
@swankjesse
Copy link
Member

Do you have code that closes the cache?

@swankjesse swankjesse added the needs info More information needed from reporter label Jan 14, 2016
@swankjesse swankjesse added this to the Icebox milestone Jan 14, 2016
@hidroh
Copy link
Author

hidroh commented Jan 14, 2016

Nope I don't have any logic that handles cache, except for during construction of OkHttpClient instance as follows:

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setCache(new Cache(context.getApplicationContext().getCacheDir(),
        CACHE_SIZE));

My network logic mostly relies on standard Retrofit interfaces with callback, with @Headers("Cache-Control: max-age=600"), except for a few POST request chain thru OkHttp with cookie handling.

@UweTrottmann
Copy link

I am seeing the cache is closed at this same location as well with okhttp 3.0.1.

Fatal Exception: java.lang.IllegalStateException: cache is closed
       at okhttp3.internal.DiskLruCache.checkNotClosed(DiskLruCache.java:622)
       at okhttp3.internal.DiskLruCache.get(DiskLruCache.java:420)
       at okhttp3.Cache.get(Cache.java:194)
       at okhttp3.Cache$1.get(Cache.java:144)
       at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:198)
       at okhttp3.RealCall.getResponse(RealCall.java:240)
       at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
       at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
       at okhttp3.RealCall.execute(RealCall.java:57)

Most of them (95%) occur on Nexus 9/Android 6.0.1. edit: It is very rare (only a handful users last month), so the device/Android version might be skewed due to the user base.

Possibly related to #2365.

@samehmikhail
Copy link

We started to see same crash after upgrading from 2.5 to 2.7.5. Using retrofit 1.9.0
It has not clear scenario and happens on different devices with different operating systems (4.4.2, 4.4.4, 5.0.2, 5.1.1)
Here is stack trace

Stack Trace


0 java.lang.IllegalStateException: cache is closed
1 at com.squareup.okhttp.internal.DiskLruCache.checkNotClosed(DiskLruCache.java:630)
2 at com.squareup.okhttp.internal.DiskLruCache.edit(DiskLruCache.java:452)
3 at com.squareup.okhttp.internal.DiskLruCache.edit(DiskLruCache.java:446)
4 at com.squareup.okhttp.Cache.put(Cache.java:235)
5 at com.squareup.okhttp.Cache.access$000(Cache.java:131)
6 at com.squareup.okhttp.Cache$1.put(Cache.java:142)
7 at com.squareup.okhttp.internal.http.HttpEngine.maybeCache(HttpEngine.java:404)
8 at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:647)
9 at com.squareup.okhttp.Call.getResponse(Call.java:287)
10 at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
11 at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
12 at com.squareup.okhttp.Call.access$100(Call.java:35)
13 at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:171)
14 at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
15 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
16 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
17 at java.lang.Thread.run(Thread.java:831)

@2fours
Copy link

2fours commented Apr 2, 2016

Also seeing this in 3.0.1.

04-02 12:11:05.956 20144-21553/com.twofours.surespot D/okhttp: okhttp: --> POST http://192.168.10.24:8080/invite/old HTTP/1.1 (0-byte body)
04-02 12:11:05.956 20144-21553/com.twofours.surespot E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
                                                                       Process: com.twofours.surespot, PID: 20144
                                                                       java.lang.IllegalStateException: cache is closed
                                                                           at okhttp3.internal.DiskLruCache.checkNotClosed(DiskLruCache.java:622)
                                                                           at okhttp3.internal.DiskLruCache.get(DiskLruCache.java:420)
                                                                           at okhttp3.Cache.get(Cache.java:194)
                                                                           at okhttp3.Cache$1.get(Cache.java:144)
                                                                           at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:198)
                                                                           at okhttp3.RealCall.getResponse(RealCall.java:240)
                                                                           at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
                                                                           at com.twofours.surespot.network.UserAgentInterceptor.intercept(UserAgentInterceptor.java:26)
                                                                           at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
                                                                           at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                                           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:33)
                                                                           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)

@2fours
Copy link

2fours commented Apr 2, 2016

For me looks like it's happening when I POST the same url twice in a row, happens every time:

04-02 12:18:09.149 26715-26889/com.twofours.surespot D/okhttp: okhttp: --> POST http://192.168.10.24:8080/invite/old HTTP/1.1 (0-byte body)
04-02 12:18:09.229 26715-26889/com.twofours.surespot D/okhttp: okhttp: <-- 204 No Content http://192.168.10.24:8080/invite/old (74ms, unknown-length body)
04-02 12:18:16.196 26715-26889/com.twofours.surespot D/okhttp: okhttp: --> DELETE http://192.168.10.24:8080/friends/old HTTP/1.1 (0-byte body)
04-02 12:18:16.236 26715-26889/com.twofours.surespot D/okhttp: okhttp: <-- 204 No Content http://192.168.10.24:8080/friends/old (46ms, unknown-length body)
04-02 12:18:19.479 26715-26889/com.twofours.surespot D/okhttp: okhttp: --> POST http://192.168.10.24:8080/invite/old HTTP/1.1 (0-byte body)
04-02 12:18:19.479 26715-26889/com.twofours.surespot E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
                                                                       Process: com.twofours.surespot, PID: 26715
                                                                       java.lang.IllegalStateException: cache is closed
                                                                           at okhttp3.internal.DiskLruCache.checkNotClosed(DiskLruCache.java:622)
                                                                           at okhttp3.internal.DiskLruCache.get(DiskLruCache.java:420)
                                                                           at okhttp3.Cache.get(Cache.java:194)
                                                                           at okhttp3.Cache$1.get(Cache.java:144)
                                                                           at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:198)
                                                                           at okhttp3.RealCall.getResponse(RealCall.java:240)
                                                                           at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
                                                                           at com.twofours.surespot.network.UserAgentInterceptor.intercept(UserAgentInterceptor.java:26)
                                                                           at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
                                                                           at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
                                                                           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:33)
                                                                           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)

@samehmikhail
Copy link

In my case this was happening because I had two okhttp instances share the same cache directory, which isn't recommended by okhttp according to documentation. Once having two different cache directories problem not happening anymore

@2fours
Copy link

2fours commented Apr 4, 2016

@samehgerges definitely only have 1 instance.

@2fours
Copy link

2fours commented Apr 4, 2016

Figured it out. I was calling cache.delete() before the second POST which apparently closes the cache.

@swankjesse swankjesse removed this from the Icebox milestone Nov 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs info More information needed from reporter
Projects
None yet
Development

No branches or pull requests

5 participants