matomo-org / matomo-sdk-android

SDK for Android to measure your apps with Matomo. Works on Android phones, tablets, Fire TV sticks, and more!
BSD 3-Clause "New" or "Revised" License
393 stars 164 forks source link

Explicit termination method 'end' not called #150

Closed d4rken closed 7 years ago

d4rken commented 7 years ago

Originates from int statusCode = urlConnection.getResponseCode();.

(line numbers from Dispatcher are not accurate in the stacktrace)

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 'end' not called
    at dalvik.system.CloseGuard.open(CloseGuard.java:180)
    at java.util.zip.Inflater.<init>(Inflater.java:104)
    at com.android.okhttp.okio.GzipSource.<init>(GzipSource.java:62)
    at com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:645)
    at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:821)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:463)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:521)
    at org.piwik.sdk.dispatcher.Dispatcher.dispatch(Dispatcher.java:250)
    at org.piwik.sdk.dispatcher.Dispatcher$1.run(Dispatcher.java:169)
    at java.lang.Thread.run(Thread.java:761)

Only visible with:

        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .build());

First thought internal Android bug, but the same happens when using okhttp:3.6.0. Google fu shows lots of other reports, but no clear cut solution.

Not really sure if this even is an issue, lets note this here for now.

aegis123 commented 7 years ago

I also seem to get it on BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));

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 com.android.org.conscrypt.Platform.closeGuardOpen(Platform.java:282)
    at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:316)
    at com.android.okhttp.Connection.connectTls(Connection.java:235)
    at com.android.okhttp.Connection.connectSocket(Connection.java:199)
    at com.android.okhttp.Connection.connect(Connection.java:172)
    at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:367)
    at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:130)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:329)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:257)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java)
    at org.piwik.sdk.dispatcher.Dispatcher.dispatch(Dispatcher.java:210)
    at org.piwik.sdk.dispatcher.Dispatcher$1.run(Dispatcher.java:159)
    at java.lang.Thread.run(Thread.java:761)
d4rken commented 7 years ago

@aegis123 always? or randomly? What Android version?

aegis123 commented 7 years ago

Im running 7.1.1 on a nexus 5x and it seems randomly.

the easiest fix seem to wrap it in a try catch and always close the BufferedWriter.

d4rken commented 7 years ago

Seeing it too on 7.1.1, just on 7+.

I just noticed that we should close the stream in a finally, depending on where an exception happens we might not close it. From my logs though, there was no exception before this strictmode warning, so I don't think this is the issue.

d4rken commented 7 years ago

HttpURLConnection.getResponseCode() opens another stream which we didn't close and if an exception happens, we didn't close the gzip stream either. So this should now be fixed.