Okhttp: A resource was acquired at attached stack trace but never released

Created on 21 Feb 2017  Â·  7Comments  Â·  Source: square/okhttp

A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.

java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:180)
at java.io.FileOutputStream.<init>(FileOutputStream.java:89)
at okio.Okio.appendingSink(Okio.java:185)
at okhttp3.internal.io.FileSystem$1.appendingSink(FileSystem.java:59)
at okhttp3.internal.cache.DiskLruCache.newJournalWriter(DiskLruCache.java:310)
at okhttp3.internal.cache.DiskLruCache.readJournal(DiskLruCache.java:302)
at okhttp3.internal.cache.DiskLruCache.initialize(DiskLruCache.java:229)
at okhttp3.internal.cache.DiskLruCache.get(DiskLruCache.java:431)
at okhttp3.Cache.get(Cache.java:194)
at okhttp3.Cache$1.get(Cache.java:144)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:70)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at com.xiangkan.android.base.http.CommonInterceptor.intercept(CommonInterceptor.java:53)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at com.xiangkan.android.base.http.CacheInterceptor.intercept(CacheInterceptor.java:24)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
at okhttp3.RealCall.access$100(RealCall.java:33)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:120)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
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)
bug

Most helpful comment

Can fix.

All 7 comments

Hey guys, i also have a similar issue:

03-13 15:33:21.736 29491-29500/de.blabla.debug E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
                                                                    java.lang.Throwable: Explicit termination method 'close' not called
                                                                        at dalvik.system.CloseGuard.open(CloseGuard.java:180)
                                                                        at java.io.FileOutputStream.<init>(FileOutputStream.java:222)
                                                                        at okio.Okio.appendingSink(Okio.java:185)
                                                                        at okhttp3.internal.io.FileSystem$1.appendingSink(FileSystem.java:59)
                                                                        at okhttp3.internal.cache.DiskLruCache.newJournalWriter(DiskLruCache.java:314)
                                                                        at okhttp3.internal.cache.DiskLruCache.readJournal(DiskLruCache.java:306)
                                                                        at okhttp3.internal.cache.DiskLruCache.initialize(DiskLruCache.java:227)
                                                                        at okhttp3.internal.cache.DiskLruCache.get(DiskLruCache.java:435)
                                                                        at okhttp3.Cache.get(Cache.java:194)
                                                                        at okhttp3.Cache$1.get(Cache.java:144)
                                                                        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:54)
                                                                        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                                                        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
                                                                        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
                                                                        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                                                        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
                                                                        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
                                                                        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
                                                                        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
                                                                        at okhttp3.RealCall.execute(RealCall.java:63)

the code triggering this looks like:

final Request.Builder okHttpBuilderHeadRequest = new Request.Builder().url(backendUrl).cacheControl(new CacheControl.Builder().noCache().build());
String brandNewLastModified = null;
final Request okHttpHeadRequest = okHttpBuilderHeadRequest.head().build();
try (final Response response = okHttp3Client.newCall(okHttpHeadRequest).execute();) {
    brandNewLastModified = response.header(HEADER_LAST_MODIFIED);
} catch (final @NonNull IOException e) {
    L.e(e, "blablabla -cut");
}

Sorry for the formatting, but it really looks like it's not working any better … :/

Can fix.

Solution provided by @p0wl: Add response.close(); to your onResponse handler.

client.newCall(okRequest).enqueue(new okhttp3.Callback() {
  @Override
  public void onResponse(final Call call, final Response response) throws IOException {
    // Your code here
    response.close(); // <- Do this and everything will be fine.
  }
});

If you have a timeout, there will be no response. Still reproducing on v3.8.0

@ytrjp I had the same error
I solved this by creating the OkHttpClient as singleton
I think that when you create multiple instance of OkHttpclient, all of them want access the cache file at the same time, and that is where the error came from

I am not 100% sure, but this approach helped me

Similar issue

05-22 14:29:17.849 24579-24587 E: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java:180)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:288)
at com.android.okhttp.internal.http.SocketConnector.connectTls(SocketConnector.java:103)
at com.android.okhttp.Connection.connect(Connection.java:143)
at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:114)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at com.android.tools.profiler.support.network.httpurl.TrackedHttpURLConnection.connect(TrackedHttpURLConnection.java:154)
at com.android.tools.profiler.support.network.httpurl.HttpsURLConnection$.connect(HttpsURLConnection$.java:366)
at services.HelpCenterUpgradeService.download(HelpCenterUpgradeService.java:189)
at services.HelpCenterUpgradeService.upgrade(HelpCenterUpgradeService.java:163)
at services.HelpCenterUpgradeService.onHandleWork(HelpCenterUpgradeService.java:141)
at android.support.v4.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:392)
at android.support.v4.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:383)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
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)

I used try with resource

try (InputStream input = new BufferedInputStream(url.openStream(), 8 * FileUtil.BYTES_IN_KB)) {
      try (OutputStream output = new FileOutputStream(destinationPath)) {
        byte[] data = new byte[FileUtil.BYTES_IN_KB];
        while ((count = input.read(data)) != -1) {
          output.write(data, 0, count);
        }
        output.flush();
      }
    }

@ytrjp I had the same error
I solved this by creating the OkHttpClient as singleton
I think that when you create multiple instance of OkHttpclient, all of them want access the cache file at the same time, and that is where the error came from

I am not 100% sure, but this approach helped me

This worked for me.

Was this page helpful?
0 / 5 - 0 ratings