WARNING: A connection to https://... was leaked. Did you forget to close a response body?
See original GitHub issueVersion:
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;
}
Issue Analytics
- State:
- Created 8 years ago
- Reactions:12
- Comments:62 (19 by maintainers)
Top Results From Across the Web
java - A connection to (...) was leaked. Did you forget to close ...
It is an error to close a response that is not eligible for a body. This includes the responses returned from cacheResponse ...
Read more >How to solve : Log: A connection to (...) was leaked, Did you ...
HTTP timeout: 60000 milliseconds. A connection to https://googleads.g.doubleclick.net/ was leaked. Did you forget to close a response body?
Read more >Java Api Client - OkHttpClient warning - Developers & API
“OkHttpClient: A connection to https://app.asana.com/ was leaked. Did you forget to close a response body?” I just followed the docs created ...
Read more >connection to sonarqube was leaked - Google Groups
A connection to https://sonarqube.company.com/ was leaked. Did you forget to close a response body? java.lang.Throwable: response.body().close()
Read more >A connection to xxx was leaked. Did you forget to close a ...
Issue · Elasticsearch pods produces many WARNING: A connection to xxx was leaked. Did you forget to close a response body? log messages...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That code does not close the body when the code is not 200. Use a try/finally with
body.close()
in the finally block.You have to close the body. Just because you are not calling the method doesn’t mean the body is not there.
Ignoring this fact, the code you showed doesn’t cover the case when
body.string()
throws either.