Okhttp: java.io.IOException: unexpected end of stream on com.squareup.okhttp.Address@a7b169ed

Created on 23 Dec 2015  Â·  15Comments  Â·  Source: square/okhttp

I am getting this exception on some devices and some requests are working and others are not working.How to fix this

Most helpful comment

I have the same error. Could you share the fix?. Please.

Thank a lot.

All 15 comments

If this is a bug, please isolate it into a test case so we can investigate & fix. If it’s a support request, please use stackoverflow instead.

I think I'm seeing the same thing when connecting to an HTTPS address if retryConnectionFailure is set to false on OkHttpClient.

We were running into this issue using OkHttp via Retrofit for inter-microservice RPC.

The fix (which seemed surprising given the description of ConnectionPool's behaviour) was to explicitly give each OkHttpClient a new ConnectionPool (previously it was unspecified and falling through to the ConnectionPool.systemDefault, now we give each OkHttpClient a new ConnectionPool(5, 300000)) and our problem has gone away.

Also worth noting is this was really only manifesting on clients doing HTTPS with clientAuth.

At this point I haven't dug deeper to explain why this actually fixes things because we've applied it against Retrofit 1 / OkHttp 2 and we need to address that dead end first.

I have the same error. Could you share the fix?. Please.

Thank a lot.

I have same problem in okhttp3. How to resolve this issue ?

I have same problem in okhttp3. How to resolve this issue ?

having same issue here too, it just randomly appears.

@swankjesse, I can reproduce this problem.
All you need is to create a websocket server using socketio

var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function(client){
  client.on('event', function(data){});
  client.on('disconnect', function(){});
});
server.listen(3000);

When I use this url "ws://echo.websocket.org", my code run correctly, but when I switch to "ws://localhost:3000", then WebSocketListener's onFailure print stacktrace like this:

java.io.IOException: unexpected end of stream on okhttp3.Address@b9c05ad8
    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.access$100(RealCall.java:33)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:120)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
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)
    ... 21 more

So maybe it's a bug on socketio or some compatibility issue.

I use Python websockets to create another websocket server, works fine.So maybe something wrong with the nodejs code (I am still learning node :) , for now i still have not a clue why socketio failed).

I also have this issue, but only on mobile data connection. Is there any fix? Thank you!

so many same issue here .
no any solution ?

Facing the same issue for HttpURLConnection any solution?

Use OkHttp

On Thu, Oct 18, 2018 at 2:47 AM AndroDevMili notifications@github.com
wrote:

Facing the same issue for HttpURLConnection any solution?

—
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/2147#issuecomment-430894369, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAEEEUjm0K23RHwGiBX0z5wjNoKtMILKks5umCQTgaJpZM4G6Zcs
.

@JakeWharton Ok I will add OkHttp.

Fixed the issue by explicitly specifying the "Host:MyHostName" in the requests** for the following set up:

Android-Retrofit-OkHttp > Nginx (TLS enabled) > Gunicorn > Flask application.

Details:

  1. As a test, OkHttp could connect to the Gunicorn server directly on the same host, but when Nginx was used for connection, the app error-ed out with "unexpected end of stream".

  2. To ensure the issue wasn't with TLS configuration, a) made the same requests through Firefox, Openssl s_client, and curl: all of which returned a correct response to the request; and b) made non-TLS (http) request to a Non-TLS configured Nginx server: got the same unexpected end of stream error.

  3. The Nginx access logs showed a 444 response to the received request in the access logs: the configured response for requests with incorrect server name.

  4. Updated the request to add the "Host:MyHostName" header: this resolved the issue.

  5. Removed the "Host:MyHostName" header from the request, and reproduced the issue. Added the "Host:MyHostName" header back, and the issue was fixed.

Note: in the dev environment where this issue occurred, the prior-to-this-fix "Host" header in the generated requests was "10.0.2.2:4433" -- as picked from the request base url -- instead of "10.0.2.2" (which is basically "127.0.0.1" for the emulator). Whereas the Nginx Server was configured for "127.0.0.1" as server_name in the Server context.

Either I've misused the concept or probably an issue with how Retrofit/OkHttp parses host-name out of the base URL? With a port specified, one would expect it to pick just the IP part as host-name.

Was this page helpful?
0 / 5 - 0 ratings