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

W/OkHttpClient: A connection to https://xxx.com/ was leaked. Did you forget to close a response body? #226

Closed findById closed 6 years ago

findById commented 6 years ago
Timber.tag(LOGGER_TAG).v("Transmission succesful (code=%d).", statusCode);
// add fix code in org.piwik.sdk.dispatcher.DefaultPacketSender#97 (start)
try {
    if (statusCode == HttpURLConnection.HTTP_OK) {
        // fix W/OkHttpClient: A connection to https://xxx.com/ was leaked. Did you forget to close a response body?
        InputStream is = urlConnection.getInputStream();
        if (is != null) {
            is.close();
        }
    }
} catch (Throwable ignore) {
}
// add fix code end
d4rken commented 6 years ago

Link to the specific section

https://github.com/matomo-org/piwik-sdk-android/blob/2638439e19aa08979ecfa0e20c58f5c653436218/piwik-sdk/src/main/java/org/piwik/sdk/dispatcher/DefaultPacketSender.java#L95

So this is likely a bug, good catch :+1:

While the Android documentation does not mention in it's example that the input stream needs to be explicitly closed, they read the inputstream completely.

So as far as I understood it you either need to read the input stream completely, at which point HttpURLConnection.disconnect() will close it or explicitly close it beforehand.