OkHttp connection leak log line even when OkHttp is not a dependency

醉酒当歌 提交于 2021-02-05 06:13:23

问题


I keep seeing the following log line in Logcat while I use my app:

19098-19147/<package> W/OkHttpClient: A connection to <my server> was leaked. Did you forget to close a response body?

I did some research on this bug and found that it can happen when you do things like forget to close a response body in an interceptor. I commented out all of interceptors to see if one of them was causing this issue, but I still saw the log line. I eventually commented out all uses of OkHttp and I still somehow got the error. I even went as far as to removing all OkHttp dependencies from my Gradle file and added an explicit line to make sure that it was excluded as a transitive dependency. I ran gradle app:dependencies in order to generate my dependency tree so I could make sure that OkHttp was not included. Somehow I am STILL seeing this log line. I don't understand how this is possible.

Does anyone know of any common libraries that may have copied and pasted this log line out of the OkHttp library and into theirs? I searched all of the source code of my other dependencies, but didn't find a similar log line in any of them.

Could this have something to do with the new profiling code that Android Studio injects? See this article for more information.

Update: Turns out that my Fresco dependency must have been using OkHttp provided by the system or something like that. @Selvin commented that Android uses it internally. Anyways, apparently when Fresco receives an HTTP response with an error when trying to load an image (in my case HTTP response code 401), it logs this error. I don't see any good way for handling HTTP errors with Fresco/OkHttp. I opened the following issue with Fresco: HTTP error response when loading image results in leaked connection #1983


回答1:


I know it's very late response but maybe anyone is still getting crazy with this issue and finally I found out what happened. Yes: OkHttp is used internally in HttpURLConnection/HttpsURLConnection components since Android 4.4 so you can get these boring messages in logcat, even if you're not using OkHttp library directly in your project.

The problem is generated by 2 factors:

  1. the socket is reused by the urlConnection object, trying to optmizing performances on multiple requests on the same host.
  2. the response stream is not correctly closed (see below how to solve)

To prevent these log lines or simply take more control on the flow I made some changes to my code and finally solved this issue.

  • I disabled persistent connections setting Connection: close header. Default is Connection: keep-alive
  • Before reading the content (body) of the response, check the http response code. As well described in this article if the response code is >= 400 you got an unsuccess: you have not to read the connection InputStream getInputStream() but the ErrorStream getErrorStream() and close it by calling .close() method on the right stream. The connection leak is here. Finally remember to disconnect() the urlConnection object.


来源:https://stackoverflow.com/questions/47666053/okhttp-connection-leak-log-line-even-when-okhttp-is-not-a-dependency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!