-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
WARNING: A connection to https://... was leaked. Did you forget to close a response body? #2311
Comments
That code does not close the body when the code is not 200. Use a try/finally with |
|
Everytime. |
Ok, it's called eveytime, as you see in my code. But I still get that warning. |
I get that warning from time to time. I make like 100 calls and get 1 warning. 99 calls are ok. |
Code is the same in all 100 calls. |
Your code calls it only in the success case, which is not every time.
This is false. |
I commented each valuable line.
|
You have to close the body. Just because you are not calling the method doesn't mean the body is not there. ResponseBody body = resp.body();
try {
...
} finally {
body.close();
} Ignoring this fact, the code you showed doesn't cover the case when |
What about this case:
|
The connection is open. That's where the code came from. You always have to close the body. There's nothing ambiguous about "always". |
#2158 (comment) |
Calling |
So, just to read the code of the get response, I have to write something like:
|
Nope Request req = new Request.Builder().url(url).build();
Response res = ok.newCall(req).execute();
System.out.println(resp.code());
res.body().close(); |
The question was answered earlier, so I'm closing the issue. |
So this is the right one:
|
That code doesn't close the body in non-200 cases. |
That's a bit weird thing to remember, to close something you didn't explicitly open :) |
You opened it by making the request. Calling body just accesses something that already exists. |
That's the right (ugly) one:
|
So if I call body.string() and it throws exception, body remains opened and has to be closed, right? |
I mean body.string() closes body implicitly only in positive case with no exceptions, and remains it opened if the exception is thrown, right? |
I think body.string() automatically closed. There is like
so you don't need 200 checks. |
@iNoles If that was the case, the first version of the code wouldn't cause warnings, I guess. |
body.string() which closes body anyway, if there's or there's no exception - that would be a nice cute API :) |
The first version doesn't close in the non-200 case.
On Wed, Feb 3, 2016, 6:53 PM bxqgit [email protected] wrote:
|
private String get(final String url) {
String res = null;
try {
final Request req = new Request.Builder().url(url).get().build();
final Response response = ok.newCall(req).execute();
if (!response.isSuccessful()) {
log.warn(new IOException("Unexpected code " + response));
} else {
res = response.body().string();
}
catch (Throwable th) {
log.warn(th.getMessage());
}
return res;
} |
@JakeWharton ok, then this should work:
|
should fix hyperledger-web3j#710 by implementing square/okhttp#2311
should fix hyperledger-web3j#710 by implementing square/okhttp#2311
should fix hyperledger-web3j#710 by implementing square/okhttp#2311
should fix hyperledger-web3j#710 by implementing square/okhttp#2311
Just saw this in my logs from Firebase Crashlytics init. Hopefully relevant / authoritative:
|
I am so glad can the comments from author. In my case I am useing Response rather than ResponseBody, so how can I close . import android.content.Intent; import androidx.annotation.NonNull; import com.heitouyang.promise.activity.LoginActivity; import java.net.SocketTimeoutException; import retrofit2.Call; /**
public abstract class ZCallBackWithFail implements Callback {
} |
I found "OkHttpClient: A connection to http://test-api.heitouyang.cn/ was leaked. Did you forget to close a response body?" should throw from glide. I do nothing about the glide OkHttpClient. How can I solve this with glide. |
It is wired. According to try-with-resources
public final class Response implements Closeable {
... So I think this would work try (Response response = postCall(WRITE_LOG, payload)) {
return true;
} because response would be closed by |
body should be closed ref: square/okhttp#2311 Currently warnings are generated from okhttp: ``` com.spotify.githubclient.shade.okhttp3.internal.platform.Platform log WARNING: A connection to https://ghe.spotify.net/ was leaked. Did you forget to close a response body? To see where this was allocated, set the OkHttpClient logger level to FINE: Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE); ```
Fixing this in your code might be as simple as updating return client.newCall(request).execute().code == 200 to return client.newCall(request).execute().use {
it.code == 200
} |
|
Version:
com.squareup.okhttp3:okhttp:3.0.1
Log message:
Code:
The text was updated successfully, but these errors were encountered: