okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

com.squareup.okhttp.ConnectionPool pruneAndGetAllocationCount

Open leelit opened this issue 10 years ago • 11 comments

when i ran my unit test, which contains a lot of, may be more than 10, requests, it came out a warning "A connection to http://localhost:8080/ was leaked. Did you forget to close a response body?"

why did this warning come out? how to solve it?

leelit avatar Dec 26 '15 05:12 leelit

Are you closing the response body of all requests made in the test?

JakeWharton avatar Dec 26 '15 05:12 JakeWharton

the sample https://github.com/square/okhttp/wiki/Recipes doesn't close response body neither.

normally i won't sumit so many requests at the same time, do i need to close response body all the time? just a little confused...

leelit avatar Dec 26 '15 06:12 leelit

Which sample? Most call body().string() which closes it for you.

JakeWharton avatar Dec 26 '15 06:12 JakeWharton

you mean if i call body().string(), no need to call body().close(), other situation it needs ?

after i call close for all response body except body().string(), no more warning.

leelit avatar Dec 26 '15 06:12 leelit

Correct. We will document which methods on ResponseBody automatically close it.

JakeWharton avatar Dec 26 '15 06:12 JakeWharton

If I use body().byteStream()do I need to close the connection explicitly as well?

kelmer44 avatar May 12 '16 15:05 kelmer44

Yes, per the javadoc, you'll need to close the body. Only bytes() and string() close it automatically. See https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/ResponseBody.java#L37

dave-r12 avatar May 12 '16 16:05 dave-r12

Just tried it but it seems if I close the stream after calling byteStream() I will then get an exception on processing the stream since it has already been closed.

kelmer44 avatar May 17 '16 08:05 kelmer44

I was confused ,,I got the warning when i used Retrofit ,,, I didn't have to do anything with the response body,how can I solve this ,,

I do have a custom interceptor ,, is it because of this ?

JerryLai8080 avatar Nov 10 '16 02:11 JerryLai8080

I also have the same problem using Retrofit when there are 2 concurrent http requests are under process. what could go wrong? I do have interceptors.

shashidhar34 avatar Nov 21 '16 08:11 shashidhar34

On a 64-bit API 25 Android emulator, the problem does not occur. But it occurs on the 32-bit emulator.

mb040574 avatar Sep 20 '17 20:09 mb040574