oksse icon indicating copy to clipboard operation
oksse copied to clipboard

StrictMode error: Explicit termination method 'response.body().close()' not called

Open RyanRamchandar opened this issue 7 years ago • 10 comments

The following code throws a StrictMode error:

val request = Request.Builder().url(url).build()
val okSse = OkSse(client)
sse = okSse.newServerSentEvent(request, listener())
04-04 14:58:32.269 7907-8257/co.app.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 'response.body().close()' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:184)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at okhttp3.internal.platform.AndroidPlatform$CloseGuard.createAndOpen(AndroidPlatform.java:340)
        at okhttp3.internal.platform.AndroidPlatform.getStackTraceForCloseable(AndroidPlatform.java:155)
        at okhttp3.RealCall.captureCallStackTrace(RealCall.java:89)
        at okhttp3.RealCall.enqueue(RealCall.java:98)
        at com.here.oksse.RealServerSentEvent.enqueue(RealServerSentEvent.java:66)
        at com.here.oksse.RealServerSentEvent.connect(RealServerSentEvent.java:49)
        at com.here.oksse.OkSse.newServerSentEvent(OkSse.java:90)
        ... app stack

Version:

implementation "com.github.heremaps:oksse:0c60781b9a"

RyanRamchandar avatar Apr 04 '18 20:04 RyanRamchandar

These warnings seemed to have resurfaced after: #14

RyanRamchandar avatar Apr 05 '18 16:04 RyanRamchandar

Hi, can you share how do you set the StrictMode, I tried with

StrictMode.enableDefaults();

And

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().build());

Could not manage to see any StrictMode complains.

Also seems that should be safe to do call.cancel() from Main thread, it used to be an issue but now is fixed https://github.com/square/okhttp/issues/1592

marcelpinto avatar Apr 12 '18 07:04 marcelpinto

From OkHttp docs

Calls can be canceled from any thread. This will fail the call if it hasn’t yet completed! Code that is writing the request body or reading the response body will suffer an IOException when its call is canceled.

marcelpinto avatar Apr 12 '18 07:04 marcelpinto

@skimarxall we setup Strict mode like so:

StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build())
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build())

RyanRamchandar avatar Apr 23 '18 18:04 RyanRamchandar

I did not try the VM policy either with the penalty log, I will, but as mentioned in the other comment is it safe to call cancel from any thread

marcelpinto avatar Apr 24 '18 11:04 marcelpinto

I think my PR is resolving this. https://github.com/heremaps/oksse/pull/24

bruestel avatar Aug 24 '18 06:08 bruestel

I just merged the fix, please try the new version and let me know if the warning is solved

marcelpinto avatar Sep 18 '18 21:09 marcelpinto

Seems like problem still exists.

ghost avatar Sep 27 '18 18:09 ghost

@kashesoft could you make sure you are using the last version and if so, copy the logcat and the strict policy configuration you are using?

marcelpinto avatar Oct 06 '18 11:10 marcelpinto

Yes, we are using

StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build())
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build())

Logcat:

10-09 09:35:52.799 20249-20969/co.umbrela.launcher.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 'response.body().close()' not called
        at dalvik.system.CloseGuard.open(CloseGuard.java:184)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at okhttp3.internal.platform.AndroidPlatform$CloseGuard.createAndOpen(AndroidPlatform.java:340)
        at okhttp3.internal.platform.AndroidPlatform.getStackTraceForCloseable(AndroidPlatform.java:155)
        at okhttp3.RealCall.captureCallStackTrace(RealCall.java:89)
        at okhttp3.RealCall.enqueue(RealCall.java:98)
        at com.here.oksse.RealServerSentEvent.enqueue(RealServerSentEvent.java:70)
        at com.here.oksse.RealServerSentEvent.connect(RealServerSentEvent.java:50)
        at com.here.oksse.OkSse.newServerSentEvent(OkSse.java:90)
        ...

ghost avatar Oct 09 '18 14:10 ghost