OkHttpClient response not closed on exception
Describe the problem you'd like to have solved
okhttp3.OkHttpClient - A connection to {some_auth0_url} was leaked. Did you forget to close a response body?
We're seeing this warning sometimes when calling TokenRequest.execute() at times. I believe response here
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try {
T parsedResponse = parseResponse(response);
future.complete(parsedResponse);
} catch (Auth0Exception e) {
future.completeExceptionally(e);
}
}
should be closed in the catch block.
Describe the ideal solution
Replace
catch (Auth0Exception e) {
future.completeExceptionally(e);
}
with
catch (Auth0Exception e) {
response.close();
future.completeExceptionally(e);
}
Alternatives and current work-arounds
The warning doesn't much affect our application, so we're ignoring it.
Hi @pnam0228, thank you for raising this issue with clear details. We will take a look into it and fix this issue.
Thanks @pnam0228. Can you provide some sample code and/or failures conditions to reproduce? I'm not able to reproduce, and am seeing the response closed on exception cases (both for synchronous and asynchronous request executions).
Also curious, you mention observing the issue at times with TokenRequest.execute(), but the code sample and suggested change is in the async execution (executeAsync). Have you observed issues with execute(), executeAsync(), or both?
FWIW, OkHttp will close the response when it is executed within a try block, or when several other methods on the response body are called, as documented here. If we can get a scenario in which we can reproduce this issue we can further investigate to figure out the scenario causing the response to not close. Thanks!
Closing this since we can't reproduce. Please chime in or open a new issue with reproducible steps if the issue persists so we can look into it.