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.

OkHttp3 - IOException: unexpected end of stream on okhttp3.Address@9d7c59b5

See original GitHub issue

The error occurs when OkHttp try to reuse a connection that is in FIN_WAIT2 state in server, because the server keep_alive timeout is lesser than the client timeout.

StackTrace:

Exception in thread "main" java.io.IOException: unexpected end of stream on okhttp3.Address@e06a4fc7
    at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:201)
    at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:53)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
    at okhttp3.RealCall.execute(RealCall.java:60)
    at br.com.edallagnol.test.App.main(App.java:54)
Caused by: java.io.EOFException: \n not found: size=0 content=…
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:215)
    at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
    ... 17 more

Steps to reproduce:

-Configure a Tomcat(7 or 8) Server with a keep_alive timeout of 60 seconds (default); -Create a OkHttpClient with retryOnConnectionFailure(false) and the default ConnectionPool (5 min timeout); -Do a GET request with client.newCall(request).execute(); -Wait more than 60 seconds (or monitor the connection state, until FIN_WAIT2 state); -Do a GET request with client.newCall(request).execute(), using the same client;

Sample code:

        OkHttpClient client = new OkHttpClient.Builder()
                .retryOnConnectionFailure(false)
                .build();

        for (int i=0; i != 10; i++) {
            Response response = client.newCall(new Request.Builder()
                    .url("http://localhost:8080/")
                    .get()
                    .build()).execute();
            try {
                System.out.println(response.body().string());
            } finally {
                response.close();
            }

            Thread.sleep(61000);
        }

*OkHttp version 3.4.1.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:15
  • Comments:28 (5 by maintainers)

github_iconTop GitHub Comments

16reactions
swankjessecommented, Jul 21, 2016

Here’s your fix:

OkHttpClient client = new OkHttpClient.Builder()
    .retryOnConnectionFailure(true)
    .build();
9reactions
vishnuelectriccommented, Jan 19, 2017

response header (“Connection”:“close”) solved this issue in my case

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android OkHttp HTTP FAILED: java.io.IOException
I/okhttp.OkHttpClient: <-- HTTP FAILED: java.io.IOException: unexpected end of stream on http://10.0.2.2:30000/... java.io.
Read more >
unexpected end of stream on com.android.okhttp.Address ...
Hi,. Why do I keep getting below error when trying to use simple PostAsync()? For example, below code is used to update the...
Read more >
unexpected end of stream on okhttp3.Address@2c98c95e - B4X
Hi all, today I'm encountering this error message while connecting to my VPS where a B4J module is listening in order to serve...
Read more >
IDEA-289610 - YouTrack
Can't connect to docker via ssh, getting: java.io.IOException: unexpected end of stream on http://127.0.0.1/... · As per machine to which I ssh: Client:...
Read more >
IOException unexpected end of stream when listing locations ...
It appears that okhttp3 is falling over on itself inside the API. If I resend the request it tends to then go through...
Read more >

github_iconTop Related Medium Post

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