Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WARNING [com.squareup.okhttp.OkHttpClient] (OkHttp ConnectionPool) A connection to https://api.github.com/ was leaked. Did you forget to close a response body? #997

Closed
soul2zimate opened this issue Dec 3, 2020 · 10 comments · Fixed by #998
Labels

Comments

@soul2zimate
Copy link

WARNING [com.squareup.okhttp.OkHttpClient] (OkHttp ConnectionPool) A connection to https://api.github.com/ was leaked. Did you forget to close a response body?

I am using version 1.116. I sometimes see this warning message in my server log, there is no real occurrence pattern. I am not sure why that happened, but it seems it relates to bad network connection.

In my code, I have a static GitHub instance and use it periodically to communicate with Github.

    protected static GitHub github;

...
// create the github instance from initialization like below 
...

    github = new GitHubBuilder()
            .withOAuthToken(config.getPassword(), config.getUsername())
            .withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache))))
            .build();

I can see similar message mentioned in a previous pull request #346 on Feb 23, 2017 trying to ignore the warning from okhttp.

Does this really a harmful messages ? How should I prevent this in github-api since I don't use okhttp directly.

Thank you.

@ghost ghost mentioned this issue Dec 3, 2020
@bitwiseman
Copy link
Member

@soul2zimate
#984 probably addresses this issue. It will be in the next release.

@bitwiseman bitwiseman added the bug label Dec 4, 2020
@soul2zimate
Copy link
Author

Great! looking forward to the next release. thanks.

@bitwiseman
Copy link
Member

@soul2zimate If you want to help out, you could build the code from that PR and see if it works for your case.

@soul2zimate
Copy link
Author

sure, I will build a 1.118-SNAPSHOT and test it at the local.

@soul2zimate
Copy link
Author

2020-12-04 10:27:55,670 WARNING [com.squareup.okhttp.OkHttpClient] (OkHttp ConnectionPool) A connection to https://api.github.com/ was leaked. Did you forget to close a response body?

Unfortunately, it still sporadically presents in log with the fix mentioned in your 1st comment. Does is also close the response body in case of unexpected IO exception from network lag?

@bitwiseman
Copy link
Member

@soul2zimate
There is probably a path where it does not. This covers cases where response info is created.

IOUtils.closeQuietly(responseInfo);

But can totally believe there is a path through all the obsolete code that gets a body and then doesn't close it.

protected GitHubResponse.ResponseInfo getResponseInfo(GitHubRequest request) throws IOException {
HttpURLConnection connection;
try {
connection = HttpURLConnectionResponseInfo.setupConnection(this, request);
} catch (IOException e) {
// An error in here should be wrapped to bypass http exception wrapping.
throw new GHIOException(e.getMessage(), e);
}
// HttpUrlConnection is nuts. This call opens the connection and gets a response.
// Putting this on it's own line for ease of debugging if needed.
int statusCode = connection.getResponseCode();
Map<String, List<String>> headers = connection.getHeaderFields();
return new HttpURLConnectionResponseInfo(request, statusCode, headers, connection);

It is also possible that some part of your code is the cause - if the caller gets a stream and doesn't close it, the leak could still happen. Are you using any of the methods that return InputStream, such as GHRepository.readBlob(), or GHRepository.renderMarkdown() or GHTreeEntry.readAsBlob()?

I'm laying blame just looking for possibilities. I thought we'd mostly closed the problem.

@bitwiseman
Copy link
Member

@soul2zimate
Oh, wait. Your output says com.squareup.okhttp.OkHttpClient. Are you using okhttp or okhttp3?

@soul2zimate
Copy link
Author

Because I need to use the conditional request against GitHub, I am using okhttp as pluggable HTTP client with okhttp-urlconnection version 2.7.5

The way I instantiate the GitHub in issue description is basically as same as the last Pluggable HTTP client section on https://github-api.kohsuke.org/

bitwiseman added a commit to bitwiseman/github-api that referenced this issue Dec 12, 2020
OkHttp 2.x is unsupported.  OkHttpUrlFactory contains bugs and limiations which will not
be fixed and also cannot be mitigated by this library.  Users should move to OkHttp3.

Closes hub4j#997
bitwiseman added a commit to bitwiseman/github-api that referenced this issue Dec 12, 2020
OkHttp 2.x is unsupported.  OkHttpUrlFactory contains bugs and limiations which will not
be fixed and also cannot be mitigated by this library.  Users should move to OkHttp3.

Closes hub4j#997
@bitwiseman
Copy link
Member

@soul2zimate
Try switching to okhttp3 v3.14.x. It should be a drop in replacement if all you're doing is using URLConnection. I'm deprecating support for 2.x.

@soul2zimate
Copy link
Author

soul2zimate commented Dec 15, 2020

Thanks, I have tried with okhttp3, the warning seems gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants