okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

EOFException in RealBufferedSource.readUtf8LineStrict(): corrupt stream

Open swankjesse opened this issue 11 years ago • 12 comments

We're getting an EOF exception because the response data isn't as expected. The stream is not empty, but it also doesn't contain the data we expect.

 java.io.EOFException: \n not found: size=45 content=32320d0a7b225374616e6a6541706c696b6163696a65223a7b22536966726122...
       at okio.RealBufferedSource.readUtf8LineStrict(SourceFile:200)
       at com.squareup.okhttp.internal.http.HttpConnection$ChunkedSource.readChunkSize(SourceFile:468)
       at com.squareup.okhttp.internal.http.HttpConnection$ChunkedSource.read(SourceFile:450)
       at okio.RealBufferedSource$1.read(SourceFile:298)

(Forked from issue 1114.)

swankjesse avatar Mar 19 '15 21:03 swankjesse

for android 4.2.2 (vivo y17t/100+v6) wifi and 3G

If the server does not support keepalive:

java.io.IOException: unexpected end of stream on Connection{59.46.115.124:11800, proxy=DIRECT@ hostAddress=59.46.115.124 cipherSuite=none protocol=http/1.1} (recycle count=0)
at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:210)
at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:830)
at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:95)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:823)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:684)
at com.squareup.okhttp.Call.getResponse(Call.java:272)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
at com.squareup.okhttp.Call.execute(Call.java:79)
at com.uu.shit.connection.NetToJavaRequest.requestServer(NetToJavaRequest.java:27)
at com.uu.shit.connection.ConnectionTest.doOwnTest(ConnectionTest.java:23)
at com.uu.shit.MainTest.doTest(MainTest.java:71)
at com.uu.shit.ShitActivity$2$1.run(ShitActivity.java:87)
at java.lang.Thread.run(Thread.java:838)
Caused by: java.io.EOFException: \n not found: size=1460 content=446174653a205468752c203236204d617220323031352030343a30333a303120...

duanhongyi avatar Mar 26 '15 06:03 duanhongyi

https + keepalive

java.io.InterruptedIOException: timeout
    at okio.AsyncTimeout.exit(AsyncTimeout.java:249)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:217)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:289)
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
    at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:190)
    at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
    at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:830)
    at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:95)
    at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:823)
    at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:684)
    at com.squareup.okhttp.Call.getResponse(Call.java:272)
    at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
    at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
    at com.squareup.okhttp.Call.execute(Call.java:79)
    at com.uu.shit.connection.NetToJavaRequest.requestServer(NetToJavaRequest.java:27)
    at com.uu.shit.connection.ConnectionTest.doOwnTest(ConnectionTest.java:23)
    at com.uu.shit.MainTest.doTest(MainTest.java:59)
    at com.uu.shit.ShitActivity$2$1.run(ShitActivity.java:87)
    at java.lang.Thread.run(Thread.java:838)

duanhongyi avatar Mar 26 '15 06:03 duanhongyi

http + keepalive

java.net.SocketTimeoutException
    at java.net.PlainSocketImpl.read(PlainSocketImpl.java:495)
    at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
    at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
    at okio.Okio$2.read(Okio.java:137)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:289)
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
    at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:190)
    at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
    at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:830)
    at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:95)
    at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:823)
    at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:684)
    at com.squareup.okhttp.Call.getResponse(Call.java:272)
    at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
    at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
    at com.squareup.okhttp.Call.execute(Call.java:79)
    at com.uu.shit.connection.NetToJavaRequest.requestServer(NetToJavaRequest.java:27)
    at com.uu.shit.connection.ConnectionTest.doOwnTest(ConnectionTest.java:23)
    at com.uu.shit.MainTest.doTest(MainTest.java:54)
    at com.uu.shit.ShitActivity$2$1.run(ShitActivity.java:87)
    at java.lang.Thread.run(Thread.java:838)

duanhongyi avatar Mar 26 '15 06:03 duanhongyi

Problem with the code:

public class HttpClientFactory {
    public static int MAX_IDLE_CONNECTIONS = 30;
    public static int KEEP_ALIVE_DURATION_MS = 3 * 60 * 1000;
    private static final ThreadLocal<OkHttpClient> local = new ThreadLocal<OkHttpClient>();


    private static OkHttpClient _newHttpClient() {
    OkHttpClient client = new OkHttpClient();
    com.squareup.okhttp.internal.Internal.instance.setNetwork(client, new Network() {
        DNSQuery query = new CachedDNSQuery();
        @Override
        public InetAddress[] resolveInetAddresses(String string) throws UnknownHostException {
            return query.query(string);
        }
    });
    client.setConnectionPool(new com.squareup.okhttp.ConnectionPool(
            MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION_MS));
    return client;
    }

    public static OkHttpClient currentHttpClient() {
    OkHttpClient client = local.get();
    if(client == null){
        client = _newHttpClient();
        local.set(client);
    }
    return client;
    }
}

Another Problem with the code:

public class HttpClientFactory {
    public static int MAX_IDLE_CONNECTIONS = 30;
    public static int KEEP_ALIVE_DURATION_MS = 3 * 60 * 1000;

