okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

EOFException in RealBufferedSource.readUtf8LineStrict

Open swankjesse opened this issue 12 years ago • 101 comments

A few people have reported this. Needs investigation & fix.

java.io.EOFException: null
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:154) ~[na:0.0]
    at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:189) ~[na:0.0]
    at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101) ~[na:0.0]
    at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:676) ~[na:0.0]
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:426) ~[na:0.0]
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:371) ~[na:0.0]
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:466) ~[na:0.0]
    at com.squareup.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105) ~[na:0.0]

swankjesse avatar Nov 01 '14 15:11 swankjesse

I haven't been able to reproduce this. Punting to 2.2.

swankjesse avatar Nov 05 '14 03:11 swankjesse

@swankjesse I am seeing a few of those in production. How can I help debugging?

niqo01 avatar Nov 27 '14 01:11 niqo01

@niqo01 can you reproduce locally or are you getting this through Crashlytics/Bugsnag etc. ?

swankjesse avatar Nov 28 '14 02:11 swankjesse

@swankjesse Getting through Crashlytics only. If you think about logs which would help debugging, I would be glad to add them to the next release.

niqo01 avatar Nov 28 '14 18:11 niqo01

@niqo01 any pattern in device maker or Android version?

I'll add something to OkHttp itself to provide more context when this is triggered.

swankjesse avatar Nov 29 '14 16:11 swankjesse

@swankjesse you can have a look at the crashlytics report here: http://crashes.to/s/816499ead0b I didn't recognized a possible pattern in device maker or Android version or specific HTTP request.

niqo01 avatar Nov 29 '14 20:11 niqo01

OkHttp 2.1. Device: Coolpad 8297. Version: Android 4.2.2. Same issues. I can confirm this device would cause this EOFException (my two users have this device reported this).

This is the one user's device's info. I hope this may help you.

Model number                      Coolpad 8297
Android version                   4.2.2 MIUI V5
MIUI version (An android rom)     MIUI-4.9.26
Processor                         8-core 1.7GHZ
RAM                               2.00 GB
ROM                               8.00 GB
Baseband version                  MOLY.WR8.W1315.MD.W
                                  G.MP.V19.P2,2014/03/11
                                                   00:55
Kernel version                    3.4.39

Simliar comment can be found here: https://github.com/square/okhttp/issues/803#issuecomment-60895738

floating-cat avatar Dec 01 '14 14:12 floating-cat

Hello @swankjesse , what I don't understand is why the error comes so quickly and not seeing a response log.

My experience is that either my server is down or a problem connecting to the server because of spotty connection that causes an EOFException for me.

12-10 17:21:27.453 30472-30652/? D/Retrofit log﹕ ---> HTTP POST https://placeholder.server.com//core/b1c51a7ec5204/ 12-10 17:21:27.463 30472-30652/? D/Retrofit log﹕ User-Agent: Android 12-10 17:21:27.463 30472-30652/? D/Retrofit log﹕ Content-Type: application/json; charset=UTF-8 12-10 17:21:27.463 30472-30652/? D/Retrofit log﹕ Content-Length: 121 12-10 17:21:27.463 30472-30652/? D/Retrofit log﹕ ---> END HTTP (121-byte body) 12-10 17:21:29.275 30472-30652/? D/Retrofit log﹕ ---- ERROR https://placeholder.server.com//core/b1c51a7ec5204/ 12-10 17:21:29.295 30472-30652/? D/Retrofit log﹕ java.io.EOFException at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:154) at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:189) at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)

simtse avatar Dec 11 '14 02:12 simtse

@swankjesse In the previous issue (#803), you theorized:

At first glance it looks like bad connection reuse, but that should be recovered from & retried automatically. Is this a POST request?

Are you referring to the fact that if the response gives HTTP 100 (HttpConnection:199), HttpConnection continues the loop to parse the StatusLine again (HttpConnection:187)?

I've attached the debugger to see if that's the case, and it's not; the exception is thrown the first time around, in my case on a HTTP 204.

I've been trying to put together a minimum working example of this problem, but to no avail. I'm only seeing it in a production environment where the API returns HTTP 204 with an empty body on a POST request.

I'm suspicious that perhaps the response coming in contains no newline '\n' character at all, as looking at RealBufferedSource:192 suggests that's the only cause of this exception. As the HTTP response should always contain at least one such character following the header, I'm properly puzzled.

On OkHttp 2.1.0, the line numbers appear to be slightly different; my stack trace is:

java.io.EOFException
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:192)
    at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
    at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:78)
    at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:665)
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:429)
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:374)
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:469)
    at com.squareup.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
    at com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
    at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
    at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
    at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
    [...]

