question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Httpclient opening too many files on linux

See original GitHub issue

Hello.

I’ve started using this lib to do Oauth in a production enviroment, and since then we are facing a problem.

The app, from times to times is having an Too Many Open Files exception. We have checked and the class that is opening the files is in the dependency httpclient-nio, called by the OauthService here.

Somehow, i think this lib is opening files for each http async request, and never closes it.

Here is how i’m calling the lib:

LoginController.java:

public class LoginController implements Serializable {
	OAuth20Service oauthService = new ServiceBuilder(OAuthADFSApi.instance().getClientId())
			.apiSecret(OAuthADFSApi.instance().getSecret())
			.callback(OAuthADFSApi.instance().getCallback())
			.httpClientConfig(ApacheHttpClientConfig.defaultConfig())
			.build(OAuthADFSApi.instance());

...

public void callbackOAuth() throws IOException {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest req   = (HttpServletRequest) context.getExternalContext().getRequest();
		HttpServletResponse resp = (HttpServletResponse) context.getExternalContext().getResponse();

		String error = req.getParameter("error");
		if ((null != error) && ("access_denied".equals(error.trim()))) {
			HttpSession sess = req.getSession();
			sess.invalidate();
			resp.sendRedirect(req.getContextPath());
			return;
		}

		String code = req.getParameter("code");
		OAuth2AccessToken accessToken = null;
		try {
			accessToken = oauthService.getAccessTokenAsync(code).get();
			JsonObject profile = (JsonObject) new JsonParser().parse(accessToken.getRawResponse());
			DecodedJWT jwt = JWT.decode(profile.get("id_token").toString());

			String login = extrairLoginDosClaims(jwt);

			Usuario usuario = usuarioService.logar(login, null);
			contextFacade.setUsuarioLogado(usuario);
			resp.sendRedirect("pages/home.xhtml");
		} catch (Exception e){
			ELContext elContext = context.getELContext();
			ValueExpression erroValueExpression = context.getApplication().getExpressionFactory().createValueExpression(elContext, "#{erroController}", ErroController.class);
			ErroController erroController = (ErroController) erroValueExpression.getValue(elContext);
			erroController.setDetalhesMensagem(e.getCause() != null? e.getCause().getMessage() : e.getMessage());
			context.getExternalContext().redirect("erro.xhtml");
		}
	}

Should i be calling any “close” or anything on this lib? Or am i doing something wrong?

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kullfarcommented, Oct 5, 2018

session scope - that’s the reason I think. You do not need so many http clients 😉 You should make oauthService a singleton either create http client once and use it for all OAuthServices as in “any externally created HTTP client example” (https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/VkontakteExternalHttpExample.java)

0reactions
kullfarcommented, Oct 8, 2018

welcome. as for apache lib, it shouldn’t close their files. It’s their feature. to reuse already opened files, connections, etc. to save cpu/memory and other resources.

It’s not a cheap operation to create http client and it’s extra and redundant spend of resource to create such powerful http client for just one http request.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.net.SocketException: Too many open files - Stack Overflow
I'm using apache-commons HTTPClient 4. Here's the exception log: java.net.SocketException: Too many open files at java.net.Socket.createImpl(Socket.java ...
Read more >
HttpClient in dotnet core – too many files open
HttpClient implements IDisposable so that many developers would correctly assume calling Dispose() method right after HttpClient is not needed ...
Read more >
Resolving problems with too many open files
The problem is usually interimittent; the web server is continually closing files or sockets that it has opened, so the limit is reached...
Read more >
[HttpClient] Failed to open stream: Too many open files #44900
Symfony version(s) affected 4.5.2 / 5.3.13 Description When launching PHPUnit test suite, I have this error: PHPUnit 9.5.11 by Sebastian ...
Read more >
rest client - 'failed java.net.SocketException:Too many open files'
open files ' error occurs. The quick fix is to add a requestHeader "Connection"="close" which tells the HttpCLient to close the connection by ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found