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

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

Open
RyanRamchandar opened this issue Apr 4, 2018 · 10 comments

Comments

@RyanRamchandar
Copy link
Contributor

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
Copy link
Contributor Author

These warnings seemed to have resurfaced after: #14

@marcelpinto
Copy link
Collaborator

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
square/okhttp#1592

@marcelpinto
Copy link
Collaborator

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
Copy link
Contributor Author

@skimarxall we setup Strict mode like so:

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

@marcelpinto
Copy link
Collaborator

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
Copy link
Contributor

I think my PR is resolving this. #24

@marcelpinto
Copy link
Collaborator

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

@ghost
Copy link

ghost commented Sep 27, 2018

Seems like problem still exists.

@marcelpinto
Copy link
Collaborator

@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
Copy link

ghost commented Oct 9, 2018

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

maniac103 added a commit to maniac103/openhab.android that referenced this issue Sep 30, 2019
0.9.0 suffers from a response body leak [1]

[1] heremaps/oksse#17

Signed-off-by: Danny Baumann <[email protected]>
mueller-ma added a commit to openhab/openhab-android that referenced this issue Oct 2, 2019
Fixes #1560

* Update OkSse to latest master branch.

0.9.0 suffers from a response body leak [1]

[1] heremaps/oksse#17

Signed-off-by: Danny Baumann <[email protected]>

* Simplify code

Co-Authored-By: mueller-ma <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants