okhttp: EOFException in RealBufferedSource.readUtf8LineStrict(): 0-bytes in stream
We’re getting an EOF exception with size=0 in the stream.
java.io.EOFException: \n not found: size=0 content=...
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:200)
at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:791)
at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:90)
(Forked from issue 1114.)
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 79 (13 by maintainers)
If you called
setRetryOnConnectionFailure(false);
on your connection, you will never have a solution to prevent those i/o errors.Java’s sockets have per design no way of knowing TCP connections are in state
CLOSE_WAIT
without trying to read from them.OkHTTP or whatever library you use will send data into the closed connection, try to read from it, (only then) realize that it was closed, open/use another one and send the request again.
Configuring a very long Keep-Alive duration on your servers will prevent most of the errors, but if your clients are behind any kind of proxies, your configuration won’t be relevant anymore. Also, if your servers handle many requests and connection, the ops team that configures your front servers might not like the ideas of having tens thousands of unused opened connections.
Me too, I am getting “unexpected end of stream” caused by: java.io.EOFException: \n not found: size=0 content=…
I am using Volley with okHttp 3.2.0. Web server is based on Tomcat. it happens often, but not always (maybe 10% of POST requests). The iOS version of the same application does not have this problem, so it seems an issue that could be solved client side.
Are there any workarounds?
Caused by: java.io.IOException: unexpected end of stream on okhttp3.Address@96651c04 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:199) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:723) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:595) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:481) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:417) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:549) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:26) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:110) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: … 1 more 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: Caused by: java.io.EOFException: \n not found: size=0 content=… 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:201) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184) 02-26 11:44:10.952 29152-29152/at.spar.mobile.interspar_app W/System.err: … 11 more
I solved this In my envrionment. In my case, when OkHttp tries to re-use a connection which has been closed by my tomcat server, the exception happens. My solution is to configure the tomcat server’s “keepAliveTimeout” > than the client’s connection timeout so that the server will never close a connection before the client.
Thanks @JakeWharton
I faced the same issue
Caused by: java.io.EOFException: \n not found: limit=0 content=…
.The issue was when requests were made in conjunction with
Connection: keep-alive
Transfer-Encoding: chunked
with a long JSON response in the body, but the body was not written according to Chucked Transfer Encoding spec. https://en.wikipedia.org/wiki/Chunked_transfer_encodingLike the suggestions above, adding
Connection: close
resolved the issue since it triggered to useUnknownLengthSource
instead ofChunkedSource
.Alternative workaround is reading
byte[]
fromResponseBody.byteStream()
, however this would mean the server response is not following Http1.1 spec where it sendsTransfer-Encoding: chunked
but the body format is not insertingOR,
the response header can specify
Content-Length
header.For me this happened when i connected to a server with HTTP, but it actually should have used HTTPS, check your URL!
hi guys, recently we changed our server and now i am using bellow using HttpLoggingInterceptor and the error java.io.EOFException: \n not found: limit=1 content=0d… gone!!!
Now the issue is fixed, error was from server side, changed hosting server and issue was fixed automatically. earlier i was hosting on TSOHost where this issue was arraving so i think this error its because of server
Using
okHttpVersion = "3.10.0"
I’m using kotlin, but it is still readable i think. I’m writing this answer without any IDE, i’m just using this comment box, sorry if any mistake:@crossle did you fix the issue?
I am not getting proper solution how can i fixed it.
@ZeWaren Thank you for your answer.
Getting this issue on Huawei P6-U06 4.2.2. The problem seems to appear quite often only while downloading large files e.g. 600 Mb. The only solution is to make a connection retry?( I’am using a GET request.