square/okhttp

EOFException on websocket

roman-upnext opened this issue · 11 comments

After connecting to webservice every single minute (seems like some ping) I'm getting the below exception (which forces me to reconnect).

java.io.EOFException
         at okio.RealBufferedSource.require(RealBufferedSource.java:59)
         at okio.RealBufferedSource.readByte(RealBufferedSource.java:72)
         at okhttp3.internal.ws.WebSocketReader.readHeader(WebSocketReader.java:113)
         at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:97)
         at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.java:261)
         at okhttp3.internal.ws.RealWebSocket$2.onResponse(RealWebSocket.java:200)
         at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
         at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
         at java.lang.Thread.run(Thread.java:818)

Looks like your server is kicking you off. If so there’s nothing we can do.

How many websockets are you creating? Usually you create on, use it for a long time, and then close it.

I'm using only a single websocket in a single Activity.
I have no problems connecting (onOpen callback is fired) and I can receive messages in onMessage during this one minute.

Weird. This error is triggered when a new websocket is being created.

Ok, thanks for the explanation. I will investigate further as online services like http://jsfiddle.net/EAVvQ/24/ seem to also have this issue (they stop receiving messages but without any error)

@swankjesse you were right, after 60s of inactivity the server was closing the socket.
The solution for this is of course setting a ping interval on OkHttpClient:

.pingInterval(30, TimeUnit.SECONDS)

@swankjesse I have the same problem as @roman-upnext but at the time to close the socket. I trigger the close method webSocket.close(1000, null); as in WebSocketEcho example and I always receive back the EOFException through onFailure method from listener public void onFailure(WebSocket webSocket, Throwable t, Response response)

Exception:

java.io.EOFException
at okio.RealBufferedSource.require(RealBufferedSource.java:59)
at okio.RealBufferedSource.readByte(RealBufferedSource.java:72)
at okhttp3.internal.ws.WebSocketReader.readHeader(WebSocketReader.java:113)
at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:97)
at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.java:262)
at okhttp3.internal.ws.RealWebSocket$2.onResponse(RealWebSocket.java:201)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)

@DavidSerr which server are you using? Some of ’em don’t do a graceful shutdown. You might be able to ignore this exception.

@swankjesse you are right, I have changed the connection url to use another fake server just for connection/disconnection checking the output when I trigger close method and I have not received the EOFException, so it is server side bug. I am using Play Framework WebSocket with Scala.

I am seeing this same error in onFailure in version 3.14.6.

java.io.EOFException: null
	at okio.RealBufferedSource.require(RealBufferedSource.java:65) ~[okio-1.17.2.jar:?]
	at okio.RealBufferedSource.readByte(RealBufferedSource.java:78) ~[okio-1.17.2.jar:?]
	at okhttp3.internal.ws.WebSocketReader.readHeader(WebSocketReader.java:117) ~[okhttp-3.14.6.jar:?]
	at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:101) ~[okhttp-3.14.6.jar:?]
	at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.java:273) [okhttp-3.14.6.jar:?]
	at okhttp3.internal.ws.RealWebSocket$1.onResponse(RealWebSocket.java:209) [okhttp-3.14.6.jar:?]
	at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174) [okhttp-3.14.6.jar:?]
	at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) [okhttp-3.14.6.jar:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_211]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_211]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_211]

My websocket server is a tyrus server. As suggested by @roman-upnext I have set a ping interval also builder.pingInterval(1, TimeUnit.SECONDS);, but still getting this issue.

I am also not sure if setting ping interval actually sends any message to server, because in server log I can't see anything

@OnMessage
public void onMessage(String message, Session session) {
    try {
        log.info("Message received at server {}", message);

Any idea how to address this?

@anidotnet I think your server is kicking you!