pflammertsma avatar Dec 11 '14 18:12 pflammertsma

Is there a fix for this? I get the exception quite often

java.io.EOFException
            at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:192)
            at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:187)
            at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:78)
            at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:665)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:429)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:374)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:469)
            at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
            at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
            at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
            at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
            at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at retrofit.Platform$Android$2$1.run(Platform.java:142)
            at java.lang.Thread.run(Thread.java:818)

ArthurSav avatar Dec 18 '14 12:12 ArthurSav

This might add some context: https://github.com/square/okio/pull/104

swankjesse avatar Dec 28 '14 01:12 swankjesse

Yes, this would indeed be useful. I haven't grabbed the latest for a new test, but it seems that the newline is missing in empty body responses (such as HTTP 204) from some servers. Oddly, it's entirely random.

I'll dump the new exception when I encounter it again.

pflammertsma avatar Dec 28 '14 01:12 pflammertsma

Might be caused by attempting to parse SPDY or HTTP/2.0 as HTTP/1.1. We had a bug on desktop Java where our ALPN implementation wasn't always telling us the negotiated protocol.

With the new information in-hand from Okio's update, I'm going to punt this to 2.3 since there's no further action to take for the 2.2 release.

swankjesse avatar Dec 29 '14 03:12 swankjesse

@swankjesse Please note my extensive comment on this Retrofit issue.

johnjohndoe avatar Jan 06 '15 16:01 johnjohndoe

@swankjesse After many (many!) tries where everything just works hunky dory, I've finally gotten it to fail again with OkHttp 2.2. I don't know what to say than that it appears to be completely at random.

As with @johnjohndoe's report, the buffer is simply empty. Unlike his report, this is coming from a Apache 2.2.15 on CentOS, running PHP 5.4.36.

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.readResponse(HttpEngine.java:678)
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:431)
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:376)
    at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:471)
    at com.squareup.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
    at com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
    at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
    at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
    at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
    at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
    at $Proxy2.locationFinishSession(Native Method)
    at com.vebego.ally.proxies.LocationSessionProxy$2.doRequest(LocationSessionProxy.java:47)
    at com.vebego.ally.SyncService$2.doInBackground(SyncService.java:269)
    at com.vebego.ally.SyncService$2.doInBackground(SyncService.java:259)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:841)

pflammertsma avatar Jan 08 '15 02:01 pflammertsma

Perhaps it would be useful for logging that the headers are output before the rest of the response body is attempted to be read? Presently, it bombs out with the ---- ERROR message line immediately after the ---> END HTTP line.

The issue is so terribly difficult to reproduce that I can't attach the debugger and hope for it to occur on a breakpoint.

pflammertsma avatar Jan 08 '15 02:01 pflammertsma

Jesse and I talked about a Retrofit-like logging system for stupid-easy things like this. We have to be careful, though, because both headers and the response body can come from different places. In this case you want the headers logged before the body is read which is a very, very fine level of logging. On Jan 7, 2015 6:49 PM, "Paul Lammertsma" [email protected] wrote:

Perhaps it would be useful for logging that the headers are output before the rest of the response body is attempted to be read? The issue is so terribly difficult to reproduce that I can't attach the debugger and hope for it to occur on a breakpoint.

— Reply to this email directly or view it on GitHub https://github.com/square/okhttp/issues/1114#issuecomment-69128693.

JakeWharton avatar Jan 08 '15 03:01 JakeWharton

Yes, I can imagine, and looking at where those headers are read, it wouldn't be an ideal place to add logging.

I took another look at the stack trace, and if I understand correctly, noticed that it doesn't even get to reading the headers. The exception is thrown from HttpConnection:187, which seems to suggest that the response is a single line. With Jesse's added details in the stack trace, it appears that the response is actually completely empty. No status line, no headers, no body, nothing.

I'll throw together a bash script to test the API using cURL and see if it ever gets empty responses, but it sounds very strange to me.

pflammertsma avatar Jan 08 '15 03:01 pflammertsma

Hi, I met this problem every time trying to send an http request. Sure the Android OS of the device I used to test is bizarrely customized, but it's still worth a look.

Device Info: device: Coolpad 9976A Android version: 4.4.2 kernel: 3.4.39

Library Info: gson-2.3.1 okhttp-2.1.0 okhttp-urlconnection-2.1.0 okio-1.0.1 retrofit-1.8.0

t-gao avatar Jan 22 '15 04:01 t-gao

An additional informartion: When I was single-step debugging the code in DDMS, everything worked fine, I got the response correctly, but when I ran the APP, I got the exception.

t-gao avatar Jan 22 '15 04:01 t-gao