okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

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

Open bxqgit opened this issue 10 years ago • 63 comments

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

bxqgit avatar Feb 03 '16 21:02 bxqgit

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

JakeWharton avatar Feb 03 '16 21:02 JakeWharton

  1. 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?
  2. 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?

bxqgit avatar Feb 03 '16 22:02 bxqgit

Everytime.

swankjesse avatar Feb 03 '16 22:02 swankjesse

Ok, it's called eveytime, as you see in my code. But I still get that warning.

bxqgit avatar Feb 03 '16 22:02 bxqgit

I get that warning from time to time. I make like 100 calls and get 1 warning. 99 calls are ok.

bxqgit avatar Feb 03 '16 22:02 bxqgit

Code is the same in all 100 calls.

bxqgit avatar Feb 03 '16 22:02 bxqgit

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.

JakeWharton avatar Feb 03 '16 22:02 JakeWharton

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

bxqgit avatar Feb 03 '16 23:02 bxqgit

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.

JakeWharton avatar Feb 03 '16 23:02 JakeWharton

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

bxqgit avatar Feb 03 '16 23:02 bxqgit

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".

JakeWharton avatar Feb 03 '16 23:02 JakeWharton

https://github.com/square/okhttp/issues/2158#issuecomment-167283937 "Most call body().string() which closes it for you." How is that?

bxqgit avatar Feb 03 '16 23:02 bxqgit

Calling string() closes the body, yes.

JakeWharton avatar Feb 03 '16 23:02 JakeWharton

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

bxqgit avatar Feb 03 '16 23:02 bxqgit

Nope

Request req = new Request.Builder().url(url).build();
Response res = ok.newCall(req).execute();
System.out.println(resp.code());
res.body().close();

JakeWharton avatar Feb 03 '16 23:02 JakeWharton

The question was answered earlier, so I'm closing the issue.

JakeWharton avatar Feb 03 '16 23:02 JakeWharton

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

bxqgit avatar Feb 03 '16 23:02 bxqgit

That code doesn't close the body in non-200 cases.

JakeWharton avatar Feb 03 '16 23:02 JakeWharton

That's a bit weird thing to remember, to close something you didn't explicitly open :)

bxqgit avatar Feb 03 '16 23:02 bxqgit

You opened it by making the request. Calling body just accesses something that already exists.

JakeWharton avatar Feb 03 '16 23:02 JakeWharton