Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetty websocket read timeout

I'm trying to setup a websocket client/server. The problem arises due to inactivity in the session where after around 30~60s of no transfer of data, the client closes the connection, with the exception:

org.eclipse.jetty.websocket.api.WebSocketTimeoutException: Timeout on Read
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onReadTimeout(AbstractWebSocketConnection.java:505)
at org.eclipse.jetty.io.AbstractConnection.onFillInterestedFailed(AbstractConnection.java:258)
at org.eclipse.jetty.websocket.common.io.AbstractWebSocketConnection.onFillInterestedFailed(AbstractWebSocketConnection.java:481)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.failed(AbstractConnection.java:415)
at org.eclipse.jetty.io.FillInterest.onFail(FillInterest.java:100)
at org.eclipse.jetty.io.AbstractEndPoint.onIdleExpired(AbstractEndPoint.java:146)
at org.eclipse.jetty.io.IdleTimeout.checkIdleTimeout(IdleTimeout.java:153)
at org.eclipse.jetty.io.IdleTimeout$1.run(IdleTimeout.java:50)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)

ES: Socket Closed: [1001] Idle Timeout

It makes no sense since I've tried both on the client and the server side to set the maxIdleTimeout to much larger values, and even after the session is established, I check it:

client.setMaxIdleTimeout(0);

I've tried different values instead of the "0" above to no avail.

like image 912
Issues-Java Avatar asked Nov 01 '22 23:11

Issues-Java


1 Answers

This issue is 2 years old but still exists. The reason behind it is Jetty default websocket timeout is 5 minutes (see org.eclipse.jetty.websocket.api.WebSocketPolicy), while on Tomcat it is 0.

To fix it, in your WebSocket client code you can do something along these lines:

 WebSocketContainer container = ContainerProvider.getWebSocketContainer();
 Session session = container.connectToServer(this, endpointURI);
 session.setMaxIdleTimeout(0);
  • tested with Jetty 9.3.9.v20160517 and Tomcat 8+ - the issue stopped appearing in Jetty while Tomcat behaves the same.

However, it might be a good idea to implement @OnError and do session.close() and connect again to keep the connection stable. Also, Jetty's WebSocketTimeoutException is derived from RuntimeException, you might check instance of Throwable and reconnect on RuntimeExceptions only. And "real" error processing for situations like server is down etc. should be in IOException processing of container.connectToServer()

like image 79
Alex Pakka Avatar answered Nov 17 '22 10:11

Alex Pakka