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.

socket failed: EMFILE (Too many open files)

See original GitHub issue

Hi, I am using Retrofit 2.0.0. OkHttp 3.1.2. Some of my users are getting the following exception:

java.net.SocketException: socket failed: EMFILE (Too many open files) at libcore.io.IoBridge.socket(IoBridge.java:619) ~[na:0.0] at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198) ~[na:0.0] at java.net.Socket.checkOpenAndCreate(Socket.java:689) ~[na:0.0] at java.net.Socket.setSoTimeout(Socket.java:543) ~[na:0.0] at com.android.org.conscrypt.OpenSSLSocketImpl.setSoTimeout(OpenSSLSocketImpl.java:1140) ~[na:0.0] at com.android.org.conscrypt.OpenSSLSocketImplWrapper.setSoTimeout(OpenSSLSocketImplWrapper.java:111) ~[na:0.0] at okhttp3.internal.io.RealConnection.connectSocket(RealConnection.java:155) ~[na:0.0] at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111) ~[na:0.0] at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:188) ~[na:0.0] at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:127) ~[na:0.0] at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:97) ~[na:0.0] at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:289) ~[na:0.0] at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:241) ~[na:0.0] at okhttp3.RealCall.getResponse(RealCall.java:240) ~[na:0.0] at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198) ~[na:0.0]

When i look at crashlytics i see a lot of threads which are running:

at com.android.org.conscrypt.NativeCrypto.SSL_read(NativeCrypto.java) at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:811) at okio.Okio$2.read(Okio.java:139) at okio.AsyncTimeout$2.read(AsyncTimeout.java:211) at okio.RealBufferedSource.request(RealBufferedSource.java:71) at okio.RealBufferedSource.require(RealBufferedSource.java:64) at okio.RealBufferedSource.readInt(RealBufferedSource.java:232) at okhttp3.internal.framed.Spdy3$Reader.nextFrame(Spdy3.java:129) at okhttp3.internal.framed.FramedConnection$Reader.execute(FramedConnection.java:600) at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) at java.lang.Thread.run(Thread.java:818)

I am initiating Okhttp only once in my app this way: `OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); clientBuilder.connectTimeout(3 * 1000, TimeUnit.MILLISECONDS); clientBuilder.readTimeout(25 * 1000, TimeUnit.MILLISECONDS);

    File cacheDir = new File(mWeakContext.get().getFilesDir().getAbsolutePath(), "http-cache");
    Cache cache = new Cache(cacheDir, 16 * 1024 * 1024); // 16MB
    clientBuilder.cache(cache);

    // set proxy
    Properties properties = System.getProperties();
    if (properties.containsKey("https.proxyHost")) {

        String proxyHost = properties.getProperty("https.proxyHost");
        int proxyPort = Integer.parseInt(properties.getProperty("https.proxyPort"));

        clientBuilder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                proxyHost,
                proxyPort)
        ));
    }
    clientBuilder.addInterceptor(mHeadersInterceptor);
    clientBuilder.addInterceptor(mSignInterceptor);
    clientBuilder.addInterceptor(mLoggingInterceptor);
    mClient = clientBuilder.build();`

I am pretty clueless here since it doesn’t happen a lot and i cant reproduce it on my device.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
rahulchowdhurycommented, Jun 17, 2017

In case anyone’s still looking for a possible solution, here’s one,

I made the mistake of instantiating OkHttpClient on every API call while injecting dependencies manually in my app, which resulted in too many open sockets and thus this error.

Tip: Make sure you are having a single instance of OkHttpClient and Retrofit in your app and reusing that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ErrnoException: socket failed: EMFILE (Too many open files ...
It looks like the http connection is keeping too many files open even after making the calls. I have added the HTTP Async...
Read more >
How to fix java.net.SocketException: Too many files open java ...
Bottom line to fix java.net.SocketException: Too many files open, is that either increasing number of open file handles or reducing TCP TIME_WAIT timeout....
Read more >
How to Fix the 'Too Many Open Files' Error in Linux?
If the number of open files in Nginx is exceeded, a “ socket() failed (24: Too many open files) while connecting to upstream...
Read more >
Fixing the “Too many open files” Error in Linux - Baeldung
When working with Linux servers, we may encounter the “Too many open files” error. In this article, we'll go over what this error...
Read more >
How to solve "Too many Open Files" in Java applications
As a result, application requests are failing and you need to restart the. ... SocketException: Too many open files at java.net.
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