okhttp: OkHttp3 - IOException: unexpected end of stream on okhttp3.Address@9d7c59b5
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.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 15
- Comments: 31 (5 by maintainers)
Commits related to this issue
- Fix OkHttp3 unexpected end of stream error reference https://github.com/square/okhttp/issues/2738 — committed to wkh237/react-native-fetch-blob by wkh237 8 years ago
- Add workaround for #41. The integration tests occasionally fail due to https://github.com/square/okhttp/issues/2738. This appears to only affect the integration tests on OS X, and only occurs occasio... — committed to batect/batect by charleskorn 5 years ago
- Fix OkHttp3 unexpected end of stream error reference https://github.com/square/okhttp/issues/2738 — committed to hotcappuccinodev/reactnative-fetch-blob by deleted user 8 years ago
Here’s your fix:
response header (“Connection”:“close”) solved this issue in my case
As far as I can tell, there’s no efficient way to detect that the connection has been closed. If you can find one, we’d love to use it!
@AjayVenkataRajuKarri to what version?
Ohhh… guys, I can’t understand why I faced with the same issue, no one of workaround doesn’t help me
My stack trace:
Client builder
openjdk 11.0.2 okhttp 3.14.2 win 10
addHeader(“Connection”,“close”)? can solve this problem? same as call retryOnConnectionFailure(true)?? please give a reply ,thanks
In case someone still encounters this issue. Try not to run your app in the emulator of Android Studio. Instead, run it in your mobile.
This works for me.
The OkHttpClient’s default request protocol is HTTP2.0. Some Web Services are required HTTP1.1 requests.
Add protocols(listOf(Protocol.HTTP_1_1)) line to your OkHttpClient.
Example;
val okHttpClient = OkHttpClient.Builder() .protocols(listOf(Protocol.HTTP_1_1)) .readTimeout(40, TimeUnit.SECONDS) .connectTimeout(40, TimeUnit.SECONDS) .addInterceptor(loggingInterceptor) .addInterceptor(authInterceptor) .retryOnConnectionFailure(true) .build()
You can try to change “http://” to “https://”
@pc9292 please create a new issue with a standalone executable test case.
We keep getting this exception . Shouldn’t the connection be automatically closed after this exception ?
Caused by: java.io.IOException: unexpected end of stream on Connection at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:205) at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:75) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:211) 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:93) 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:120) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) at okhttp3.RealCall.execute(RealCall.java:69)