WARNING: A connection to https://... was leaked. Did you forget to close a response body?
Version:
com.squareup.okhttp3:okhttp:3.0.1
Log message:
<timestamp> okhttp3.ConnectionPool pruneAndGetAllocationCount
WARNING: A connection to https://... was leaked. Did you forget to close a response body?
Code:
private String get(final String url) {
String res = null;
try {
final Request req = new Request.Builder().url(url).get().build();
final Response resp = ok.newCall(req).execute();
final int code = resp.code();
if (code == 200) {
final ResponseBody body = resp.body();
res = body.string();
body.close(); // even this doesn't work!
}
}
catch (final Throwable th) {
System.out.println(th.getMessage());
}
return res;
}
That code does not close the body when the code is not 200. Use a try/finally with body.close() in the finally block.
- If the code is not 200 -> there's no call to resp.body() -> there's no body -> there's nothing to close. Is it so?
- I thought I don't have to call body.close() at all! I just put it there for an experiment. Do I have to call body.close() everytime?
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.
If the code is not 200 -> there's no call to resp.body() -> there's no body -> there's nothing to close.
This is false.
I commented each valuable line.
private String get(final String url) {
String res = null;
try {
final Request req = new Request.Builder().url(url).get().build();
final Response resp = ok.newCall(req).execute();
final int code = resp.code(); // can be any value
if (code == 200) {
final ResponseBody body = resp.body(); // body exists, I have to close it
res = body.string(); //
body.close(); // I close it explicitly
}
// if code was not 200 -> there's no body (opened to be closed) -> there's nothing to close
// What am I supposed to close?
}
catch (final Throwable th) {
System.out.println(th.getMessage());
}
return res;
}
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 body.string() throws either.
What about this case:
private String get(final String url) {
String res = null;
try {
final Request req = new Request.Builder().url(url).get().build();
final Response resp = ok.newCall(req).execute();
final int code = resp.code(); // can be any value
System.out.println(code); // I don't want to get body from resp, just print the code
// There's no body, there's nothing opened, there's nothing to close
// Do I have to call resp.body().close() anyway in finally block or something like that?
}
catch (final Throwable th) {
System.out.println(th.getMessage());
}
return res;
}
there's nothing opened
The connection is open. That's where the code came from. You always have to close the body. There's nothing ambiguous about "always".
https://github.com/square/okhttp/issues/2158#issuecomment-167283937 "Most call body().string() which closes it for you." How is that?
Calling string() closes the body, yes.
So, just to read the code of the get response, I have to write something like:
private void printCode(final String url) {
Response resp = null;
try {
final Request req = new Request.Builder().url(url).get().build();
resp = ok.newCall(req).execute();
final int code = resp.code();
System.out.println(code);
}
catch (final Throwable th) {
System.out.println(th.getMessage());
}
finally {
if (resp != null) {
try {
resp.body().close();
}
catch (final Throwable th) {
System.out.println(th.getMessage());
}
}
}
}
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:
private String get(final String url) {
String res = null;
try {
final Request req = new Request.Builder().url(url).get().build();
final Response resp = ok.newCall(req).execute();
final int code = resp.code();
if (code == 200) {
try (ResponseBody body = resp.body()) {
res = body.string();
}
}
}
catch (final Throwable th) {
System.out.println(th.getMessage());
}
return res;
}
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.