Skip to content

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

@edallagnol

Description

@edallagnol

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions