A connection to (…) was leaked. Did you forget to close a response body?

喜欢而已 提交于 2020-08-04 05:20:10

问题


I'm having a warning message constantly, despite my code seems to be good. The message is:

WARNING: A connection to http://someurl.com was leaked. Did you forget to close a response body?
java.lang.Throwable: response.body().close()
    at okhttp3.internal.platform.Platform.getStackTraceForCloseable(Platform.java:148)
    at okhttp3.RealCall.captureCallStackTrace(RealCall.java:89)
    at okhttp3.RealCall.execute(RealCall.java:73)
    at com.example.HTTPSClientReferenceRate.runClient(HTTPSClientReferenceRate.java:78)
    at com.example.HTTPSClientReferenceRate.main(HTTPSClientReferenceRate.java:137)

I'm working with Java 8. I've tried with the traditional try-catch and with this approach (try-with-resources):

boolean repeatRequest = true;

while(repeatRequest) {
    Call call = client.newCall(request);
    try (Response response = call.execute()){
        if (!response.isSuccessful()) {
            log.error("Error with the response: " + response.message());
            continue;
        }
        ResponseBody body = response.body();
        if (body == null){
            log.error("Error when getting body from the response: " + response.message());
            continue;
        }
        BufferedReader br = new BufferedReader(body.charStream());

        //...DATA HANDLING

    } catch (Exception e) {
        log.error("Error Connecting to the stream. Retrying... Error message: " + e.getMessage());
    }
}

In fact, the first if line is never called, I always have an exception, so I cannot understand why the response/body is not closed by the try-with-resources block

I've tried this option as well, but it didn't work either:

try (Response response = client.newCall(request).execute()) { ... }

EDIT

I've reduced my code, and I'm still having the same error, this is even weirder:

boolean repeatRequest = true;

while(repeatRequest) {
    Call call = client.newCall(request);
    try (Response response = call.execute()){
        //NOTHING
    } catch (Exception e) {
        log.error("Error Connecting to the stream. Retrying... Error message: " + e.getMessage());
    }
}

EDIT 2:

I've tried with the traditional try-catch but I'm still having the same issue:

boolean repeatRequest = true;

while(repeatRequest) {
    Call call = client.newCall(request);
    Response response = null;
    try {
        response = call.execute();
        try (ResponseBody body = response.body()) {
            //Nothing...
        }
    } catch (Exception e) {
        log.error("Error Connecting to the stream. Retrying... Error message: " + e.getMessage());
    } finally {
        if (response != null){
            response.close();
        }
    }
}

回答1:


As per Response.close() javadoc:

It is an error to close a response that is not eligible for a body. This includes the responses returned from cacheResponse, networkResponse, and priorResponse().

Perhaps your code should look like below, as per Github comment:

while (repeatRequest) {
    Call call = client.newCall(request);
    Response response = call.execute();
    try (ResponseBody body = response.body()) {
        ...
    }
}


来源:https://stackoverflow.com/questions/57260220/a-connection-to-was-leaked-did-you-forget-to-close-a-response-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!