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.

EOFException: source exhausted prematurely

See original GitHub issue

I’m always getting java.io.EOFException: source exhausted prematurely when I try a request using OkHttp (2.x, 3.x).

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
    .url("http://www.ddanzi.com")
    .build();   
Response response = client.newCall(request).execute();
System.out.println(response.body().string().length());

output

java.io.EOFException: source exhausted prematurely
    at okio.InflaterSource.read(InflaterSource.java:82)
    at okio.GzipSource.read(GzipSource.java:80)
    at okio.Buffer.writeAll(Buffer.java:956)
    at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:92)
    at okhttp3.ResponseBody.bytes(ResponseBody.java:83)
    at okhttp3.ResponseBody.string(ResponseBody.java:109)

But if I add an Accept-Encoding header to a request addHeader("Accept-Encoding", "gzip") there is no problem. Does anyone know why?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
JakeWhartoncommented, Nov 16, 2017

Actually bytes+headers would be awesome since we could repro in a unit test, in theory. Do you have a payload that doesn’t contain anything secret? Or can your server engs set up an example one that you can capture the raw bytes from?

On Wed, Nov 15, 2017, 7:43 PM Eric Denman notifications@github.com wrote:

Hey team! My server guys just migrated us from one type of AppEngine server to another, and now my okhttp connections all fail with this error. I’ve isolated it to: okhttp:3.9.0 and retrofit:2.3.0 HttpLoggingInterceptor with level BODY I hooked up Charles proxy and got the raw headers coming back on the response:

Server nginx Date Thu, 16 Nov 2017 00:30:11 GMT Content-Type application/json; charset=UTF-8 Cache-Control no-cache, no-store, must-revalidate Content-Encoding gzip Expires 0 Pragma no-cache Vary Accept-Encoding X-Appengine-Log-Flush-Count 0 Via 1.1 google Alt-Svc hq=“:443”; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=“:443”; ma=2592000; v=“41,39,38,37,35” Transfer-Encoding chunked

I’m entirely willing to support the “there’s a bug in the webserver” hypothesis, but there’s no Content-Length header on the response. Any other ideas of what could be wrong here? Or suggestions for how to investigate further? What would a test of this even look like…raw response bytes?

(for now, I’m just gonna turn off BODY logging, but I’d really like to have that capability added back, it’s super helpful for debugging)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/square/okhttp/issues/2193#issuecomment-344777662, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEETTdqR-YX4gjKfyW0A67n02h5JOvks5s24UVgaJpZM4G-MVs .

0reactions
ethanyuwangcommented, Aug 29, 2018

I’m having the same problem. Here are the logged request and response:

request:

I/System.out: INFO: Sending request http://test.essaybot.com/msg/tag_search on Connection{test.essaybot.com:80, proxy=DIRECT@ hostAddress=test.essaybot.com/34.208.145.50:80 cipherSuite=none protocol=http/1.1}
I/System.out: Content-Type: application/x-www-form-urlencoded
I/System.out: Content-Length: 33
I/System.out: Host: test.essaybot.com
I/System.out: Connection: Keep-Alive
I/System.out: Accept-Encoding: gzip
I/System.out: User-Agent: okhttp/3.11.0

Response:

I/System.out: INFO: Received response for http://test.essaybot.com/msg/tag_search in 62.2ms
I/System.out: Cache-Control: no-cache
I/System.out: Content-Encoding: gzip
I/System.out: Content-Length: 139
I/System.out: Content-Type: application/json
I/System.out: Set-Cookie: PHP_SESSION=0Q4rZJplDjrUNB4ZbWAG; Path=/; Max-Age=2592000
I/System.out: Set-Cookie: VISITOR_ID=65xGr53M1xM0waK8; Path=/; Max-Age=31536000
I/System.out: Date: Wed, 29 Aug 2018 20:45:06 GMT
I/System.out: INFO: Received response body bytes: 
I/System.out:  [31, -117, 8, 0, 0, 0, 0, 0, 0, -1, 44, -51, 65, 10, -62, 64, 12, 5, -48, -85, 72, -42, 93, -72, -18, -50, -91, 103, 16, -111, -23, 52, -83, 31, 66, -90, 100, 50, -94, -120, 119, -105, 116, -70, 9, 47, -16, -109, -1, 37, 54, 123, 104, -95, -15, 60, -112, 113, 109, -30, 52, -34, -24, 50, -65, 88, -67, 25, -45, 16, 70, -34, 97, 30, 115, 49, -28, 20, -128, 9, -108, 107, 80, -33, 96, -1, -12, 12, 22, 100, 36, 57, 93, -43, 89, 4, 43, 107, -65, -34, -74, 61, 58, -49, -56, -114, -94, -31, 86, -35, -110, -32, -8, 54, -23, 20, 88, -95, 107, -81, 101, 115, -44, 99, -109, 92, -98, 69, 66, 21, -119, -18, -65, 63, 0, 0, 0, -1, -1] 
I/System.out:  With length: 139

Content-Length is the correct length for response body. What else could cause this? Our server works fine with the web and iOS ends

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting "source exhausted prematurely" when inflating gzip ...
The server data is corrupt. It's supposed to contain a gzip trailer to indicate the end of the stream and that's missing.
Read more >
java.io.EOFException: source exhausted prematurely - B4X
EOFException : source exhausted prematurely. i have a server side function (VB.Net) to download the data, had some error but found a solution ......
Read more >
Getting "Source Exhausted Prematurely" When ... - ADocLib
I'm getting this error when I try to make a HTTP call with okhttp: W/System.err: java.io.EOFException: source exhausted prematurely W/System.err: at okio.
Read more >
okio/src/main/java/okio/InflaterSource.java - Google Git
public final class InflaterSource implements Source { ... if (sourceExhausted) throw new EOFException("source exhausted prematurely");.
Read more >
【OkHttp】java.io.EOFException: source exhausted prematurely
GCPのError Reportingで以下のエラーが検知されていた java.io.EOFException: source exhausted prematurely AppEngineがある外部サービスのAPI ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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