heremaps / oksse

An extension library for OkHttp to create a Server-Sent Event (SSE) client.
Apache License 2.0
228 stars 27 forks source link

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

Open RyanRamchandar opened 6 years ago

RyanRamchandar commented 6 years ago

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 commented 6 years ago

These warnings seemed to have resurfaced after: #14

marcelpinto commented 6 years ago

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 commented 6 years ago

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.

RyanRamchandar commented 6 years ago

@skimarxall we setup Strict mode like so:

StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build())
StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .build())
marcelpinto commented 6 years ago

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

bruestel commented 6 years ago

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

marcelpinto commented 5 years ago

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

ghost commented 5 years ago

Seems like problem still exists.

marcelpinto commented 5 years ago

@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?

ghost commented 5 years ago

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)
        ...