    private static OkHttpClient _newHttpClient() {
    OkHttpClient client = new OkHttpClient();
    com.squareup.okhttp.internal.Internal.instance.setNetwork(client, new Network() {
        DNSQuery query = new CachedDNSQuery();
        @Override
        public InetAddress[] resolveInetAddresses(String string) throws UnknownHostException {
            return query.query(string);
        }
    });
    return client;
    }

    public static OkHttpClient currentHttpClient() {
       return _newHttpClient();
    }
}

No problem code:

public class HttpClientFactory {
    public static int MAX_IDLE_CONNECTIONS = 30;
    public static int KEEP_ALIVE_DURATION_MS = 3 * 60 * 1000;

    private static OkHttpClient _newHttpClient() {
    OkHttpClient client = new OkHttpClient();
    com.squareup.okhttp.internal.Internal.instance.setNetwork(client, new Network() {
        DNSQuery query = new CachedDNSQuery();
        @Override
        public InetAddress[] resolveInetAddresses(String string) throws UnknownHostException {
            return query.query(string);
        }
    });
    client.setConnectionPool(new com.squareup.okhttp.ConnectionPool(
            MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION_MS));
    return client;
    }

    public static OkHttpClient currentHttpClient() {
       return _newHttpClient();
    }
}

Important code:

client.setConnectionPool(new com.squareup.okhttp.ConnectionPool(MAX_IDLE_CONNECTIONS, KEEP_ALIVE_DURATION_MS));

So each connection will use the new ConnectionPool, means not multiplex connection.To solve the problem, but the performance decreases a lot.

duanhongyi avatar Mar 26 '15 06:03 duanhongyi

I know how to solve this problem.

String str1 = "SFRUUC8xLjEgNDA0IE5PVCBGT1VORA0KU2VydmVyOiBuZ2lueC8xLjYuMg0KRGF0ZTogVHVlLCAzMSBNYXIgMjAxNSAwNzo1NzoyMSBHTVQNCkNvbnRlbnQtVHlwZTogdGV4dC9odG1sDQpDb250ZW50LUxlbmd0aDogMjMzDQpDb25uZWN0aW9uOiBrZWVwLWFsaXZlDQoNCjwhRE9DVFlQRSBIVE1MIFBVQkxJQyAiLS8vVzNDLy9EVEQgSFRNTCAzLjIgRmluYWwvL0VOIj4KPHRpdGxlPjQwNCBOb3QgRm91bmQ8L3RpdGxlPgo8aDE+Tm90IEZvdW5kPC9oMT4KPHA+VGhlIHJlcXVlc3RlZCBVUkwgd2FzIG5vdCBmb3VuZCBvbiB0aGUgc2VydmVyLiAgSWYgeW91IGVudGVyZWQgdGhlIFVSTCBtYW51YWxseSBwbGVhc2UgY2hlY2sgeW91ciBzcGVsbGluZyBhbmQgdHJ5IGFnYWluLjwvcD4K";

Buffer buffer = new Buffer(); buffer.write(Base64.decode(str1)); System.out.println( "index:" + Long.toString(buffer.indexOf((byte)'\n')));

In some Android models will have half of the probability of return -1;After about 2 weeks of tracking,i found some models of JIT existed in bug,Just close jit will all normal.

add: android:vmSafeMode="true" In the Android configuration file

As for why will appear in the okio.Buffer, this I have no further investigation.

duanhongyi avatar Mar 31 '15 08:03 duanhongyi

@duanhongyi i found in android debug model correct,but in normal model error, log is: java.io.IOException: unexpected end of stream on Connection{ip:11800, proxy=DIRECT@ hostAddress=ip cipherSuite=none protocol=http/1.1} (recycle count=0)

or

callback not trigger,the android main thread block!!!

yq76034150 avatar Mar 31 '15 17:03 yq76034150

@duanhongyi got a specific device that is impacted here? I'm trying to reproduce.

swankjesse avatar Mar 31 '15 22:03 swankjesse

(if you can name an emulator, even better)

swankjesse avatar Mar 31 '15 22:03 swankjesse

@swankjesse

Yes, Many devices have problems.

eg:

HUAWEI H60-L11 Coolpad 9976A for miui v5 100+ v6 for miui v5 Vivo Y17t

These are test mobile phone in our company.

Close the JIT android:vmSafeMode="true", everything is normal.

duanhongyi avatar Apr 01 '15 00:04 duanhongyi

VIVO Y17T, first request: java.net.SocketTimeoutException at java.net.PlainSocketImpl.read(PlainSocketImpl.java:495) at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) at okio.Okio$2.read(Okio.java:137) at okio.AsyncTimeout$2.read(AsyncTimeout.java:211) at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300) at okio.RealBufferedSource.indexOf(RealBufferedSource.java:289) at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196) at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:190) at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80) at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:830) at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:95) at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:823) at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:684) at com.squareup.okhttp.Call.getResponse(Call.java:272) at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228) at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199) at com.squareup.okhttp.Call.execute(Call.java:79)

iooly avatar Apr 10 '15 13:04 iooly

Here's a gist that demonstrates the problem with the impacted devices. They pass the test on the first few iterations, but eventually fail it. https://gist.github.com/swankjesse/4396e276e43dbb0e71cc

swankjesse avatar Jul 31 '15 06:07 swankjesse

Hopefully this fixes it with Okio 1.6.0: https://github.com/square/okio/pull/164

swankjesse avatar Jul 31 '15 07:07 swankjesse