auth0-java icon indicating copy to clipboard operation
auth0-java copied to clipboard

OkHttpClient response not closed on exception

Open pnam0228 opened this issue 3 years ago • 1 comments

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.

pnam0228 avatar Aug 05 '22 18:08 pnam0228

Hi @pnam0228, thank you for raising this issue with clear details. We will take a look into it and fix this issue.

poovamraj avatar Aug 07 '22 21:08 poovamraj

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!

jimmyjames avatar Aug 24 '22 19:08 jimmyjames

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.

jimmyjames avatar Oct 05 '22 02:10 jimmyjames