question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WARNING: A connection to https://... was leaked. Did you forget to close a response body?

See original GitHub issue

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;
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:12
  • Comments:62 (19 by maintainers)

github_iconTop GitHub Comments

71reactions
JakeWhartoncommented, Feb 3, 2016

That code does not close the body when the code is not 200. Use a try/finally with body.close() in the finally block.

34reactions
JakeWhartoncommented, Feb 3, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found